Record the template
#include <vector>#include <cstring>#include <algorithm>#include <cstdio>#include <queue>using namespace STD;#define MAXN 1100#define INF 0x7f7f7f7fstructEdge {intFrom,to,cap,flow;};structDinic {intN,m,s,t; vector<Edge>Edges vector<int>G[MAXN];BOOLVIS[MAXN];intD[MAXN];intCUR[MAXN];voidAddedge (intFromintTo,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); }voidInit () {edges.clear (); for(intI=1; i<=maxn;i++) g[i].clear (); }BOOLBfs () {memset(Vis,0,sizeof(VIS)); Queue<int>Q; while(! Q.empty ()) Q.pop (); Q.push (s); d[s]=0; Vis[s] =1; while(! Q.empty ()) {intx = Q.front (); Q.pop (); for(intI=0; I<g[x].size (); i++) {edge& e = edges[g[x][i]];if(!vis[e.to]&&e.cap>e.flow) {Vis[e.to] =1; D[e.to] = d[x]+1; Q.push (e.to); } } }returnVIS[T]; }intDfsintXintA) {if(x==t| | a==0)returnAintFlow =0, F; for(int&i=cur[x]; I<g[x].size (); i++) {edge& e = edges[g[x][i]];if(d[x]+1==d[e.to]&& (F=dfs (E.to,min (a,e.cap-e.flow)) >0) {E.flow + = f; edges[g[x][i]^1].flow-= f; Flow + + F; A-= f;if(a==0) Break; } }returnFlow }intMaxflow (intSintT) { This->s=s; This->t=t;intFlow =0; while(Bfs ()) {memset(cur,0,sizeof(cur)); Flow+=dfs (S,inf); }returnFlow }};intMain () {intTintN,k; Dinic Slove; while(scanf("%d", &t) = =1){intcas=0; while(t--) {scanf("%d%d", &n,&k); Slove.init (); while(k--) {intX, Y, Zscanf("%d%d%d", &x,&y,&z); Slove.addedge (x, y, z); }printf("Case%d:%d\n", ++cas,slove.maxflow (1, n)); } }}
Copyright notice: All brothers, please feel free to reprint, please indicate who is the brother
HDU 3549 Flow problem (Network flow template problem)