Before it seems that the network flow of the construction of the problem is relatively few ah ... Now let's do a little.
The first is the template.
poj1273 Grassland Drainage
#include <iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<string.h>#include<vector>#include<limits>#include<Set>#include<map>using namespacestd;#defineSZ 233333intn,m=1; typedefLong Longll;intFst[sz],nxt[sz],vb[sz]; ll Cap[sz];voidAD_DL (intAintb,ll c) { ++m; Nxt[m]=fst[a]; Fst[a]=m; Vb[m]=b; cap[m]=C;}voidAddl (intAintb,ll c) {AD_DL (a,b,c); Ad_dl (B,a,0);}intS,t,q[sz],d[sz];BOOLBFs () {memset (d,-1,sizeof(d)); D[s]=0; q[1]=s;intH=1, t=2; while(h!=t) {intcur=q[h++]; for(intE=fst[cur];e;e=Nxt[e]) { intb=Vb[e]; if(d[b]==-1&&cap[e]);Else Continue; D[B]=d[cur]+1; q[t++]=b; } } returnd[t]!=-1;} ll Dfs (intX,ll F) { if(f<=0)return 0; if(x==t)returnF; LL CA=0; for(intE=fst[x];e;e=Nxt[e]) { intb=Vb[e]; if(d[b]!=d[x]+1)Continue; ll W=dfs (B,min (cap[e],f-CA)); Cap[e]-=w; cap[e^1]+=w; ca+=W; if(ca==f) Break; } if(!CA) d[x]=-1; returnCA;}#defineINF 100000000000000000LLll Dinic () {ll ans=0; while(BFS ()) ans+=DFS (S,inf); returnans;}intMain () {intm; while(SCANF ("%d%d", &m,&n)! =EOF) {memset (FST,0,sizeof(FST)); m=1; for(intI=1; i<=m;i++) { intA,b,c; scanf ("%d%d%d",&a,&b,&c); Addl (A,B,C); } S=1; t=N; printf ("%d\n", Dinic ()); }}
poj2135 Farm Tour
#include <iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<string.h>#include<vector>#include<limits>#include<Set>#include<map>using namespacestd;#defineSZ 233333typedefLong Longll;intm=1, S,t,fst[sz],nxt[sz],va[sz],vb[sz];ll Cap[sz],cost[sz];voidAD_DL (intAintB,ll CA,LL Co) { ++m; Nxt[m]=fst[a]; Fst[a]=m; Va[m]=a; Vb[m]=b; Cap[m]=ca; cost[m]=Co;}voidAddl (intAintB,ll Ca,ll Co) {AD_DL (A,b,ca,co); Ad_dl (B,a,0,-co);} ll Dis[sz]; intN,q[sz],fe[sz];BOOLInq[sz];BOOLSPFA () {ll inf=2000000000000000LL; for(intI=1; i<=n;i++) dis[i]=inf; Inq[s]=1; q[1]=s; dis[s]=0;intH=1, t=2; while(h!=t) {intCur=q[h++]; h&=131071; for(intE=fst[cur];e;e=Nxt[e]) { if(!cap[e]| | Dis[vb[e]]<=dis[cur]+cost[e])Continue; intb=vb[e],co=Cost[e]; DIS[B]=dis[cur]+co; fe[b]=e; if(!inq[b]) {inq[b]=1; Q[t++]=b; t&=131071;} } Inq[cur]=0; } returndis[t]!=inf;} ll Mcf () {ll ans=0; while(SPFA ()) {ll cur=2000000000000000LL; for(intI=fe[t];i;i=fe[va[i]]) cur=min (cur,cap[i]); for(intI=fe[t];i;i=Fe[va[i]]) {ans+=cur*Cost[i]; Cap[i]-=cur; cap[i^1]+=cur; } } returnans;}intMain () {intM scanf"%d%d", &n,&m); n+=2; S=1; t=N; Addl (1,2,2,0); Addl (t1T2,0); while(m--) { intA,b,c; scanf ("%d%d%d",&a,&b,&c); Addl (A+1, B +1,1, c); Addl (b+1, A +1,1, c); } printf ("%d\n",(int) MCF ());
Network Flow Example Learning