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

Source: Internet
Author: User

Implementation function: Ibid

The program is still beautiful, although a little longer than the adjacency matrix, but it does not matter this is inevitable, but the more important one must be-speed will be a qualitative leap ^_^ (this point pointer slightly made some innovative--anti pointer, which points to the current arc of the reverse arc, Facilitate the operation of the path augmentation, compared to the non-recursive inside to use an OP function to find out is already much stronger!!! )

1 type2Point=^node;3Node=Record4 G,w:longint;5 Anti,next:point;6     End;7 var8 I,j,k,l,m,n,ans,s,t:longint;9A:Array[0..10000] ofPoint ;TenD,DV:Array[0..10000] ofLongint; One functionmin (x,y:longint): Longint;inline; A          begin -               ifX<y ThenMin:=xElsemin:=y; -          End; the procedureAdd (x,y,z:longint); inline; -           varP:point; -           begin -New (p);p ^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p; +New (p);p ^.g:=x;p^.w:=0;p ^.next:=a[y];a[y]:=p; -a[y]^.anti:=a[x];a[x]^.anti:=A[y]; +           End; A functionDFS (x,flow:longint): Longint;inline; at          varI,j,k,l:longint;p:point; -          begin -               ifX=t Thenexit (flow); -dfs:=0;p: =A[x]; -                whileP<>Nil  Do -                     begin in                          if(p^.w>0) and(D[x]= (d[p^.g]+1)) Then -                             begin toK:=dfs (P^.g,min (flow-dfs,p^.w)); + Dec (p^.w,k); - Inc (P^.ANTI^.W,K); the Inc (DFS,K); *                                  ifDfs=flow Thenexit; $                             End;Panax Notoginsengp:=P^.next; -                     End; the               ifD[s]=n Thenexit; + Dec (dv[d[x]]); A               ifdv[d[x]]=0  Thend[s]:=N; the Inc (D[x]); Inc (Dv[d[x]); +          End; - begin $ readln (n,m,s,t); $       fori:=1  toN Doa[i]:=Nil; -       fori:=1  toM Do -          begin the readln (j,k,l); - Add (j,k,l);Wuyi          End; theFillchar (d,sizeof (d),0); -Fillchar (Dv,sizeof (DV),0); Wudv[0]:=n;ans:=0; -       whileD[s]<n DoInc (Ans,dfs (S,maxlongint)); About writeln (ans); $ End.

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

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.