Network flow (study notes)

 Network flow (study notes)

 (PS: This article review the use of pure, want to see the figure for children's shoes is not very friendly)

   

  We imagine the water plant to your home water supply network is a complex directed graph, each tube has a maximum load of water-saving flow. Waterworks do not turn on the water, your home is without water. But even if waterworks desperately to the water pipe network inside your home to receive water flow is the upper limit (after all, the limited carrying capacity of each pipe). You want to know how much water you can get, this is a network flow problem.

  Here, I do not like to throw a lot of direct theorems, such as what oblique symmetry ah, ah Han Han's flow conservation, when you learn the basic network flow algorithm, I believe that these are not important .

Here I did not follow the different issues which are classified, but on different algorithms to consider.

  • EK algorithm (Edmonds Karp algorithm)

 - This is a one-way bfs based augmentation algorithm.

  Pre-knowledge:

  In the network flow augmenting path: if there is a path c (s, t), such that the total flow of the current is increased, then this path is called augmenting path.

  Easy to find, if augmented road network does not exist, then you can get the maximum flow of traffic on the network.

  So seeking maximum flow problem is transformed for the sake of augmenting path issues that continue to find augmenting paths until found.

  How does it work? We start from the BFS s, by the remaining amount (the total capacity of the current flow rate minus side) is greater than 0 side, when the time ran each t, ​​we have to find out the smallest remaining amount change c (s, t) path, the this path fill: i.e. each side are subtracted the minimum residual.

  Until we find that there is so far no such path.

  So here there is a question: When augmenting a path, you find another path has been to reduce the amount of residues on the current path, but from a point where the amount of residual t still available, then you will find you can not get the optimal solution. Okay, here Pirates a map:

For example: You have found a s-> 3-> 5-> t, the flow rate is 10

You're a search s-> 4-> 5-> t, subtracting a first flow path, the new flow rate is 35

But you find that you can search s-> 3-> t and s-> 4-> 5-> t, 10, 45 respectively so that the flow rate, clearly greater.

可如果你不做任何处理的话,你会发现你在搜过s->3->5->t后,你永远不会再搜到3了,也就不会找到s->3->t这条路径了。

  那么怎么办呢?我们考虑对每条边加一条初始流量为0的反向边。

  当我们先搜s->3->5->t时,我们把正边减去最大流量,反边加上最大流量。

这就告诉了第二条路径s->4->5->t,在到达节点5时,其实有10的流量是通过3留过来的,那么如果把这10流量返回去,再次从3开始跑,若能到达t则相当于找到了一条增广路。进而为第二条路径增大了10残量。

是不是很NB,下面放上code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=10010;
const int maxm=100010;
const int inf=1<<30;

struct node
{
    int to,next,dis;
}g[maxm*2];
int head[maxn],cnt=1; 
struct P
{
    int edge,from;
}pre[maxn];
int ans;
int n,m,s,t;
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

inline void addedge(int u,int v,int dis)
{
    g[++cnt].next=head[u];
    g[cnt].to=v;
    g[cnt].dis=dis;
    head[u]=cnt;
}

bool vis[maxn];
bool bfs()
{
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    queue<int>q;
    q.push(s);
    vis[s]=1;
    while(q.size())
    {
        int u=q.front();q.pop();
        for(int i=head[u];i;i=g[i].next)
        {
            int v=g[i].to;
            if(!vis[v]&&g[i].dis)
            {
                pre[v].edge=i;
                pre[v].from=u;
                if(t==v)return 1;
                vis[v]=1;
                q.push(v);
            }
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/THRANDUil/p/10958237.html