poj2516Minimum Cost最小费用最大流

本文介绍了一种解决特定运输问题的最小费用流算法,通过分解不同种类货物的运输过程,利用类似于EK最大流算法的方式,寻找使总运输成本最低的方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport. 

It's known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places' storage of K kinds of goods, N shopkeepers' order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers' orders, with each line containing K integers (there integers are belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places' storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in that supply place. 

Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper. 

The input is terminated with three "0"s. This test case should not be processed.

Output

For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output "-1".

Sample Input

1 3 3   
1 1 1
0 1 1
1 2 2
1 0 1
1 2 3
1 1 1
2 1 1

1 1 1
3
2
20

0 0 0

Sample Output

4
-1

Source

/*
 * 把k种物品分开算  再求和
 * 和EK最大流算法差不多,只不过EK算法是BFS搜索深度最小的可行路径作为增广路,而最小费用流是找权值最小的可行路径作为增广
 * 路。(这里可行路径指可以使流量增大的路径)
 * 这个代码完全是根据POJ1459改的,我把1459的代码贴在最下面了,就不讲了,还是画图,看代码才能理解。
 *
 *顺便说一下,有人说不知道增广路是什么,其实他啥也不是,在图论中,增广路就是能使结果更优的路径!
 *二分图中增广路是可以使匹配数更大的路径,最大流中增广路是使流量更大的路径,最小费用流中是使流量满足条件并使费用尽量小的路径
 */


//poj2516
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <climits>
#define  maxn 55
#define  MAX INT_MAX-11111111
#define  mem(a,b) memset(a,b,sizeof(a))

using namespace std;

int shopkeeper[maxn][maxn];
int storage[maxn][maxn];
int _map[maxn<<1][maxn<<1];
int capacity[maxn<<1][maxn<<1];
int prev[maxn<<1];
int flow[maxn<<1];
int shortest_dis[maxn<<1];
bool vis[maxn<<1];

int find_augment_path(int srt,int end,int n)
{
	int i;
	for(i=0;i<n;++i)
	{
		shortest_dis[i]=MAX;
	}
	shortest_dis[srt]=0;
	mem(prev,-1);
	mem(vis,0);
	flow[end]=INT_MAX;
	queue<int>que;
	que.push(srt);
	while(!que.empty())//spfa  突然发现和bfs好像
	{
		int now=que.front();
		que.pop();
		int i;
		for(i=0;i<n;++i)
		{
			if(capacity[now][i]>0 && shortest_dis[i]>shortest_dis[now]+_map[now][i])//流量为0的路不能走  相当于无穷大
			{
				shortest_dis[i]=shortest_dis[now]+_map[now][i];
				prev[i]=now;
				if(!vis[i])
				{
					vis[i]=true;
					que.push(i);
				}
			}
		}
		vis[now]=false;
	}
	if(prev[end]!=-1)
	{
		int k=end;
		while(prev[k]!=-1)
		{
			flow[prev[k]]=min(flow[k],capacity[prev[k]][k]);
			k=prev[k];
		}
		return flow[k];//flow[srt];
	}
	return -1;


}

int MaxflowEK(int srt,int end,int n)
{
	int up;
	int sum_flow=0;
	while((up=find_augment_path(srt,end,n))!=-1)
	{
		int k=end;
		int price_up=0;
		while(k!=srt)
		{
			int prev_node=prev[k];
			capacity[prev_node][k]-=up;
			capacity[k][prev_node]+=up;
			price_up+=_map[prev[k]][k]*up;
			k=prev[k];
		}
		sum_flow+=price_up;
	}
	return sum_flow;
}

int main()
{
	int num_shopkeeper,num_storage,num_kind_of_goods;
	while(scanf("%d%d%d",&num_shopkeeper,&num_storage,&num_kind_of_goods),num_storage+num_shopkeeper+num_kind_of_goods)
	{
		int i,j;
		for(i=0;i<num_shopkeeper;++i)
		{
			for(j=0;j<num_kind_of_goods;++j)
			{
				scanf("%d",&shopkeeper[i][j]);
			}
		}
		for(i=0;i<num_storage;++i)
		{
			for(j=0;j<num_kind_of_goods;++j)
			{
				scanf("%d",&storage[i][j]);
			}
		}
		int ans=0;
		for(int k=0;k<num_kind_of_goods;++k)
		{
			mem(capacity,0);
			mem(_map,0);
			int temp_storage=0;
			for(i=0;i<num_storage;++i)
			{
				capacity[0][i+1]=storage[i][k];
				temp_storage+=storage[i][k];
			}
			for(i=0;i<num_shopkeeper;++i)
			{
				capacity[i+num_storage+1][num_shopkeeper+num_storage+1]=shopkeeper[i][k];
				temp_storage-=shopkeeper[i][k];
			}
			for(i=1;i<=num_storage;++i)
			{
				for(j=1;j<=num_shopkeeper;++j)
				{
					capacity[i][j+num_storage]=storage[i-1][k];
				}
			}
			if(temp_storage<0)
			{
				ans=-1;
			}
			for(i=0;i<num_shopkeeper;++i)
			{
				for(j=0;j<num_storage;++j)
				{
					scanf("%d",&_map[j+1][i+1+num_storage]);
					_map[i+1+num_storage][j+1]=-_map[j+1][i+1+num_storage];
				}
			}
			if(ans!=-1)//已经不可完成 不再计算
				ans+=MaxflowEK(0,num_storage+num_shopkeeper+1,num_storage+num_shopkeeper+2);
		}
		printf("%d\n",ans);
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值