Network flow (cancer) study summary 1

These days to learn the network flow, just want to say, really toxic. . .

For a network G = (V, E); For each side (x, y) ∈ E; has a flow rate c (x, y) is called the capacity side. In particular, if (x, y) ∉ E, then
c (x, y) = 0 . FIG there are two special nodes specified in S ∈ V and V ∈ T
(S ≠ T), referred to as the source and sink. (Above excerpt from "algorithm contest Step-up Guide")

Good lie, do not say so inscrutableWasteThen, first on the map (s v2 does not flow into the first tube do):

A flow network is a path from s to a flow path and the path consisting of t.

Analogy, each edge is like a pipe, its capacity is the capacity of water, water will burst exceeds the capacity (~ bang), and s is water, the water pouring out from s, t Yes. . . Place to water (can not think of any good terms QWQ).

Thus, to use more water, you let more water flows from t s, and the last is the most water maximum flow of the network.

Or the map:

A size 16 for the flow of water from the s arriving v1, v1 to v3 capacity size found only 12, so no recourse but to flow past the first 12 of the remaining water can reach 4 found v1 v2, and capacity 10 (greater than 4), can, in the past it so happy! ! V3 reaches only found in the t, and a capacity of 20 (really big), then go to the 12 water. . . V2 found in water can only go to v4, a capacity of 14, then by the same v4, v4 just right by the t, s and t from a size of the birth of the stream 16! ! !

If the capacity of a pipe 10, which has a size of 4 through water, and then it can only be a maximum size of the water flowing through the 6. . .

Above, if s to v2 stream is 0 (i.e., not connected), then the maximum flow of the network is just obtained 16; assuming s to v2 capacity of 10, the 10 water after v2 not go v1, because edge v1 to v3 has been full flow, while the edge v2 to v4 remaining capacity of just 10, so finally reach v4, v4 and found to be full of t side stream, and had to go to v3, v3 also found 8 remaining capacity, flow past 8:00, and the remaining 2 points (you cry). . . The maximum flow is obtained 8 + 16 = 24;

Finally finished what is the maximum flow, but it is still a good idea to understand, but how to achieve it? ?

Sell ​​tubes (guan) child first. . .

So, our Edmonds-Karp algorithm augmenting path was born.

Introduce the augmenting path concept: If a path from s to t, the remaining capacity of the edges are greater than 0, this path is called an augmenting path. So it is clear that our task is to constantly look for augmenting path, the overall network traffic increases until not find such an augmenting path so far.

The idea is to find EK algorithm augmenting path via BFS, until there is no date on the network augmenting paths. Looking at each process, we consider each side of the remaining capacity is greater than 0, and find a path from s to t, then this path is the minimum capacity of the network is to be increased minf traffic.

But if the first time we flow the wrong how to do it? What does it mean, for example, the point compared to the city, side compared to the train line, and edge capacity is the number of remaining tickets this route, if there are three people came to the city 2, city 2 to city from city 13 originally had five ticket, they took three tickets to the city 3, and now five people to the city 2, 3 and they want to go to the city, but the ticket was not enough, but they heard a city have a direct line to the city and 3 tickets the remaining three, you certainly think so smart, let the three men back to the city 1 (that is, not to the city 2) 3 directly to the city, so the city five people 2 can also go to the city 3, perfect! But, how to make the machine also know this schedule it?

Thus, we can keep the edge at (x, y), while the storage capacity of a reverse side (y, x) 0 (since no one go back), in the course of the BFS, the forward side and the reverse side Meanwhile the search, if the search to the positive side, show a direct flow, if the reverse side, indicating go back.

That is, for an augmenting path, wherein each side (x, y) (either forward or reverse side edge, can go back because the reverse side) of the residual capacity to be reduced augmenting flow passage e ,
while its reverse side (y, x) of the remaining capacity will also increase e (as can go back up to e).

Well, the core has been introduced over EK algorithm, it is theoretically time complexity O ($ nm ^ {2} $)In fact O (I can live)But it falls far short of this level, the higher the efficiency of the actual operation.

Specific details of the code explain it.

Luo Gu P3376 template title

And then paste the code:

#include<bits/stdc++.h>

using namespace std;

const int M = 100100,inf = 1 << 29;
int n,m,s,t,maxflow,tot = 1;//令tot从1开始累加,方便后面运算,具体看42,43行。 
int ver[M*2],edge[M*2],Next[M*2],head[M*2],pre[M*2];

void add(int x,int y,int z){
     ver[++tot] = y;edge[tot] = z;Next[tot] = head[x];head[x] = tot;//边权用来存剩余容量。 
     ver[++tot] = x;edge[tot] = 0;Next[tot] = head[y];head[y] = tot;//反向建边 
}

const int N = 10100;
int v[N],incf[N];
bool bfs(){
     memset(v,0,sizeof(v));
     queue<int>q;
     q.push(s);v[s] = 1;
     incf[s] =  inf;//初始化流量为无穷大,这样才能找到最小值minf。 
     while(q.size()){
        int x = q.front();q.pop();
        for(int i = head[x];i;i = Next[i]){
            if(edge[i]){//如果当前的边剩余容量大于0,可以选取。 
                int y = ver[i];
                if(v[y]) continue;//选过的点不选。 
                pre[y] = i;//记录前驱,便于找到路径的实际方案。 
                incf[y] = min(incf[x],edge[i]);//找路径上的最小流量minf。 
                q.push(y);v[y] = 1;
                if(y == t) return 1;//找到了,就返回1。 
             }
         }
     }
     return 0;//未找到,说明不存在增广路了,搜索结束,返回0。 
}

void update(){
     int x = t;
     while(x != s){//更新每一条边的剩余容量。 
        int i = pre[x];
        edge[i] -= incf[t];
        edge[i^1] += incf[t];//用了成对存储的技巧,从1开始成对存,i^1就可以找到反向边的编号。 
        x = ver[i^1];//另一个节点,直到倒推到源点s。 
     }
     maxflow += incf[t];//更新最大流。 
}

int main(){
    cin>>n>>m;
    cin>>s>>t;
    for(int i = 1;i <= m; i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);//加边。 
    }
    while(bfs()) update(); 
    printf("%d",maxflow);
    return 0;
} 

This Tacca first blog Yeah, well written forgive me ah QAQ

886。。。

Guess you like

Origin www.cnblogs.com/Aurelian/p/11570307.html