Analysis is here
Nest to the original method is also written well, anyway is similar ....
In fact.... It also wrote the highest-grade pre-flow propulsion ......... .............. It's such a pain in the egg .... However, on this topic, the three methods are all the same ...
Code
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include < vector> #include <string> #include <queue> #include <map> #include <set> #include <cmath > #include <cstdlib>using namespace std, #define INF 0x3f3f3f3f#define PI acos ( -1.0) #define MEM (A, B) memset (A, B, sizeof (a)) typedef pair<int,int> PII;TYPEDEF Long Long ll;//------------------------------const int MAXN = 205;int n,m;struct edge{int to, CAP, rev;//end capacity reverse edge edge (int to_ = 0, int cap_ = 0, int rev_ = 0) {to = To_ ; Cap = CAP_; Rev = rev_; }};vector<edge> g[maxn];int level[maxn];//Vertex to source point distance designator int iter[maxn];void Add_edge (int from, int. to, int cap) {Ed GE Tmp1 (To, Cap, g[to].size ()); G[from].push_back (TMP1); Edge tmp2 (from, 0, G[from].size ()-1); G[to].push_back (TMP2);} void BFs (int s) {memset (level,-1, sizeof); Queue<int> Q; Level[s] = 0; Q.push (s); while (!q.empty ()) {int v = q.front (); Q.pop (); for (int i = 0; i < g[v].size (); i++) {edge& e = g[v][i]; if (E.cap > 0 && level[e.to] < 0) {level[e.to] = Level[v] + 1; Q.push (e.to); }}}}int dfs (int v, int t, int f) {if (v = = t) return F; for (int& i = iter[v]; i < g[v].size (); i++) {edge& e = g[v][i]; if (E.cap > 0 && level[v] < level[e.to]) {int d = DFS (e.to, T, Min (E.cap, f)); if (d > 0) {e.cap-= D; G[e.to][e.rev].cap + = D; return D; }}} return 0;} int Max_flow (int s, int t) {int flow = 0; for (;;) {BFS (s); if (Level[t] < 0) return flow; memset (ITER, 0, sizeof (ITER)); int F; while ((f = DFS (S, T, INF)) > 0) {flow + F; }}}//------------------above is the maximum flow template part correctly----------------------int NP, nc;void init () {for (int i = 0; i < MAXN; I + +) G[i].clear (); int U, V, W; Char ch; for (int i = 1; I <= m; i++) {while (scanf ("%c", &ch)) {if (ch = = ' (') break; } scanf ("%d,%d%c%d", &u,&v,&ch,&w); Add_edge (U, V, W); } for (int i = 1; I <= np; i++) {while (scanf ("%c", &ch)) {if (ch = = ' (') break; } scanf ("%d%c%d", &u, &ch, &w); Add_edge (N, u, W); } for (int i = 1; I <= NC; i++) {while (scanf ("%c", &ch)) {if (ch = = ' (') break; } scanf ("%d%c%d", &v, &ch, &w); Add_edge (V, N+1, W); } int ans = Max_flow (n, n+1); printf ("%d\n", ans);} int main () {while(scanf ("%d%d%d%d", &N,&NP, &NC, &m)! = EOF) {init (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ more than 1459 Yuanhui maximum flow