[模板]最大流(Edmonds_Karp/Dinic算法(以poj1273为例

poj1273 Drainage Ditches

最大流算法

1、Edmonds-Karp算法

这个链接学习,比较简单易懂,虽然实际比赛并不用,但是因为其他代码较之更复杂,所以先理解这个算法!

代码奉上:

用邻接矩阵实现:

/**************************************************************
    Problem: POJ_1273
    User: soundwave
    Language: C++
    Result: Accepted
    Time: 16ms
    Memory: 1032KB
****************************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
/*
int cap[][];//cap[i][j] 代表点i到j的容量
int flow[][];//flow[i][j] 代表点i到j的流量
int path[];//记录路径,p[i]到i
int a[];//起点到i的可改进量, a[v] = cap[u][v]-flow[u][v]; a[]有vis的功能
*/
using namespace std;
const int maxn = 500+5;
const int INF = 1<<29;
int cap[maxn][maxn];
int flow[maxn][maxn];
int path[maxn];
int n, m;
int my_min(int x, int y){
    return x<y?x:y;
}
void init(){
    memset(cap,0,sizeof(cap));
    memset(flow,0,sizeof(flow));
    memset(path,0,sizeof(path));
}
int Edmonds_Karp(int s, int t){
    int a[maxn];
    int re = 0;
    queue<int>Q;
    while(1){
        memset(a,0,sizeof(a));
        a[s] = INF;
        Q.push(s);
        //bfs找增广路
        while(!Q.empty()){
            int u = Q.front();
            Q.pop();
            for(int v=1; v<=n; v++)
            if(!a[v] && cap[u][v]>flow[u][v]){
                path[v] = u;
                Q.push(v);
                a[v] = my_min(a[u], cap[u][v]-flow[u][v]);
            }
        }
        if(a[t]==0) break;//找不到增广路,当前的流量就是最大流
        for(int u=t; u!=s; u=path[u]){
            flow[path[u]][u] += a[t];
            flow[u][path[u]] -= a[t];
        }
        re += a[t];
    }
    return re;
}
int main(){
    int x, y, val;
    while(~scanf("%d%d", &n, &m)){
        init();
        while(m-->0){
            scanf("%d%d%d", &x, &y, &val);
            cap[x][y] += val;
        }
        x = Edmonds_Karp(1,n);
        printf("%d\n", x);
    }
    return 0;
}
/*
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
--------
50
*/

用邻接表实现,白皮书提供的模板

/**************************************************************
    Problem: POJ_1273
    User: soundwave
    Language: C++
    Result: Accepted
    Time: 16ms
    Memory: 748KB
****************************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <queue>
#include <vector>
#include <cmath>
#include <string.h>
//注意重边
using namespace std;
typedef __int64 LL;
const int maxn = 500+5;
const int INF = 1<<29;
const double PI = acos(-1.0);
const double EPS = 1e-10;
//const int MemsetBigNum=127;///对于 int数组 可以得到  2139062143  INT_MAX = 2147483647

int my_min(int x, int y){
    return x<y?x:y;
}
struct Edge{
    int from, to;
    int cap, flow;
    Edge(int u, int v, int c, int f): from(u), to(v), cap(c), flow(f) {}
};
struct Edmonds_karp{
    int n, m;
    vector<Edge> edges;//边数的两倍
    vector<int> G[maxn];//邻接表, G[i][j]表示节点i的第j条边在e数组中的序号
    int path[maxn];//记录路径,p[i]到i
    int a[maxn];//起点到i的可改进量, a[v] = cap[u][v]-flow[u][v]; a[]有vis的功能

    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.push_back(Edge(to,from,0,0));
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    int MaxFlow(int s, int t){
        int re = 0;
        while(1){
            queue<int> Q;
            /*要把"queue<int> Q;"这个声明放进来,
            因为"if(a[t]) break;"语句的缘故,while退出时队列里面可能不是空的
            */
            Q.push(s);
            memset(a,0,sizeof(a));
            a[s] = INF;
            //bfs找增广路
            while(!Q.empty()){
                int u = Q.front();
                Q.pop();
                for(int i=0; i<G[u].size(); i++){
                    Edge &v = edges[G[u][i]];
                    if(!a[v.to] && v.cap>v.flow){
                        path[v.to] = G[u][i];
                        Q.push(v.to);
                        a[v.to] = my_min(a[u], v.cap-v.flow);
                    }
                }
                if(a[t]) break;//找到一条增广路,,退出
            }
            if(!a[t]) break;//找不到增广路,当前的流量就是最大流
            for(int u=t; u!=s; u=edges[path[u]].from){
                edges[path[u]].flow += a[t];
                edges[path[u]^1].flow -= a[t];
            }
            re += a[t];
        }
        return re;
    }
};
int n,m;
Edmonds_karp EK;
int main(){
    int x, y, val;
    while(~scanf("%d%d", &m, &n)){
        EK.init(n);
        while(m-->0){
            scanf("%d%d%d", &x, &y, &val);
            EK.AddEdge(x,y,val);
        }
        x = EK.MaxFlow(1,n);
        printf("%d\n", x);
    }
    return 0;
}
/*
6 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
3 4 10
--------
60
*/


