Question link: http://poj.org/problem? Id = 2516
There are n stores, M suppliers, and K types of goods. It is known that the number of each item in the supplier's warehouse and the cost for each item to be shipped to each store. Each store requires the minimum charge for each item.
Input
The first line is n, m, K.
Then there are 1-N rows and K columns in each row. The number of J items in row I represents the number of items required for shop I.
Then, in the n + 1-m rows, each row has K columns, and the number of J items in row I represents the number of items in type J of supplier I.
Then there are K matrices, and each n rows of M columns, column J of line I of the Tianji matrix represents the cost required by the supplier J to issue goods of the Tianji type to the shop I. Alas, my day
I feel that I am not examining network streams, but the debugging capability of tedious data input. I am confused...
Ideas:At the beginning, the idea was wrong. When we went up, we split up the items, matched the suppliers and stores with K types of products, split each supplier into K, split the stores into k, and half of them felt wrong, 50*50 + 50*50 + 50.
Since the splitting fails, you can split the network... Split the entire network of all commodities into a network of K single commodities, find the minimum cost of a single commodity, and then accumulate
Graph creation:
Add two points, source point S = 0, sink point t = n + m + 1, source point connected to supplier, cost 0, capacity is the supply of the current single product
The cost of connecting the supplier to the store is known, and the capacity is infinite.
The store is connected to the settlement point at a cost of 0, and the capacity is the current demand for a single item.
# Include <iostream> # include <cstdlib> # include <cstdio> # include <cstring> # include <queue> # include <algorithm> const int maxn = 110; const int maxm = 10000; const int INF = 1e8; # define min int_min # define Max 1e6 # define ll long # define Init (a) memset (A, 0, sizeof (A) # define for (I, a, B) for (INT I = A; I <B; I ++) # define max (A, B) (A> B )? (A) :( B) # define min (A, B) (A> B )? (B) :( A) using namespace STD; struct node {int U, V, W, Cap, next;} edge [maxm]; int pre [maxn], dis [maxn], head [maxn], CNT; bool vis [maxn]; int n, m; void add (int u, int V, int C, int cap) {edge [CNT]. U = u; edge [CNT]. V = V; edge [CNT]. W = C; edge [CNT]. CAP = CAP; edge [CNT]. next = head [u]; head [u] = CNT ++; edge [CNT]. U = V; edge [CNT]. V = u; edge [CNT]. W =-C; edge [CNT]. CAP = 0; edge [CNT]. next = head [v]; head [v] = CNT ++;} int spfa (int s, int t) {que UE <int> q; while (Q. empty () = false) Q. pop (); q. push (s); memset (VIS, 0, sizeof (VIS); memset (PRE,-1, sizeof (pre); for (I, s, t + 1) dis [I] = inf; DIS [s] = 0; while (! Q. empty () {int u = Q. front (); q. pop (); vis [u] = 0; For (INT I = head [u]; I! =-1; I = edge [I]. next) {If (edge [I]. cap & dis [edge [I]. v]> dis [u] + edge [I]. w) {dis [edge [I]. v] = dis [u] + edge [I]. w; Pre [edge [I]. v] = I; If (! Vis [edge [I]. v]) {vis [edge [I]. v] = 1; q. push (edge [I]. v) ;}}} if (DIS [T]! = Inf) return 1; else return 0;} int mincostmaxflow (int s, int t) {int flow = 0, cost = 0; while (spfa (S, T )) {int df = inf; For (INT I = pre [T]; I! =-1; I = pre [edge [I]. u]) {If (edge [I]. cap <DF) df = edge [I]. CAP;} flow + = DF; For (INT I = pre [T]; I! =-1; I = pre [edge [I]. u]) {edge [I]. cap-= DF; edge [I ^ 1]. cap + = DF;} cost + = dis [T] * DF;} return cost;} void initt () {CNT = 0; memset (Head,-1, sizeof (head);} int main () {int Gong [51], Qiu [51]; int K, S, T; int need [200] [200], gei [200] [200]; while (scanf ("% d", & N, & M, & K), n, m, K) {Init (need); Init (GEI); Init (GONG); Init (Qiu); s = 0, T = N + m + 1; for (I, 1, n + 1) {for (J, 1, k + 1) {scanf ("% d", & need [I] [J]); Qiu [J] + = need [I] [J] ;}}for (I, 1, m + 1) {for (J, 1, k + 1) {scanf ("% d ", & gei [I] [J]); Gong [J] + = gei [I] [J] ;}} bool flag = 1; for (I, 1, k + 1) {If (Gong [I] <Qiu [I]) // if the supply of a single product does not meet the demand of the product, print-1 {flag = 0; break ;}} int cost, Yao, money = 0; For (JI, 1, k + 1) {initt (); for (I, 1, n + 1) {for (J, 1, m + 1) {scanf ("% d", & cost); If (flag = 0) continue; add (J, M + I, cost, INF); // the traffic of suppliers and shopkeepers is positive infinity} If (flag = 0) continue; for (I, 1, n + 1) {Add (m + I, T, 0, need [I] [JI]); // The cost of the store and settlement point is 0, and the traffic is the store's demand for this product} for (I, 1, m + 1) {Add (S, I, 0, gei [I] [JI]); // The Source site charges the supplier 0, the traffic is the supplier's supply of this product} money + = mincostmaxflow (S, T);} (flag = 0 )? Puts ("-1"): printf ("% d \ n", money);} return 0 ;}