Beginners network flow

Beginners network flow

This theme not previously been exposed, I feel a little abstract, algorithm a little less clear, read some blog, below, summarizes these blog do.

Introduction:

Background knowledge to explain some of the specific reference to this blog (before specific to the code), the network has made a preliminary first impression stream

https://blog.csdn.net/wzw1376124061/article/details/55001639

Then some conceptual definitions and specific reference this blog, some of which may affect the basic concepts of algorithm understanding of the conduct master

https://www.cnblogs.com/widerg/p/9387909.html

algorithm:

Then contacts the algorithm, the display thought FF (Ford-Fulkerson) algorithm, using a DFS, it is understood that the main flow and reverse sides algorithms increased. Then EK algorithm, also above a blog

After an overview of the algorithm can take a look at an example, a blog of the above chart is a good understanding of the negative side of example, with this blog

https://www.cnblogs.com/zsboy/archive/2013/01/27/2878810.html binding analysis method of example

Code:

Here you can see the code, mainly because it is relatively clear several blog:

https://blog.csdn.net/wzw1376124061/article/details/55001639

https://www.cnblogs.com/widerg/p/9387909.html

Summary and Development:

The final summary of this blog is more suitable: https://www.cnblogs.com/rvalue/p/10650849.html, analysis of relatively small, although I'm not real clear on what to say, it is estimated that familiar with it later You will understand it

If you want more advanced dinic algorithm, also on a blog, add this https://baijiahao.baidu.com/s?id=1612179096991409044&wfr=spider&for=pc, explain more clearly

template:

//Edmonds Karp
#include <iostream>
#include <vector>
#include <queue>
#include <memory.h>
#define maxn 1005
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp
{
    int n,m;
    vector<Edge> edges;//保存各个边的信息,包括反向边
    vector<int> G[maxn];//G[i][j]表示第i个节点的第j条边在edges中的编号
    int a[maxn];//起点到i的可改进量
    int p[maxn];//最短路树p的入弧编号
    void init(int n)
    {
        for(int i = 0;i<n;i++)
        {
            G[i].clear();
        }
        edges.clear();
    }
    void AddEdge(int from,int to,int cap)
    {
        edges.push_back(Edge(from,to,cap,0));//edges中的第m-2号边,目前的倒数第二个
        edges.push_back(Edge(to,from,0,0));//反向边,edges中的第m-1号边,目前的最后一个
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    int Maxflow(int s,int t)
    {
        int flow = 0;
        for(;;)
        {
            memset(a,0,sizeof(a));
            queue<int> que;
            que.push(s);
            a[s]=INF;
            while(!que.empty())
            {
                int x = que.front();
                que.pop();
                for(int i = 0;i<G[i].size();i++)
                {
                    Edge& e = edges[G[x][i]];
                    if(!a[e.to]&&e.cap>e.flow)//如果e.to节点还没有加过流(这一次BFS)
                    {
                        p[e.to] = G[x][i];
                        a[e.to] = min(a[x],e.cap-e.flow);
                        que.push(e.to);
                    }
                }
                if(a[t]) break;//若已拓展到t点,跳出
            }
            if(!a[t])//若无法到达a点,说明无增广路,结束算法
            for(int u = t;u!=s;u=edges[p[u]].from)//从t开始向前更新路径上的流
            {
                edges[p[u]].flow+=a[t];
                edges[p[u]^1].flow-=a[t];//亦或是相反边的编号
            }
            flow += a[t];
        }
        return flow;
    }
};
//Ford Fulkerson
#include <iostream>
#include <vector>
#include <memory.h>
#define maxn 1005
#define INF 0x3f3f3f3f
using namespace std;
struct edge
{
    int to;//终点
    int cap;//容量
    int rev;//反向边号
};
vector<edge> G[maxn];//图的邻接表表示
bool used[maxn];//DFS中用到的访问标记
void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});//记录反向边在to个节点的vector中的位置
    G[to].push_back((edge){from,0,G[from].size()-1});
}
int dfs(int v,int t,int f)
{
    if(v==t) return f;
    used[v]=1;
    for(int i = 0;i<G[v].size();i++)
    {
        edge& e = G[v][i];
        if(!used[e.to]&&0<e.cap)
        {
            int d = dfs(e.to,t,min(e.cap,f));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;//未找到增广路
}
int max_flow(int s,int t)
{
    int flow = 0;
    for(;;)
    {
        memset(used,0,sizeof(used));
        int f = dfs(s,t,INF);
        if(f==0)
        {
            return flow;
        }
        flow += f;
    }
}
//Dinic
#include <iostream>
#include <vector>
#include <queue>
#include <memory.h>
#define maxn 1005
using namespace std;
struct edge
{
    int to;
    int cap;
    int rev;
};
vector<edge> G[maxn];//图的邻接表表示
int level[maxn];//顶点到原点的距离标号
int iter[maxn];//当前弧,在之前的变已经没有用了
void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}
//通过BFS寻找从原点出发的且未满流的距离标号
void bfs(int s)
{
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while(!que.empty())
    {
        int v = que.front();
        que.pop();
        for(int i = 0;i<G[v].size();i++)
        {
            edge& e = G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to] = level[v]+1;
                que.push(e.to);
            }
        }
    }
}
//通过dfs寻找增广路
int dfs(int v,int t,int f)
{
    if(v==t) return f;
    for(int& i = iter[v];i<G[v].size();i++)
    {//当前弧优化,iter[v]的值在随着i改变
        edge& e = G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            int d = dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;//找不到增广路
}

Representation template above are not the same, the understanding can easily choose

Reference article:

the above.

Guess you like

Origin www.cnblogs.com/zhanhonhao/p/11285031.html