POJ 2516 Minimum Cost
Question link
QUESTION: Let's turn to a question .. I feel very detailed written, thank YoU http://blog.csdn.net/lyy289065406/article/details/6742534
Idea: In the beginning, every item of all sellers and all items of all suppliers were connected and the cost flow was reached. The result was TLE. Because of this, the number of edges would be very large.
In fact, there is no mutual influence between each item. After finding out each item, the answer is accumulated. Then pay attention to the data to be read, directly judge whether the data is read.
Code:
# Include <cstdio> # include <cstring> # include <vector> # include <queue> # include <algorithm> using namespace std; const int MAXNODE = 5005; const int MAXEDGE = 500005; typedef int Type; const Type INF = 0x3f3f3f; struct Edge {int u, v; Type cap, flow, cost; Edge () {} Edge (int u, int v, Type cap, Type flow, Type cost) {this-> u = u; this-> v = v; this-> cap = cap; this-> flow = flow; this-> cost = cost ;}}; struct MCFC { Int n, m, s, t; Edge edges [MAXEDGE]; int first [MAXNODE]; int next [MAXEDGE]; int inq [MAXNODE]; Type d [MAXNODE]; int p [MAXNODE]; Type a [MAXNODE]; void init (int n) {this-> n = n; memset (first,-1, sizeof (first )); m = 0;} void add_Edge (int u, int v, Type cap, Type cost) {edges [m] = Edge (u, v, cap, 0, cost ); next [m] = first [u]; first [u] = m ++; edges [m] = Edge (v, u, 0, 0,-cost ); next [m] = first [v]; first [v] = m ++;} bool Bellmanford (int s, int t, Type & flow, Type & cost) {for (int I = 0; I <n; I ++) d [I] = INF; memset (inq, false, sizeof (inq); d [s] = 0; inq [s] = true; p [s] = s; a [s] = INF; queue <int> Q; Q. push (s); while (! Q. empty () {int u = Q. front (); Q. pop (); inq [u] = false; for (int I = first [u]; I! =-1; I = next [I]) {Edge & e = edges [I]; if (e. cap> e. flow & d [e. v]> d [u] + e. cost) {d [e. v] = d [u] + e. cost; p [e. v] = I; a [e. v] = min (a [u], e. cap-e. flow); if (! Inq [e. v]) {Q. push (e. v); inq [e. v] = true ;}}}if (d [t] = INF) return false; flow + = a [t]; cost + = d [t] * a [t]; int u = t; while (u! = S) {edges [p [u]. flow + = a [t]; edges [p [u] ^ 1]. flow-= a [t]; u = edges [p [u]. u;} return true;} Type Mincost (int s, int t, Type sum) {Type flow = 0, cost = 0; while (bellmanford (s, t, flow, cost); if (sum! = Flow) cost =-1; return cost ;}} gao; const int N = 55; int n, m, k, sum; int a [N] [N], B [N] [N]; int solve () {int c, ans = 0, flag = 0; for (int I = 0; I <k; I ++) {gao. init (n + m + 2); for (int j = 1; j <= m; j ++) gao. add_Edge (0, j, B [j] [I], 0); int sum = 0; for (int j = 1; j <= n; j ++) {sum + = a [j] [I]; gao. add_Edge (m + j, n + m + 1, a [j] [I], 0) ;}for (int x = 1; x <= n; x ++) {for (int y = 1; y <= m; y ++) {SC Anf ("% d", & c); gao. add_Edge (y, x + m, INF, c) ;}} if (flag) continue; int tmp = gao. mincost (0, n + m + 1, sum); if (tmp =-1) flag = 1; else ans + = tmp;} if (flag) ans =-1; return ans;} int main () {while (~ Scanf ("% d", & n, & m, & k) & n) {for (int I = 1; I <= n; I ++) for (int j = 0; j <k; j ++) scanf ("% d", & a [I] [j]); for (int I = 1; I <= m; I ++) for (int j = 0; j <k; j ++) scanf ("% d ", & B [I] [j]); printf ("% d \ n", solve ();} return 0 ;}
POJ 2516 Minimum Cost (billing flow)