Topic links: http://poj.org/problem?id=2516
Minimum Cost
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions:19928 | Accepted: 7064 |
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.
Subject to the effect: n businessman, m warehouse, each warehouse, there are k kinds of goods. Given the needs of each merchant for goods, given the amount of each warehouse to store goods, giving each warehouse to each merchant each unit of goods spending. Seek minimum cost while meeting the needs of businessmen.
Ideas:
1. This question is entered it is made uncomfortable, although the last was written out. I do not want to explain to explain the code inside.
2. The key is that
each cargo is independent, there is no association,
it is possible to carry out separately for each item MCMF (). Final results will be superimposed.
3. The composition is easy to think, for the current cargo warehouse connected to each source point edge with a capacity of warehouse storage capacity of the cargo, the cost is zero. Warehouse to each merchant even side, capacity inf, to spend for the kinds of goods spent the businessman units. Each businessmen even to edge meeting point for businessmen capacity demand, the cost is zero.
code show as below:
. 1 #include <stdio.h> 2 #include < String .h> . 3 #include <Queue> . 4 #include <algorithm> . 5 #define MEM (A, B) Memset (A, B, the sizeof (A)) . 6 const int MAXN = 300 ; . 7 const int INF = 0x3f3f3f3f ; . 8 the using namespace STD; . 9 10 int n-, m, k; // n-m merchant warehouses with k respectively each warehouse goods . 11 int NUM [MAXN] [MAXN ]; // represents the i j th individual needs of goods number 12 intc_num [MAXN] [MAXN]; // represents the number of i-th j th warehouse storing goods 13 is int VIS [ 2 * MAXN], DIS [ 2 * MAXN], Flow [ 2 * MAXN]; 14 int pre [ 2 * MAXN], Last [ 2 * MAXN], minCost, MaxFlow; 15 Queue < int > Q; 16 . 17 struct Edge 18 is { . 19 int to, Next, Flow, DIS; 20 is } Edge [ 2 * MAXN + MAXN * MAXN] ; 21 is int head [MAXN], CNT; 22 is 23 void add(int a, int b, int c, int d) 24 { 25 cnt ++; 26 edge[cnt].to = b; 27 edge[cnt].flow = c; 28 edge[cnt].dis = d; 29 edge[cnt].next = head[a]; 30 head[a] = cnt; 31 } 32 33 bool spfa(int st, int ed) 34 { 35 mem(dis, inf), mem(flow, inf), mem(vis, 0); 36 Q.push(st); 37 vis[st] = 1; 38 dis[st] = 0; 39 pre[ed] = -1; 40 while(!Q.empty()) 41 { 42 int now = Q.front(); 43 Q.pop(); 44 vis[now] = 0; 45 for(int i = head[now]; i != -1; i = edge[i].next) 46 { 47 int to = edge[i].to; 48 if(edge[i].flow > 0 && dis[to] > dis[now] + edge[i].dis) 49 { 50 dis[to] = dis[now] + edge[i].dis; 51 pre[to] = now; 52 last[to] = i; 53 flow[to] = min(flow[now], edge[i].flow); 54 if(!vis[to]) 55 { 56 vis[to] = 1; 57 Q.push(to); 58 } 59 } 60 } 61 } 62 return pre[ed] != -1; 63 } 64 65 void MCMF() 66 { 67 int st = 0, ed = m + n + 1; 68 maxflow = 0, mincost = 0; 69 while(spfa(st, ed)) 70 { 71 int now = ed; 72 maxflow += flow[ed]; 73 mincost += flow[ed] * dis[ed]; 74 while(now != st) 75 { 76 edge[last[now]].flow -= flow[ed]; 77 edge[last[now] ^ 1].flow += flow[ed]; 78 now = pre[now]; 79 } 80 } 81 } 82 83 int main() 84 { 85 while(scanf("%d%d%d"!, & N-, & m, & K) = the EOF) 86 { 87 int In Flag = . 1 ; // number of items stored in the warehouse is sufficient 88 IF (n-== 0 && m == 0 && K == 0 ) 89 BREAK ; 90 for ( int I = . 1 ; I <= n-; I ++ ) 91 is for ( int J = . 1 ; J <= K; J ++ ) 92 Scanf ( " % D " , & NUM [I] [J ]); 93 for( Int I = . 1 ; I <= m; I ++ ) 94 for ( int J = . 1 ; J <= K; J ++ ) 95 Scanf ( " % D " , & [I] c_num [J]); 96 for ( int I = . 1 ; I <= K; I ++) // Laid enumerate each determination goods 97 { 98 int X = 0 , Y = 0 ; 99 for ( int J = . 1 ; J <= n- ; J ++) // enum people 100 + = X NUM [J] [I]; 101 for ( int J = . 1 ; J <= m; J ++) // Enumeration warehouse 102 Y + = c_num [J] [I]; 103 IF (X> Y) 104 in Flag = 0 ; 105 } 106 int ANS = 0 ; 107 for ( int K = . 1 ; K <= K; K ++) // K matrix where each enumeration cost item 108 { 109 CNT = - . 1 , MEM (head, -. 1 ); 110 for ( int I = . 1 ; I <= m; I ++) // source built into the sides of the warehouse 111 { 112 the Add ( 0 , I, c_num [I] [K], 0 ); // super source 0 113 the Add (I, 0 , 0 , 0 ); 114 } 115 for ( int I = . 1 ; I <= n-; I ++) // warehouse built to represent the human side of the warehouse at which article the unit cost person 1 16 { 117 for ( int j = 1; j <= m; j ++) 118 { 119 int x; 120 scanf("%d", &x); 121 if(flag == 0) 122 continue; 123 add(j, m + i, inf, x); 124 add(m + i, j, 0, -x); 125 } 126 } 127 for(int i = 1; i <= n; i ++)//People to super sink m + n + 1 building side 128 { 129 the Add (m + I, m + n-+ . 1 , NUM [I] [K], 0 ); 130. the Add (m + n-+ . 1 , m + I , 0 , 0 ); 131 is } 132 MCMF (); 133 ANS + = minCost; 134 } 135 IF (In Flag) 136 the printf ( " % D \ n- " , ANS); 137 the else 138 the printf ( " -1 \ n- "); 139 } 140 return 0; 141 }