Question Link
Poj3422
It seems that the network stream is getting amazing.
1. Minimum fee stream-conversion between the maximum fee streams
2. Split points
3. cost or traffic (cost, flow)
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <cmath> # include <queue> # include <stack> # include <map> # include <set> using namespace STD; # define CLR (x) memset (x, 0, sizeof (x) # define FP1 freopen ("in.txt", "r", stdin) # define fp2 freopen ("out.txt", "W", stdout) # define Pb push_back # define INF 0x3c3c3ctypedef long ll; // The network can have a negative edge, however, there cannot be a negative weight circle. // Call Init () First each time, such as the initial maxn and INF. // Addedge (Cap, flow): Cap indicates capacity and flow indicates traffic. Const int maxn = 100000; struct edge {int from, to, Cap, flow, cost;}; struct mcmf {int n, m, S, T; vector <edge> edges; vector <int> G [maxn]; int INQ [maxn]; // whether int d [maxn] is in the queue; // Bellman-Ford int P [maxn]; // The Last arc int A [maxn]; // void Init (int n) that can be improved // It indicates which init should be called for initialization every time {This-> N = N; for (INT I = 0; I <n; I ++) g [I]. clear (); edges. clear (); // CLR (p); CLR (a); CLR (d);} void addedge (int from, int to, int Cost, int cap) {edges. pb (edge) {from, to, Cap, 0, cost}); edges. pb (edge) {to, from, 0, 0,-cost}); M = edges. size (); G [from]. pb (m-2); G [to]. PB m-1);} // other member functions bool spfa (int s, int T, Int & flow, Int & cost) {for (INT I = 0; I <N; I ++) d [I] = inf; CLR (INQ); D [s] = 0; INQ [s] = 1; p [s] = 0; A [s] = inf; queue <int> q; q. push (s); While (! Q. empty () {int u = Q. front (); q. pop (); INQ [u] = 0; For (INT I = 0; I <G [u]. size (); I ++) {edge & E = edges [G [u] [I]; If (E. cap> E. flow & D [E. to]> d [u] + E. cost) {d [E. to] = d [u] + E. cost; P [E. to] = G [u] [I]; A [E. to] = min (A [u], E. cap-e.flow); If (! INQ [E. to]) {q. push (E. to); INQ [E. to] = 1 ;}}}if (d [T] = inf) return false; // s-t is not connected, and the flow + = A [T] fails to exit; 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]. from;} return true;} int mincost (int s, int t) {int flow = 0, cost = 0; while (spfa (S, T, flow, cost )); return cost ;}}; mcmf flow; int map1 [55] [55]; int main () {// freopen ("in.txt", "r", stdin ); int n, m; while (scanf ("% d", & N, & M) = 2) {flow. init (N * n * 2 + 100); int I, j; For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {Scanf ("% d", & map1 [I] [J]) ;}}for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {int u = (I-1) * n + J; flow. addedge (U, N * n + u,-map1 [I] [J], 1); // reversed flow. addedge (U, N * n + u, 0 m-1); If (J! = N) {flow. addedge (u, u + 1, 0, m); flow. addedge (N * n + u, u + 1, 0, m);} if (I! = N) {flow. addedge (u, u + N, 0, m); flow. addedge (N * n + u, u + N, 0, m) ;}} flow. addedge (0, 1, 0, m); // supersourceflow. addedge (N * n * 2, N * n * 2 + 1, 0, m); // supersink cout <-flow. mincost (0, N * n * 2 + 1);} return 0 ;}
A fee flow from poj3422