POJ2516 Minimum Cost 最小费用流

本文详细介绍了如何解决POJ2516中的最小费用流问题,包括输入输出格式和示例,深入探讨了算法的构建和矩阵在其中的作用。
此题一开始想错了算法,直接将k个物品放在一个网络中求了一次最小费用。直接TLE。。。。后来看了DISCUSS发现对于每一种物品分开求最小费用最大流。这样就降低了点的数量。每次费用流最坏的情况就是102个点,而之前一次求的话点最坏可能到5000,边的数量更加恐怖。。。。。。
建图。
对于第k种物品
1:第i个商店和源点连,容量为相应物品的需求,费用为0
2:第i个仓库和汇点连,容量为相应的物品库存,费用为0
3:每一个商店和每一个仓库分别连线,容量INF,费用为相应的耗费。
这里注意。对于每种商品如果供小于求,那么我们就可以直接判断不可能,并且不去计算费用流了。但这里要注意把所有数据读完
Minimum Cost

Time Limit: 4000MS

 

Memory Limit: 65536K

Total Submissions: 11009

 

Accepted: 3700

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

 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>

using namespace std;

#define MAXN 300
#define MAXM 1000000
#define INF 0xFFFFFF
struct edge
{
	int to,c,w,next;
};

edge e[MAXM];
bool in[MAXN];
int stack[MAXN];
int head[MAXN],dis[MAXN],pre[MAXN],en,maxflow,mincost;
int vn,st,ed;
int n,m,k;
int nk[MAXN][MAXN],mk[MAXN][MAXN];

void add(int a,int b,int c,int d)
{
	e[en].to=b;
	e[en].c=c;
	e[en].w=d;
	e[en].next=head[a];
	head[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].w=-d;
	e[en].next=head[b];
	head[b]=en++;	
}

bool spfa(int s)
{
	int t=0,f=0;
	for(int i=0;i<=vn;i++)
	{
		dis[i]=INF;
		in[i]=false;
		pre[i]=-1;
	}
	dis[s]=0;
	in[s]=true;
	stack[t++]=s;
	while(t>f)
	{
		int tag=stack[--t];
		in[tag]=false;
		for(int i=head[tag];i!=-1;i=e[i].next)
		{
			int j=e[i].to;
			if(e[i].w+dis[tag]<dis[j] && e[i].c)
			{
				dis[j]=e[i].w+dis[tag];
				pre[j]=i;
				if(!in[j])
				{
					stack[t++]=j;
					in[j]=true;
				}
			}
		}
	}
	if(dis[ed]==INF)
		return false;
	return true;
}

void update()  
{  
	int flow=INF;  
    for (int i=pre[ed];i!=-1;i=pre[e[i^1].to])
		if(e[i].c<flow) flow=e[i].c;    
    for (int i=pre[ed];i!=-1;i=pre[e[i^1].to]) 
    {
		e[i].c-=flow,e[i^1].c+=flow;
	}   
    maxflow+=flow; 
    mincost+=flow*dis[ed];
}  

void mincostmaxflow()
{
	maxflow=0,mincost=0;
	while(spfa(st))
		update();
}

bool build(int j)
{
	int sum1=0,sum2=0;
	en=0,memset(head,-1,sizeof(head));
	st=0,ed=n+m+1,vn=n+m+2;
	for(int i=1;i<=n;i++)
		add(st,i,nk[i][j],0),sum1+=nk[i][j];
	for(int i=1;i<=m;i++)
		add(n+i,ed,mk[i][j],0),sum2+=mk[i][j];
	return sum1<=sum2;
	
}
void solve()
{
	int temp,ans=0;
	bool flag=true;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++)
		for(int j=1;j<=k;j++)
			scanf("%d",&nk[i][j]);
	for(int i=1;i<=m;i++)
		for(int j=1;j<=k;j++)
			scanf("%d",&mk[i][j]);
	for(int i=1;i<=k;i++)
	{	
		if(!build(i)) flag=false;
		for(int j=1;j<=n;j++)
		{
			for(int q=1;q<=m;q++)
			{
				scanf("%d",&temp);
				add(j,n+q,INF,temp);
			}
			
		}
		if(flag)
		{
			mincostmaxflow();
			ans+=mincost;
		}
	}
	if(flag)
		printf("%d\n",ans);
	else
		printf("-1\n");
}

int main()
{
	while(scanf("%d%d%d",&n,&m,&k)!=EOF && n+m+k)
		solve();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值