and cannot be used for data that is relatively large. Learned the maximum SAP stream
Algorithm . Template question:
/* Maximum Flow template SAP */ # Include # Include String . H> # Include # Include Using Namespace STD; Const Int Maxn = 100010 ; // Maximum number of points Const Int Maxm = 400010 ; // Maximum number of Edges Const Int INF = 0x3f3f3f ; Struct Node { Int From , T
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 flowPrinciple: SAP network flow algorithm (see Baidu Encyclopedia, personally think this template has been good, although I have not considered the introduction of ad
begin -ans:=ans+; $ whileI 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:=N
) the { +j=Findalowarc (i); - if(j>=0) the {Bayipre[j]=i; theI=J; the if(i==t)//Updating residual networks - { -Delta=INF; the for(i=t;i!=s;i=Pre[i]) theDelta=min (delta,mp[pre[i]][i]); the for(i=t;i!=s;i=Pre[i]) themp[pre[i]][i]-=delta,mp[i][pre[i]]+=Delta; -flow+=Delta; the } the } the Else94 { the intX=relable (i);//re-label thenum[x]++; thenum[d[i]]--;98 i
,l:longint;p:point; - begin - ifX=t Thenexit (flow); -dfs:=0;p: =A[x]; - whilePNil 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
Implementation features: Same as beforeCan 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 amazing1 var2 S,t,i,j,k,l,m,n,ans:longint;3A:Array[0.. +,0.. +] ofLongint;4D,DV:Array[0..10000] ofLongint;5 functionm
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.