Links: http://bak3.vjudge.net/problem/UVA-1660
Analysis: This blog is very detailed. Http://www.cnblogs.com/xcw0754/p/4662429.html
1#include <cstdio>2#include <queue>3#include <cstring>4#include <vector>5 using namespacestd;6 7 Const intMAXN = -+5;8 Const intINF =0x3f3f3f3f;9 Ten structEdge { One int from, to, cap, flow; AEdge (intUintVintCintf): from(U), to (v), Cap (c), Flow (f) {} - }; - the structEdmondskarp { - intN, M; -Vector<edge>edges; -vector<int>G[MAXN]; + intA[MAXN]; - intP[MAXN]; + A voidInitintN) { at for(inti =0; I < n; i++) g[i].clear (); - edges.clear (); - } - - voidAddedge (int from,intTo,intcap) { -Edges.push_back (Edge ( from, to, Cap,0)); inEdges.push_back (Edge to, from,0,0)); -m =edges.size (); tog[ from].push_back (M-2); +G[to].push_back (M-1); - } the * intMaxflow (intSintt) { $ intFlow =0;Panax Notoginseng for (;;) { -Memset (A,0,sizeof(a)); thequeue<int>Q; + Q.push (s); AA[s] =INF; the while(!Q.empty ()) { + intx =Q.front (); Q.pop (); - for(inti =0; I < g[x].size (); i++) { $edge& e =Edges[g[x][i]]; $ if(!a[e.to] && e.cap >e.flow) { -P[e.to] =G[x][i]; -A[e.to] = min (a[x], E.cap-e.flow); the Q.push (e.to); - }Wuyi } the if(A[t]) Break; - } Wu if(!a[t]) Break; - for(intu = t; U! = S; U = edges[p[u]]. from) { AboutEdges[p[u]].flow + =A[t]; $Edges[p[u] ^1].flow-=A[t]; - } -Flow + =A[t]; - } A returnflow; + } the }; - $ Edmondskarp G; theVector<edge>Bak; the the intMain () { the intN, M; - while(SCANF ("%d%d", &n, &m) = =2) { inG.init (n *2); the for(inti =0; I < n; i++) G.addedge (i, i + N,1); the for(inti =0, u, v; I < m; i++) { Aboutscanf"(%d,%d)", &u, &v); theG.addedge (U +N, v, INF); theG.addedge (v +N, u, INF); the } + ints = N, flow = n; Bak =g.edges; - for(inti =1; I < n; i++) { theG.edges =Bak;BayiFlow =min (Flow, g.maxflow (S, i)); the } theprintf"%d\n", flow); - } - return 0; the}
UVa1660 Cable TV Network (undirected graph, point connectivity, maximum flow)