structedge{int from, To,cap,flow,cost;};structmcmf{intn,m,s,t; Vector<Edge>edges; Vector<int>G[MAXN]; intINQ[MAXN]; intD[MAXN]; intP[MAXN]; intA[MAXN]; voidInitintN) { This->n=N; REP (i,1, N) G[i].clear (); Edges.clear (); } voidAddedge (int from,intTo,intCapintCost ) {Edges.push_back (Edge) { from, To,cap,0, cost}); Edges.push_back (Edge) {to, from,0,0,-Cost }); M=edges.size (); g[ from].push_back (M-2); G[to].push_back (M-1); } BOOLBellman_ford (intSintTint&flow,int&Cost ) {REP (i,1, N) d[i]=INF; MS0 (INQ); D[s]=0; inq[s]=1;p [s]=0; a[s]=INF; Queue<int>Q; Q.push (s); while(!Q.empty ()) { intu=Q.front (); Q.pop (); Inq[u]=0; for(intI=0; I<g[u].size (); i++) {Edge& e=Edges[g[u][i]]; Inq[u]=0; 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 0; Flow+=A[t]; Cost+=d[t]*A[t]; intu=T; while(u!=s) {Edges[p[u]].flow+=A[t]; Edges[p[u]^1].flow-=A[t]; U=edges[p[u]]. from; } return 1; } intMincost (intSintt) {intflow=0, cost=0; while(Bellman_ford (s,t,flow,cost)); returnCost ; }}; MCMF F;
View Code
Minimum cost Max Stream Template (training guide)