Algorithm template--SAP Network maximum stream 3 (recursive + adjacency matrix)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.