SAP Algorithm Insights

Source: Internet
Author: User

2014 11 13

Today, the SAP algorithm, feel a lot of harvest, write some experience.

On the Internet to check SAP, "The Mark I is d[i], then if the arc to meet D[i]=d[j]+1 (I,J) is called the permissible arc, and the augmented only go to allow the arc, then you can achieve" how to walk is the shortest "effect. In fact , the idea of dinic, d "I" is the distance from the current point to the meeting point, and the D array to all point hierarchies. (If you don't know what to look for yourself)

the method of maintaining the distance label: when finding a certain point in the process of finding a way to start without an allowable arc, the distance label of the point is set to the minimum value of the distance designator of the end point of all arcs from which it departs. This method of maintaining the distance from the label is correct. Due to the existence of distance marking, because "how to walk is the shortest", so you can use DFS to find the augmented road, with a stack to save the current path arc. When the distance designator of a point is changed, the arc that points to it in the stack is definitely not an allowable arc, so let it out of the stack and continue with the endpoint augmentation of the arc at the top of the stack. There is also a constant optimization of the wording is to change the distance label when the front arc is set to provide the minimum marking of the arc. The correct notation for the current arc is that at any time we can guarantee that there is no allowable arc in front of the current arc in the adjacency table.

Here we focus on two optimizations.

1 Current arc: Di "I" records another node of the current edge starting with the first point. Note the his array, which records the minimum value in the current path and prevents the previous result from being overwritten by the change in the changes to the.

2 Gap Optimization: VH "I" records the number of nodes with a distance of I. If the vh "I" =0, S, T is not connected, exit. When you change the distance of a node from X to Y, Inc "Y" Dec "x".

The following program is given:

var
Flag:boolean;
Jl,min,flow,aug,j,m,n,tmp,a,b,c,i:longint;
his,pre,dis,vh,di:array[0..1024] of Longint;
map:array[1..1024,1..1024] of Longint;
begin
readln (m,n);
For i:=1 to M do
begin
readln (a,b,c);
Inc (MAP[A,B],C);
end;
vh[0]:=n;
For i:=1 to n do di[i]:=1;
i:=1;
Aug:=maxlongint;
While dis[1]<n do
begin
His[i]:=aug;
Flag:=false;
For J:=di[i] to n do
begin
if (map[i,j]>0) and (Dis[j]+1=dis[i]) Then
begin
flag:=true;
di[i]:=j;
if Map[i,j]<aug then aug:=map[i,j];
pre[j]:=i;
i:=j;
if I=n then
begin
Inc (FLOW,AUG);
While i<>1 do
begin
tmp:=i;
I:=pre[i];
Dec (map[i,tmp],aug);
Inc (MAP[TMP,I],AUG);
end;
Aug:=maxlongint;
end;
Break ;
end;
end;
if flag then continue;
min:=n-1;
For j:=1 to n do
begin
if (map[i,j]>0) and (dis[j]<min) then begin jl:=j;min:=dis[j];end;
end;
Di[i]:=jl;
Dec (vh[dis[i]]);
if vh[dis[i]]=0 then break;
dis[i]:=min+1;
Inc (Vh[dis[i]);
if i<>1 then begin i:=pre[i];aug:=his[i];end;
end;
write (flow);
end.

SAP Algorithm Insights

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.