Http://imlazy.ycool.com/post.2059102.html
I read this article and learned a lot about it. I worship God!
The difficulty of network flow lies in the Construction of graphs and the simplification of graphs.
// Network stream -- maximum stream <br/> // The Key to this question is the simplification of the diagram and the merge of the flow <br/> // see the http://imlazy.ycool.com/post.2059102.html <br/>/ /very detailed explanation <br/> # include <iostream> <br/> # include <queue> <br/> # include <cstring> <br/> # define INF 2000000000 <br/> using namespace STD; <br/> int CAP [105] [105], flow [105] [105]; <br/> int rflow [105], pre [105]; <br/> int m, n, x, y, U, V, C, maxflow, St, Ed, E; <br/> int pign [1005]; <br/> int connect [1005]; <br/> bool vis [100 5]; <br/> int main () <br/> {<br/> // freopen ("in.txt", "r", stdin ); <br/> scanf ("% d", & N, & M); <br/> memset (Cap, 0, sizeof (CAP )); <br/> memset (flow, 0, sizeof (flow); <br/> memset (VIS, 0, sizeof (VIS); <br/> memset (connect, 0, sizeof (CONNECT); <br/> ST = m + 1; <br/> ED = m + 2; <br/> for (INT I = 1; I <= N; ++ I) <br/> {<br/> scanf ("% d", & pign [I]); <br/>}< br/> for (x = 1; x <= m; ++ X) // composition is very important <br/>{< br/> scanf ("% d", & E); <br/> While (E --) <br/>{< br/> scanf ("% d", & Y); <br/> If (! Vis [y]) <br/>{< br/> CAP [st] [x] + = pign [y]; // merge traffic <br/> vis [y] = 1; <br/> connect [y] = X; <br/>}< br/> else <br/> {<br/> CAP [connect [y] [x] = inf; <br/>}< br/> scanf ("% d", & C); <br/> CAP [x] [ed] = C; <br/>}< br/> for (;) // maximum stream template <br/>{< br/> queue <int> q; <br/> memset (rflow, 0, sizeof (rflow); <br/> memset (PRE, 0, sizeof (pre )); <br/> rflow [st] = inf; <br/> q. push (ST); <br/> while (! Q. empty () <br/>{< br/> U = Q. front (); <br/> q. pop (); <br/> for (int v = 0; v <= Ed; ++ v) <br/>{< br/> If (! Rflow [v] & Cap [u] [v]> flow [u] [v]) <br/> {<br/> pre [v] = u; <br/> rflow [v] = min (rflow [u], Cap [u] [v]-flow [u] [v]); <br/> q. push (V); <br/>}< br/> If (rflow [ed] = 0) break; <br/> for (u = Ed; u! = ST; u = pre [u]) <br/>{< br/> flow [pre [u] [u] + = rflow [ed]; <br/> flow [u] [pre [u]-= rflow [ed]; <br/>}< br/> maxflow ++ = rflow [ed]; <br/>}< br/> printf ("% d/N", maxflow); <br/> return 0; <br/>}