Network flow initial:< maximum flow >--core (augmented path algorithm)

Source: Internet
Author: User

Finally began to touch the network stream;

Network flow in the end is a shrimp east, in the comparative academic words, is

A direction graph g= (v,e);

There are two special points: source point S, meeting point T;

Each edge (U,V) ∈e in the figure has a non-negative capacity of C (U,V)

Recorded as g= (V,E,C)

Network three elements: Point, edge, capacity

In my case, the most difficult to understand is the capacity and flow, for beginners, it should be understood that each side has a C to indicate the maximum number of goods to be transported, and traffic F indicates the maximum number of goods currently shipped.

That would be better.

After solving the network flow, we have to think about the maximum flow.

What is the maximum flow

After we have the concept of network flow, we think, the maximum flow is the maximum flow from the starting point to the end point.

Among them, in the problem of maximum flow, we have to meet three conditions, 1: Capacity limit: F (u,v) <c (U,V);

2: Oblique symmetry: (f (u,v) =-f (V,u)) in the later use, we call once the goods from U to V, then there must be a fallback flow from V to u

3: Flow balance: Simply speaking, there is no other point to save the goods except source and sink points. Apparently F (s,u) ==f (v,t);

In this case, our aim is to make F (s,u) and F (v,t) the largest

How to maximize the goal, now, it is necessary to introduce the augmented path algorithm, which is very important: you can say how a network flow must have augmented path algorithm shadow.

Well, we think first, if a network flow has an augmented path, according to our understanding, then all the arcs on this road can let X goods through, then must not be the maximum flow

such as now;

There are also s--a---C---t can be augmented, then it must not be the maximum flow

But conversely, if a network does not have an augmented path, then he must be the biggest flow, some people say yes, of course. But look,

This is also a network without augmented road, but he is the biggest stream.

We found that it was possible. Orz

So, that was a maximum flow, not the maximum flow (what shrimp stuff).

That is to say, starting from B, there are two ways to go, but B has gone the wrong way, so it succeeded to the c---t blocked.

We also call this "blocking flow". This means that a wrong path is blocking other viable streams.

How to improve it, anyway B set out only two roads, you have to try it, since the above is blocking the flow.

The next one, try not to know, then we will put B--c back, take another road.

Right now. That's it

However, this swelling is realized, if the simulation will flow pressure back, it is difficult to point it.

So, we've introduced a reverse arc to push the flow with him.

Augmented Path ( paths can be improved ) the definition

If p is a link between the source point S and the meeting point T in the network, we define the direction of the road from S to T, then there are two types of arcs on the road:

L The direction of the forward arc---arc is the same as the direction of the road. The whole of the forward arc is recorded as p+;

L The direction of the---arc is opposite to the direction of the road. The whole of the back arc is the p;

Set F is a feasible flow, p is a path from S to T, if p satisfies the following conditions:

On all forward arcs (u,v) of the p+, 0≦f (u,v) < C (u,v);

On all back arcs (u,v) of P, 0<f (u,v) ≦c (U,V);

It is said that P is an extensible path for the feasible flow F.

It's a little hard to understand, right.

But we have succeeded.

Now, the network has become this way.

In fact, why reverse arc flow on the right, I think so, for an already have a reversible flow of the arc, once the withdrawal of the flow, its withdrawal must be able to return the flow back, so, to meet the three elements. It must be right.

Therefore, the augmented road theorem is: When a residual network is not augmented road, then he must be the largest flow.

Based on this theorem, we can design an algorithm.

Find the Augmented path augmentation on a residual network, and then, once there is no augmented path, it must be the maximum flow.

#include <cstdio>#include<cstring>#include<algorithm>#defineN 10000+10#defineM 10000+10#defineINF 1e9;using namespacestd;inthead[n],arnum=1, used[n],ans,way[m];structss{intNext,to,cap;} A[M];voidAddint  from,intTo,intCAP) {a[++arnum]= (ss) {head[ from],to,cap};head[ from]=Arnum;}voidInsertintUintVintCAP) {Add (U,v,cap), add (V,u,0);}intn,m,s,t;voidWorkintStep) {    intminn=inf;  for(intI=1; i<=step;i++) Minn=min (minn,a[way[i]].cap); Ans+=Minn;  for(intI=1; i<=step;i++) {A[way[i]].cap-=Minn; A[way[i]^1].cap+=Minn; }}intDfsintUintStep) {     for(intI=head[u];i;i=A[i].next) {        intv=a[i].to; intcap=A[i].cap; if(not Used[v] and cap>0) {Way[step]=i; USED[V]=1; if(v==t) {Work (step);return 1;} Else if(Dfs (v,step+1))return 1; }        return 0; }}intMain () {scanf ("%d%d%d%d",&n,&m,&s,&T); intU,v,c;  for(intI=1; i<=m;i++) {scanf ("%d%d%d",&u,&v,&c); insert (U,V,C);}  for(;;) {memset (used,0,sizeof(used)); Used[s]=1; if(Not DFS (S,1)) Break; } printf ("%d", ans); return 0;}

This is the lowest-level algorithm O (∩_∩) o haha ~.

Attached code

Network flow initial:< maximum flow >--core (augmented path algorithm)

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.