Poj 2516 Minimum Cost
Link: http://poj.org/problem? Id = 2516
Question:There are m warehouses, N merchants, and K items. Input n, m, K first. Then, enter the number of K in N rows. Each row represents the items to be purchased by a merchant. K numbers represent the number of items to be purchased. Then there is the k Number in m rows. Each row indicates the situation in the warehouse, where k number indicates the inventory of each item. Next, there are K matrices. Each matrix is N * m, indicating the cost for the k-th item to be shipped from M warehouse to the n-th merchant respectively. Ask if it can be reasonably arranged to minimize the cost. If not, output-1.
Ideas:At the beginning, we constructed N * k + M * k points. Later, we found that the maximum minimum cost flow can be performed for each item. Then sum the fee. However, you must first determine whether the conditions can be met. You only need to sum up the demand and supply of each item. If the supply of each item is greater than or equal to the value, you will surely have the answer.
Code:
/* ID: [email protected] prog: Lang: c ++ */# include <map> # include <set> # include <queue> # include <stack> # include <cmath> # include <cstdio> # include <vector> # include <string> # include <fstream> # include <cstring> # include <ctype. h> # include <iostream> # include <algorithm> using namespace STD; # define Inf (1 <30) # define linf (1ll <60) # define PI ACOs (-1.0) # define MEM (a, B) memset (a, B, sizeof (A) # define rep (I, A, n) for (in T I = A; I <n; I ++) # define per (I, A, n) for (INT I = n-1; I> = A; I --) # define EPS 1e-6 # define debug puts ("================ ") # define Pb push_back # define mkp make_pair # define all (x ). begin (), (x ). end () # define Fi first # define se second # define SZ (x) (INT) (x ). size () # define posin (x, y) (0 <= (x) & (x) <n & 0 <= (y) & (y) <m) typedef long ll; typedef unsigned long ull; const int maxn = 5 555; const int maxm = 500000; struct node {int V, Cap, NXT, cost;} e [maxm * 2]; int G [maxn], CNT, St, Ed, n, m; int ans, flow; int n, m, K; int NK [60] [60], MK [60] [60]; void add (int u, int V, int cap, int cost) {e [++ CNT]. V = V; E [CNT]. CAP = CAP; E [CNT]. cost = cost; E [CNT]. NXT = G [u]; G [u] = CNT; E [++ CNT]. V = u; E [CNT]. CAP = 0; E [CNT]. cost =-cost; E [CNT]. NXT = G [v]; G [v] = CNT;} void Init (int K) {CNT = 1; ans = flow = 0; memset (G, 0, sizeof (INT) * (m + n + 10); // edge ST = 0, ed = m + n + 1, n = Ed; For (INT I = 1; I <= m; I ++) add (St, I, MK [I] [K], 0); For (INT I = 1; I <= N; I ++) add (I + M, Ed, NK [I] [K], 0); int C; For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {scanf ("% d", & C); add (J, I + M, INF, c );}}} int dis [maxn], que [maxn], pre [maxn]; bool vis [maxn]; bool spfa () {int font = 0, rear = 1; for (INT I = 0; I <= N; I ++) {dis [I] = inf; vis [I] = false ;} dis [st] = 0; que [0] = sT; vis [st] = true; while (rear! = Font) {int u = que [font ++]; font % = N; vis [u] = false; For (INT I = G [u]; I; I = E [I]. NXT) {int v = E [I]. v; If (E [I]. cap & dis [v]> dis [u] + E [I]. cost) {dis [v] = dis [u] + E [I]. cost; Pre [v] = I; If (! Vis [v]) {vis [v] = true; que [rear ++] = V; Rear % = n ;}}}} if (DIS [ed] = inf) return false; return true;} void augment () {int U, P, MI = inf; For (u = Ed; u! = ST; u = E [P ^ 1]. v) {P = pre [u]; MI = min (MI, E [p]. CAP) ;}for (u = Ed; u! = ST; u = E [P ^ 1]. v) {P = pre [u]; E [p]. cap-= mi; E [P ^ 1]. cap + = mi; ans + = mi * E [p]. cost; // The unit traffic fee recorded by cost must be multiplied by the traffic fee.} Flow + = mi;} int mcmf (int K) {Init (k); While (spfa () Augment (); Return ans;} bool get () {int n_k [110] = {0}, m_k [110] = {0}; int C; rep (I, 1, n + 1) rep (J, 1, k + 1) {scanf ("% d", & C); n_k [J] + = C; NK [I] [J] = C;} rep (I, 1, m + 1) rep (J, 1, k + 1) {scanf ("% d", & C); m_k [J] + = C; MK [I] [J] = C ;}for (INT I = 1; I <= K; I ++) if (n_k [I]> m_k [I]) return false; return true;} int main () {While (~ Scanf ("% d", & N, & M, & K), N | M | K) {If (get ()) {int tot = 0; For (INT I = 1; I <= K; I ++) {tot + = mcmf (I );} printf ("% d \ n", TOT);} else {int C; rep (I, 0, k) rep (J, 0, n) rep (K, 0, m) scanf ("% d", & C); puts ("-1") ;}} return 0 ;}
Poj 2516 minimum cost (maximum minimum cost Stream)