http://hihocoder.com/problemset/problem/1369?sid=1328132
Reference 73195243
51908303
"AC1"
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>#include<queue>#include<map>using namespaceStd;typedefLong Longll;intn,m;Const intmaxn=5e2+2;Const intmaxm=2e4+2;Const intinf=0x3f3f3f3f;intMaxflow;structedge{intto ; intNXT; intW;} e[2*MAXM];intHEAD[MAXN];inttot;intFA[MAXN];intMP[MAXN][MAXN];BOOLVIS[MAXN];voidinit () {memset (MP,-1,sizeof(MP)); memset (Head,-1,sizeof(head)); Tot=0; Maxflow=0;}voidAddintUintv) {e[tot].to=v; E[TOT].NXT=Head[u]; Head[u]=tot++;}BOOLBFsintSintt) {memset (Vis,false,sizeof(VIS)); memset (FA,-1,sizeof(FA)); Queue<int>Q; Q.push (s); while(!Q.empty ()) { intu=Q.front (); Q.pop (); if(u==t)return true; if(Vis[u])Continue; Vis[u]=true; for(inti=head[u];i!=-1; i=e[i].nxt) { intv=e[i].to; if(!vis[v]&&Mp[u][v]) {Fa[v]=u; Q.push (v); } } } return false;}intMax_flow (intSintt) { intflow=0;//int cnt=0; while(BFS (s,t)) {//for (int i=1;i<=n;i++) {//for (int j=1;j<=n;j++) {//cout<<mp[i][j]<< "";// }//cout<<endl;// }//cout<< "**********************" <<endl; //cnt++; intu=T; intDelta=inf; while(fa[u]!=-1){ //cout<<u<< "";Delta=min (delta,mp[fa[u]][u]); U=Fa[u]; } //cout<<endl; //cout<<delta<<endl;flow+=Delta; U=T; while(fa[u]!=-1) {Mp[fa[u]][u]-=Delta; Mp[u][fa[u]]+=Delta; U=Fa[u]; } } returnflow;}intMain () { while(~SCANF ("%d%d",&n,&m)) {init (); intU,v,c; for(intI=1; i<=m;i++) {scanf ("%d%d%d",&u,&v,&c); if(mp[u][v]==-1) {Add (u,v); Add (V,u); MP[U][V]=C; Mp[v][u]=0; }Else{Mp[u][v]+=C; } } intAns=max_flow (1, N); printf ("%d\n", ans); } return 0;}
"AC2"
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>#include<queue>#include<map>using namespaceStd;typedefLong Longll;intn,m;Const intmaxn=5e2+2;Const intmaxm=2e4+2;Const intinf=0x3f3f3f3f;structedge{intto ; intNXT; intW;} E[MAXM<<1];intHEAD[MAXN];structnode{intx; inte;} FA[MAXN]; BOOLVIS[MAXN];inttot;voidinit () {memset (head,-1,sizeof(head)); Tot=0;}voidAddintUintVintc) {e[tot].to=v; E[TOT].W=C; E[TOT].NXT=Head[u]; Head[u]=tot++;}BOOLBFsintSintt) {memset (Vis,false,sizeof(VIS)); Queue<int>Q; Q.push (s); Vis[s]=true; while(!Q.empty ()) { intu=Q.front (); Q.pop (); if(u==t)return true; for(inti=head[u];i!=-1; i=e[i].nxt) { intv=e[i].to; intw=E[I].W; if(!vis[v]&&W) {Q.push (v); VIS[V]=true; fa[v].x=u; FA[V].E=i; } } } return false;}intWork () {ints=1, t=N; for(intI=1; i<=n;i++) {FA[I].E=-1; fa[i].x=-1; } intans=0; while(BFS (s,t)) {intDelta=inf; intu=T; while(fa[u].x!=-1) {Delta=min (DELTA,E[FA[U].E].W); U=fa[u].x; } ans+=Delta; U=T; while(fa[u].x!=-1){ intI=FA[U].E; E[I].W-=Delta; E[i^1].w+=Delta; U=fa[u].x; } for(intI=1; i<=n;i++) {FA[I].E=-1; fa[i].x=-1; } } returnans;}intMain () { while(~SCANF ("%d%d",&n,&m)) {init (); intU,v,c; for(intI=1; i<=m;i++) {scanf ("%d%d%d",&u,&v,&c); Add (U,V,C); Add (V,u,0); } intans=Work (); printf ("%d\n", ans); } return 0;}
"Maximum Flow" Hihocoder 1369: Network Flow one · Ford-fulkerson algorithm