2、Dinic算法

因为邻接表更好用一些,所以还是以Edmonds-Karp算法的邻接表实现作为底板进行扩展

“层次图”和“阻塞流”是Dinic算法的关键词。
该算法就是不停地用BFS构造层次图,然后用阻塞流来增广(即从起点开始在层次图中DFS,每找到一条路就增广)。

层次图的概念很好理解,就是设每条给定边的权值为1,累计的不同权值就是层次图的不同层次,也就是“入度”这一概念;

阻塞流也不难理解,就是指不考虑反向弧时的“极大流”,也就是找增广路啦

代码奉上:

/**************************************************************
    Problem: POJ_1273
    User: soundwave
    Language: C++
    Result: Accepted
    Time: 0ms
    Memory: 740KB
****************************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <queue>
#include <vector>
#include <cmath>
#include <string.h>
//注意重边
using namespace std;
typedef __int64 LL;
const int maxn = 500+5;
const int INF = 1<<29;
const double PI = acos(-1.0);
const double EPS = 1e-10;

int my_min(int x, int y){
    return x<y?x:y;
}
struct Edge{
    int from, to;
    int cap, flow;
    Edge(int u, int v, int c, int f): from(u), to(v), cap(c), flow(f) {}
};
struct Dinic{
    int n, m;
    int s, t;
    vector<Edge> edges;//边数的两倍
    vector<int> G[maxn];//邻接表, G[i][j]表示节点i的第j条边在e数组中的序号
    bool vis[maxn];//BFS使用
    int dis[maxn];//从起点s到i的距离
    int cur[maxn];//当前弧下标

    void init(int n){
        s=1, t=n;
        memset(vis,false,sizeof(vis));
        memset(dis,0,sizeof(dis));
        memset(cur,0,sizeof(cur));
        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.push_back(Edge(to,from,0,0));
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    //BFS构造层次图
    bool BFS(){
        memset(vis, 0, sizeof(vis));
        queue<int> Q;
        Q.push(s);
        dis[s] = 0;
        vis[s] = true;
        while(!Q.empty()){
            int u = Q.front();
            Q.pop();
            for(int i=0; i<G[u].size(); i++){
                Edge &e = edges[G[u][i]];
                if(!vis[e.to] && e.cap>e.flow){//只考虑残量网络中的弧
                    vis[e.to]  = true;
                    dis[e.to] = dis[u]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    //DFS找增广路
    int DFS(int u, int a){
        if(u==t || a==0) return a;
        int re=0, f;
        for(int &i=cur[u]; i<G[u].size(); i++){//从上次考虑的弧
            Edge &e = edges[G[u][i]];
            if(dis[u]+1==dis[e.to] && (f=DFS(e.to,min(a, e.cap-e.flow)))>0){
                e.flow += f;
                edges[G[u][i]^1].flow -= f;
                re += f;
                a -= f;
                if(a==0) break;
            }
        }
        return re;
    }

    int MaxFlow(int s, int t){
        this->s=s; this->t=t;
        int re=0;
        while(BFS()){
            memset(cur,0,sizeof(cur));
            re += DFS(s, INF);
        }
        return re;
    }
};
int n,m;
Dinic dinic;
int main(){
    int x, y, val;
    while(~scanf("%d%d", &m, &n)){
        dinic.init(n);
        while(m-->0){
            scanf("%d%d%d", &x, &y, &val);
            dinic.AddEdge(x,y,val);
        }
        x = dinic.MaxFlow(1,n);
        printf("%d\n", x);
    }
    return 0;
}
/*
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
6 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
3 4 10
-------
50
60
*/


3、ISAP算法


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值