Implementation function: Same maximum flow 1
This is mainly to the front of the adjacency matrix into the adjacency table, in contrast to the speed greatly improved-I measured, when m=1000000 n=10000, for the moment, regardless of the adjacency matrix will be MLE, the new program speed many times faster (our home this very weak computer time 0.3s), and when m= 300000 n=10000, the advantage is more obvious (almost the second out), the other is not, especially when encountering the sparse map when this is a big cost-effective!!!
1 type2Point=^node;3Node=Record4 G,w:longint;5 Next:point;6 End;7 8 var9 I,j,k,l,m,n,tmp,ans,aug,mi,s,t:longint;TenDi,a:Array[0..10005] ofPoint ; OnePRE,HIS,DIS,VH:Array[0..10005] ofLongint; A Flag:boolean;p,jl:point; - functionmin (x,y:longint): Longint;inline; - begin the ifX<y ThenMin:=xElsemin:=y; - End; - functionAdd (x,y,z:longint): Longint;inline; - varP:point; + begin -New (p);p ^.w:=z;p^.g:=y; +p^.next:=a[x];a[x]:=p; A End; at procedureop (x,y,z:longint); inline; - varP:point; - begin -p:=A[x]; - whileP<>Nil Do - begin in if(p^.g=y) and((p^.w+z) >=0) Then - begin top^.w:=p^.w+Z; + Break ; - End; thep:=P^.next; * End; $ End;Panax Notoginseng begin - readln (n,m,s,t); the fori:=1 toN Doa[i]:=Nil; + fori:=1 toM Do A begin the readln (j,k,l); +Add (j,k,l); Add (K,j,0); - End; $ fori:=1 toN Dodi[i]:=A[i]; $Fillchar (dis,sizeof (DIS),0); -Fillchar (pre,sizeof (PRE),0); -Fillchar (His,sizeof (HIS),0); theFillchar (vh,sizeof (VH),0); -i:=s;vh[0]:=n;ans:=0; aug:=Maxlongint;Wuyi whileDis[s]<n Do the begin -flag:=false;his[i]:=; Wup:=A[i]; - whileP<>Nil Do About begin $ if(p^.w>0) and(Dis[i]= (dis[p^.g]+1)) Then - begin -aug:=min (AUG,P^.W); -pre[p^.g]:=i;di[i]:=p; Aflag:=true;i:=p^.g; + ifI=t Then the begin -ans:=ans+; $ whileI<>s Do the begin thetmp:=i; thei:=Pre[i]; theOP (i,tmp,-); - op (Tmp,i,aug); in End; theaug:=Maxlongint; the End; About Break ; the End; thep:=P^.next; the End; + ifFlag Thencontinue; -jl:=Nil; mi:=n-1; thep:=A[i];Bayi whileP<>Nil Do the begin the if(p^.w>0) and(DIS[P^.G]<MI) Then - begin -jl:=p;mi:=DIS[P^.G]; the End; thep:=P^.next; the End; thedi[i]:=JL; - Dec (vh[dis[i]]); the ifvh[dis[i]]=0 ThenBreak ; thedis[i]:=mi+1; the Inc (Vh[dis[i]);94 ifI<>s Then the begin thei:=Pre[i]; theaug:=His[i];98 End; About End; - writeln (ans);101 End.102
Algorithm template--SAP Network maximum stream 2