#include <cstdio>#include<iostream>#include<vector>#include<queue>#include<algorithm>using namespacestd;#defineMin (x, y) (x) < (y)? (x): (y)Const intMAXN = -;Const intINF =0x3f3f3f3f;structedge{int from, to, cap, flow; Edge (intUintVintCintf): from(U), to (v), Cap (c), Flow (f) {}};structedmondskarp{intn,m; Vector<Edge>edges; Vector<int>G[MAXN]; intA[MAXN]; intP[MAXN]; voidInitintN) { for(inti =0; I < n; i++) g[i].clear (); Edges.clear (); } voidAddedge (int from,intTo,intcap) {Edges.push_back (Edge ( from, to, Cap,0 ) ); Edges.push_back (Edge to, from,0,0 ) ); M=edges.size (); g[ from].push_back (M-2 ); G[to].push_back (M-1 ); } intMaxflow (intS,intt) { intFlow =0; for( ; ; ) {memset (A,0,sizeof(a)); Queue<int>Q; Q.push (s); A[s]=INF; 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); Q.push (e.to); } } if(A[t]) Break; } if(!a[t]) Break; for(intu = t; U! = S; U = edges[P[u]]. from) {Edges[p[u]].flow+=A[t]; Edges[p[u]^1].flow-=A[t]; } Flow+=A[t]; } returnflow; }};intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin);#endifEdmondskarp graph; intm, N; int from, to, Cap; while(~SCANF ("%d%d", &m, &N)) {graph.init (GRAPH.N); for(inti =0; I < n; i++) {scanf ("%d%d%d",& from, &to, &cap); Graph. Addedge ( from, to, CAP); } cout<<"Max Flow:"<< graph. Maxflow (0, M-1) <<Endl; } return 0;}
Maximum network flow