Algorithm template--SAP Network maximum flow

Source: Internet
Author: User

Implementation function: The first line input n,m,s,t, representing this graph n points, M edge, the source point is S, the meeting point is T, then the T line enters the starting point, the end point and the weight of the edge; output maximum flow

Principle: SAP network flow algorithm (see Baidu Encyclopedia, personally think this template has been good, although I have not considered the introduction of adjacency table for optimization)

1 var2 I,j,k,l,m,n,ans,aug,s,t,tmp,jl,mi:longint;3 Flag:boolean;4Vh,dis,di,his,pre:Array[0..10000] ofLongint;5MapArray[0..2050,0..2050] ofLongint;6 functionmin (x,y:longint): Longint;inline;7          begin8               ifX<y ThenMin:=xElsemin:=y;9          End;Ten begin One readln (n,m,s,t); AFillchar (map,sizeof (map),0); -       fori:=1  toM Do -          begin the readln (j,k,l); -map[j,k]:=map[j,k]+l; -          End; -i:=s;ans:=0; aug:=Maxlongint; +Fillchar (dis,sizeof (DIS),0); -       fori:=1  toN Dodi[i]:=1; +i:=s; A       whileDis[s]<n Do at            begin -flag:=false; -his[i]:=; -                  forJ:=di[i] toN Do -                     begin -                          if(map[i,j]>0) and(Dis[i]= (dis[j]+1)) Then in                             begin -aug:=min (aug,map[i,j]); topre[j]:=i;di[i]:=J; +i:=j;flag:=true; -                                  ifI=t Then the                                     begin *ans:=ans+; $                                           whileI<>s DoPanax Notoginseng                                                begin -tmp:=i; thei:=Pre[i]; +map[i,tmp]:=map[i,tmp]-; Amap[tmp,i]:=map[tmp,i]+; the                                                End; +aug:=Maxlongint; -                                     End; $ Break ; $                             End; -                     End; -                 ifFlag Thencontinue; thejl:=-1; mi:=n-1; -                  forj:=1  toN DoWuyi                     begin the                          if(map[i,j]>0) and(DIS[J]&LT;MI) Then -                             begin Wumi:=Dis[j]; -jl:=J; About                             End; $                     End; -di[i]:=JL; - Dec (vh[dis[i]]); -                 ifvh[dis[i]]=0  ThenBreak ; Adis[i]:=mi+1; + Inc (Vh[dis[i]); the                 ifI<>s Then -                    begin $i:=Pre[i]; theaug:=His[i]; the                    End; the            End; the writeln (ans); - End. in         

Algorithm template--SAP Network maximum flow

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.