#POJ2516#Minimum Cost(拆点+Km完备匹配)

来源:互联网 发布:httpclient工具类json 编辑:程序博客网 时间:2024/05/18 01:41

Minimum Cost
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 16562 Accepted: 5817

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 10 1 11 2 21 0 11 2 31 1 12 1 11 1 132200 0 0

Sample Output

4-1

题目大意:

Dearboy有n个卖场,由m个库房供货。一共有k种商品。

现在告诉你n个卖场每种货物需要多少,m个库房每种货物有多少,求满足要求的最小的运输费用。

不同的供货基地给不同的卖场运送不同的货物收取的物流费是不同的。

数据规模:0<m,n,k<50,每个订单中每种货物的数量和每个库房中每种货物的库存数量都不超过3。


输入:

第一行是N,M,K

接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况

接下来M行:第j行有K个数字表示第j个库房对K种商品的存货情况

接下来K个N*M的矩阵:

每个矩阵(i,j)表示第k种商品从第j个库房运到第i个卖场的运费(单价)


拆点,对于第k种商品有:

把每个卖场的需求按件数拆成各个小点(比如需要x件就看做x个点)

把每个库房的存货量按件数拆成各个小点(同上)

然后以运费为权值连边(单向边)

做K次完备匹配即可


BFS实现:

StatusAcceptedTime94msMemory1664kBLength2441LangG++Submitted2017-05-01 12:11:48Shared

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;const int Max=55;const int Maxnum=105;const int inf=0x3f3f3f3f;int cnta,cntb,N,M,K;int cont[Max][2];bool viy[Max*Maxnum];int pre[Max*Maxnum],A[Max*Maxnum],B[Max*Maxnum];int match[Maxnum*Max],slack[Maxnum*Max],wx[Maxnum*Max],wy[Maxnum*Max];int store[Max][Max],need[Max][Max],cost[Max][Max][Max];int map[Max*Maxnum][Max*Maxnum];bool getint(int &num){char c;int flg=1;num=0;while((c=getchar())<'0'||c>'9')if(c=='-')flg=-1;while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}num*=flg;return 1;}void Bfs(int k){int px,py=0,delta,yy=0;match[py]=k;memset(slack,0x3f,sizeof(slack));memset(pre,0,sizeof(pre));do{delta=inf;px=match[py];viy[py]=1;for(int i=1; i<=cntb; ++i)if(!viy[i]){if(wx[px]+wy[i]-map[px][i]<slack[i]){slack[i]=wx[px]+wy[i]-map[px][i];pre[i]=py;}if(slack[i]<delta){delta=slack[i];yy=i;}}for(int i=0; i<=cntb; ++i)if(viy[i]){wx[match[i]]-=delta;wy[i]+=delta;}else slack[i]-=delta;py=yy;}while(match[py]!=0);while(py){match[py]=match[pre[py]];py=pre[py];}}int Km(){memset(wy,0,sizeof(wy));memset(match,0,sizeof(match));for(int i=1; i<=cnta; ++i){wx[i]=-inf;for(int j=1; j<=cntb; ++j)wx[i]=max(wx[i],map[i][j]);}for(int i=1; i<=cnta; ++i){for(int j=1; j<=cntb; ++j)viy[j]=0;Bfs(i);}int ans=0;for(int i=1; i<=cntb; ++i)ans+=wx[match[i]]+wy[i];return -ans;}int main(){while(getint(N)&&getint(M)&&getint(K)&&N&&M&&K){memset(cont,0,sizeof(cont));for(int i=1; i<=N; ++i)for(int j=1; j<=K; ++j)getint(need[i][j]),cont[j][0]+=need[i][j];for(int i=1; i<=M; ++i)for(int j=1; j<=K; ++j)getint(store[i][j]),cont[j][1]+=store[i][j];for(int k=1; k<=K; ++k)for(int i=1; i<=N; ++i)for(int j=1; j<=M; ++j)getint(cost[k][i][j]);bool flg=1;for(int i=1; i<=N; ++i)if(cont[i][0]>cont[i][1]){flg=0;break;}if(!flg){puts("-1");continue;}int Ans=0;for(int k=1; k<=K; ++k){cnta=0,cntb=0;for(int i=1; i<=N; ++i)while(need[i][k])A[++cnta]=i,--need[i][k];for(int i=1; i<=M; ++i)while(store[i][k])B[++cntb]=i,--store[i][k];for(int i=1; i<=cnta; ++i)for(int j=1; j<=cntb; ++j)map[i][j]=-cost[k][A[i]][B[j]];Ans+=Km();}printf("%d\n",Ans);}return 0;}














0 0
原创粉丝点击