Implementation features: Same as before
Can see is a lot of beautiful program, code short more than one times, but the speed is and the original adjacency table A level (in the codevs above the grassland drainage that the running time comparison, but obviously the data is very big should be slower than that), the principle is similar, feel DFS inside the back and forth variable is amazing
1 var2 S,t,i,j,k,l,m,n,ans:longint;3A:Array[0.. +,0.. +] ofLongint;4D,DV:Array[0..10000] ofLongint;5 functionmin (x,y:longint): Longint;inline;6 begin7 ifX<y ThenMin:=xElsemin:=y;8 End;9 functionDFS (x,flow:longint): Longint;inline;Ten varI,j,k,l:longint; One begin A ifX=t Thenexit (flow); -dfs:=0; - fori:=1 toN Do the if(a[x,i]>0) and(D[x]= (d[i]+1)) Then - begin -K:=dfs (I,min (flow-dfs,a[x,i])); - Dec (a[x,i],k); + Inc (A[I,X],K); - Inc (DFS,K); + ifDfs=flow Thenexit; A End; at ifD[s]=n Thenexit; - Dec (dv[d[x]]); - ifdv[d[x]]=0 Thend[s]:=N; - Inc (D[x]); - Inc (Dv[d[x]); - End; in begin - readln (n,m,s,t); toFillchar (A,sizeof (a),0); + fori:=1 toM Do - begin the readln (j,k,l); * Inc (A[J,K],L); $ End;Panax NotoginsengFillchar (d,sizeof (d),0); -Fillchar (Dv,sizeof (DV),0); thedv[0]:=n;ans:=0; + whileD[s]<n DoInc (Ans,dfs (s, Maxlongint)); A writeln (ans); the End.
Algorithm template--SAP Network maximum stream 3 (recursive + adjacency matrix)