Minimum Cost
Recently, I was looking at the maximum traffic with the minimum cost. I found a question and practiced it, which is relatively simple.
It is clear that there are n shopkeepers and M suppliers, and there are K types of goods. Each shopkeepers have different requirements for each product, and each supplier has different inventory for each product, each type of product has different suppliers for different shopkeepers, and the prices are different. They have already been given, and the minimum cost is required to satisfy all shopkeepers. Because each product does not interfere with each other, you just need to find the maximum minimum cost for each product. Create a Source Vertex S = 0, sink vertex t = n + m + 1
There are three types of edges:
1. s to the supplier's edge supplier number 1 to m, the cost is 0, the capacity is the quantity of inventory
2. The number m + 1 to m + n from the supplier to the shopkeeper. The cost is read, and the capacity is INF.
3. The number of bytes from the shopkeeper to T is 0, and the capacity is the number required for reading.
# Include <stdio. h> # define INF 0 xfffffffint n, m, K, S, T; int cost [54] [54] [54]; // cost [k] [I] [J] The cost int have [54] [54] required for the k-th commodity to be sent from the J supplier to the I-th shopkeeper; // have [I] [k] Number of products owned by the I supplier int need [54] [54]; // need [I] [k] Number of products required by the I-th shopkeeper int B [104] [104]; // The distance between B [I] [J] node I and node J (unit cost of commodity transportation) int f [104] [104]; // F [I] [J] remaining capacity of the edge <I, j> int dis [104]; // dis [I] minimum distance from saving I node to s of source point int mark [104]; // mark array int stack when finding the shortest path [104]; // use Stack + spfa to implement the shortest short-circuit int pre [104]; // record the Shortest Path int min (int A, int B) {return a <B? A: B;} int spfa () // calculate the shortest path {int I, U, V, top; for (I = s; I <= T; I ++) {dis [I] = inf; Mark [I] = 0;} mark [s] = 1; DIS [s] = 0; Top = 0; stack [++ top] = s; while (top) {u = stack [top --]; Mark [u] = 0; For (V = 0; v <= N + m + 1; V ++) {If (F [u] [v] & dis [v]> dis [u] + B [u] [v]) {dis [v] = dis [u] + B [u] [v]; Pre [v] = u; // each update needs to change the direction of the forward node, whether or not you want to import the stack if (! Mark [v]) {stack [++ top] = V; Mark [v] = 1 ;}}} return dis [T] ;} int mincostmaxflow () {int I, T, minf, C; int ans = 0; int maxflow = 0; while (C = spfa ())! = Inf) {minf = inf; t = T; while (T! = S) {minf = min (minf, F [pre [T] [T]); t = pre [T];} maxflow ++ = minf; t = T; while (T! = S) {f [pre [T] [T]-= minf; F [T] [pre [T] + = minf; B [T] [pre [T] =-B [pre [T] [T]; t = pre [T];} ans + = C * minf ;} for (I = 1; I <= N; I ++) {If (F [I + M] [T]) break;} return (I <= n? 0: ANS);} void read () // read data {int I, j, k; for (I = 1; I <= N; I ++) {for (j = 1; j <= K; j ++) {scanf ("% d", & need [I] [J]) ;}} for (I = 1; I <= m; I ++) {for (j = 1; j <= K; j ++) {scanf ("% d ", & have [I] [J]) ;}}for (k = 1; k <= K; k ++) {for (I = 1; I <= N; I ++) {for (j = 1; j <= m; j ++) {scanf ("% d ", & cost [k] [I] [J]) ;}}} void creatgraph (int K) // create a graph {int I, j; for (I = s; I <= T; I ++) {for (j = s; j <= T; j ++) {B [I] [J] = INF; F [I] [J] = 0 ;}} for (I = 1; I <= m; I ++) {B [s] [I] = 0; // B [I] [s] = inf; F [s] [I] = have [I] [k]; // F [I] [s] = 0; for (j = 1; j <= N; j ++) {B [I] [J + M] = cost [k] [J] [I]; // B [J + M] [I] = inf; F [I] [J + M] = inf; // F [J + M] [I] = 0 ;}} for (I = 1; I <= N; I ++) {B [I + M] [T] = 0; // B [T] [I + M] = inf; f [I + M] [T] = need [I] [k]; // F [T] [I + M] = 0 ;}} int main () {int K, T, ans; while (scanf ("% d", & N, & M, & K )! = EOF) {If (n = 0 & M = 0 & K = 0) break; read (); s = 0; // Source Vertex T = m + n + 1; // sink vertex ans = 0; For (k = 1; k <= K; k ++) {creatgraph (k ); T = mincostmaxflow (); If (! T) break; ans + = T;} If (k <= k) puts ("-1"); else printf ("% d \ n", ANS );} return 0 ;}