hdu 三部曲 1Minimum Cost 最小费用最大流EK算法
Problem 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.
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.
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
***************************************************************************************************************************
最小费用最大流EK算法 注:初始化很重要
***************************************************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 #include<stack> 7 #include<algorithm> 8 #define inf 0x7fffffff 9 using namespace std; 10 int a[61][61],b[61][61],c[61][61][61]; 11 int pre[201],cost[101][201]; 12 int dis[222],cap[121][221]; 13 int vis[211]; 14 int que[1000011]; 15 int n,m,k,i,j,src; 16 int end1,s; 17 int sum1[161],sum2[161]; 18 19 int bfs()//注意用STL超时 20 { 21 for(int it=0;it<=end1;it++) 22 dis[it]=inf; 23 memset(vis,0,sizeof(vis)); 24 int base,top; 25 base=top=0; 26 que[top++]=src; 27 dis[src]=0; 28 vis[src]=1; 29 pre[src]=0; 30 while(base<top) 31 { 32 int fs=que[base++]; 33 vis[fs]=0; 34 for(int it=1;it<=end1;it++) 35 { 36 if(it!=fs&&cap[fs][it]>0) 37 if(dis[it]>(dis[fs]+cost[fs][it]))//此处找到最小费用 38 { 39 pre[it]=fs; 40 dis[it]=dis[fs]+cost[fs][it]; 41 if(!vis[it]) 42 { 43 que[top++]=it; 44 vis[it]=1; 45 } 46 } 47 } 48 } 49 if(dis[end1]==inf) 50 return -1; 51 return 1; 52 53 } 54 int EK()//算出最小费用最大流 55 { 56 int jt,kt; 57 int sm=0; 58 int min1=inf; 59 while(1) 60 { 61 kt=bfs(); 62 if(kt==-1) 63 break; 64 for(int it=end1;it!=0;it=pre[it]) 65 { 66 jt=pre[it]; 67 if(min1>cap[jt][it]) 68 min1=cap[jt][it]; 69 } 70 for(int it=end1;it!=0;it=pre[it]) 71 { 72 jt=pre[it]; 73 sm+=cost[jt][it]*min1; 74 cap[jt][it]-=min1; 75 cap[it][jt]+=min1; 76 } 77 } 78 return sm; 79 80 } 81 int main() 82 { 83 while(scanf("%d%d%d",&n,&m,&k)!=EOF) 84 { 85 if(n==0&&m==0&&k==0) 86 break; 87 for(i=1;i<=n;i++) 88 for(j=1;j<=k;j++) 89 { 90 scanf("%d",&a[i][j]); 91 } 92 for(i=1;i<=m;i++) 93 for(j=1;j<=k;j++) 94 { 95 scanf("%d",&b[i][j]); 96 } 97 98 99 for(i=1;i<=k;i++) 100 for(j=1;j<=n;j++) 101 for(int gs=1;gs<=m;gs++) 102 scanf("%d",&c[i][j][gs]); 103 for(i=1;i<=k;i++) 104 { 105 int s1=0,s2=0; 106 for(j=1;j<=n;j++) 107 s1+=a[j][i]; 108 for(j=1;j<=m;j++) 109 s2+=b[j][i]; 110 if(s1>s2) 111 break; 112 } 113 if(i!=(k+1)) 114 { 115 printf("-1\n"); 116 continue; 117 } 118 s=0; 119 for(i=1;i<=k;i++) 120 { 121 memset(cost,0,sizeof(cost)); 122 memset(cap,0,sizeof(cap)); 123 for(j=1;j<=m;j++) 124 cap[0][j]=b[j][i]; 125 for(j=1;j<=m;j++) 126 for(int ds=1;ds<=n;ds++) 127 { 128 cap[j][m+ds]=b[j][i]; 129 cost[j][m+ds]=c[i][ds][j]; 130 cost[m+ds][j]=-1*c[i][ds][j]; 131 } 132 for(j=1;j<=n;j++) 133 cap[m+j][m+n+1]=a[j][i]; 134 src=0; 135 end1=m+n+1; 136 s+=EK(); 137 } 138 printf("%d\n",s); 139 140 } 141 return 0; 142 }
EK