Network flow study concluded 2

Yesterday wrote a network flow EK algorithm (I myself do not understand written), Today again to summarize another network maximum flow algorithm of Dinic .

First to introduce a concept: the residual network.

A network capacity is greater than all the remaining side edges and with the nodes connected to the network consisting of network remaining amount of 0.

We recall process EK algorithm:

1. Find the edge of a remaining capacity in the network is greater than 0, it is added into the augmenting path.

2. find augmenting paths based on the remaining capacity and maximum flow side update.

So, every time we search augmenting paths, may need to traverse the entire residual network, but only found an augmenting path can be further optimized.

Here, we propose a hierarchical node D [x], which represents the number of s from the edge to go through a minimum of x. The residual network, satisfy d [y] = d [x ] + all sides (x, y) constituting the subgraph is referred to as a hierarchical diagram . Obviously, it is a directed acyclic graph.

The legendary Dinic algorithm is to seek maximum flow through the following two steps:

1. residual network constantly by BFS FIG layered configuration, can not be reached until s t.

2. FIG request in a layering augmenting path with DFS, updates the remaining capacity at backtracking.

Well, in general presentation.thank, specifically how it happened? ? (In fact, I do not understand

When the BFS, we find each node x, to which the minimum number of edges s D [x] must have been determined, then all edges (x, Y) (the remaining capacity is greater than 0) to x can become starting points FIG subgraph layer, so we put all the nodes connected to the x-y, D [y] (it has not been assigned) are updated to d [y] = d [x] + 1, since only one to walk you can go to the edge y. Thus, at the end of the BFS, layered graph has been constructed out.

Then we DFS on this layered graph, you backtrack until you find the Meeting Point t when, at the same time over which the DFS updates the remaining capacity of each side of it, every last updated at maximum flow maxflow (and then you AC La). Time complexity a little faster than the EK, is O (mn ^ 2)In fact O (I can live)

QAQ still that question: Luo Gu P3376 template title

Then mygood-lookingCode:

#include<bits/stdc++.h>

using namespace std;

const int inf = 1 << 29,N = 10100,M = 100100;
int n,m,s,t,maxflow,tot = 1;
int ver[M*2],edge[M*2],Next[M*2],head[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;//依然要反向建边。 
}
queue<int>q;
int d[N];//d[x]记录s到x要经过的最少边数。 
bool bfs(){
     memset(d,0,sizeof(d));
     while(q.size()) q.pop();
     q.push(s);d[s] = 1;
     while(q.size()){
        int x = q.front();q.pop();
        for(int i = head[x];i;i = Next[i]){
            if(edge[i]){//在残量网络中寻找。 
                int y = ver[i];
                if(d[y]) continue;//如果d[y]有值,说明在x之前已经搜过了,不再搜。 
                d[y] = d[x] + 1;//没搜过就更新d[y]。 
                q.push(y);
                if(y == t) return 1;//如果到达t,说明分层图建完了。 
             }
         }
     }
     return 0;//未到达说明以求得网络最大流。 
}

int dinic(int x,int flow){//x为当前节点,flow为当前流量。 
    if(x == t) return flow;//搜到t,结束并回溯。 
    int rest = flow,k;
    for(int i = head[x];i;i = Next[i]){
        if(edge[i]){//依旧在残量网络中搜索。 
            int y = ver[i];
            if(d[y] == d[x] + 1){//如果是分层图中的点,说明可以有增广路。 
                k = dinic(y,min(rest,edge[i]));//接着搜索y,当前流为rest和edge[i]中更小的。 
                if(!k) d[y] = 0;//如果k==0,说明无法到达 t了,剪枝,从分层图中删去。 
                edge[i] -= k;//回溯时更新。 
                edge[i^1] += k;
                rest -= k;//剩余流减少,因为要返回flow - rest。 
            }
        }
    }
    return flow - rest;
}

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);
    }
    int flow = 0;
    while(bfs())
      while((flow = dinic(s,inf))) maxflow += flow;//更新最大流。 
    printf("%d",maxflow);
    return 0;
}

Network tumors too toxic, I just really have to learn a little bit of fur. . .

Guess you like

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