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]<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