dinic算法学习(以poj1273为例)

Dinic 算法

特点:将顶点按其与源点s的最短距离分层。

Dinic算法步骤:

   

  (1)L0={s},i=0,R=V-L0; //源点标号为0,从原图定点集合去掉源点
(2)S={v|v∈R,且存在从Li某顶点到v的未饱和边};//以当前处理的点为起点,寻找一条边权值大于零的顶点
(3)若S={},则网络流已达到最大,停止;//找不到新的增广路,则达到最大流
(4)i=i+1,Li=S,R=R-Li;//把新找到的顶点入队,迭代进行
(5)若t∈S,则分层停止,否则令S={},转(2);//终点已经处理到,则可以结束,进行增广


Dinic算法是一种比较容易实现的,相对比较快的最大流算法。

求最大流的本质,就是不停的寻找增广路径。直到找不到增广路径为止。

对于这个一般性的过程,Dinic算法的优化如下:


将顶点按其与源点s的最短距离分层
(1)Dinic算法首先对图进行一次BFS,然后在BFS生成的层次图中进行多次DFS。
层次图的意思就是,只有在BFS树中深度相差1的节点才是连接的。
这就切断了原有的图中的许多不必要的连接。


(2)除此之外,每次DFS完后,会找到路径中容量最小的一条边。
在这条边之前的路径的容量是大于等于这条边的容量的。
那么从这条边之前的点,可能引发出别的增广路径。
比如说 S -> b -> c -> d -> T 是一条增广路径,容量最小的边是 b -> c。
可能存在一条 S -> b -> e -> f -> g -> T 这样的增广路径。
这样的话,在找到第一条增广路径后,只需要回溯到 b 点,就可以继续找下去了。
这样做的好处是,避免了找到一条路径就从头开始寻找另外一条的开销。
也就是再次从 S 寻找到 b 的开销。

(3)在同一次 DFS 中。如果从一个点引发不出任何的增广路径,就将这个点在层次图中抹去。

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50


//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define LL __int64
#define rep(i,a,n) for(int i=a;i<n;++i)
#define per(i,a,n) for(int i=n-1;i>=a;--i)
#define mp make_pair
#define pb push_back
#define mem(a,t) memset(a,t,sizeof(a))
#define all(v) v.begin(),v.end()
#define N 205
const int inf=0x7fffffff;
int head[N];
int level[N];
struct node
{
    int v,w,next;
}e[N*2];
int cnt;
int q[N];
void add(int u,int v,int w)
{
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
int bfs(int s,int t)
{
    int l,r,u,v;
    l=r=0;
    mem(level,0);     //构建图中从源点开始的层次图
    q[r++]=s;
    level[s]=0;
    while(l<r){
        u=q[l++];
        if(u==t)
            return 1;
        for(int i=head[u];i!=-1;i=e[i].next){
            v=e[i].v;
            if(!level[v]&&e[i].w){
                level[v]=level[u]+1; //标号为下一层次
                q[r++]=v;        //下一层次的点入队
            }
        }
    }
    return 0;  //不存在从源点到终点的增广路
}
int dfs(int u,int t,int maxf)
{
    if(u==t)
        return maxf;
    int ret=0,tmp,v;
    for(int i=head[u];i!=-1;i=e[i].next){
        v=e[i].v;
        if(level[v]==level[u]+1&&e[i].w){
            tmp=dfs(v,t,min(e[i].w,maxf-ret));
            ret+=tmp;
            e[i].w-=tmp;
            e[i^1].w+=tmp;
            if(ret==maxf)
                return ret;
        }
    }
    return ret;
}
int dinic(int s,int t)
{
    int ans=0;
    while(bfs(s,t)){
        ans+=dfs(s,t,inf);
    }
    return ans;
}
int main()
{
    int n,m,u,v,w;
    while(~scanf("%d%d",&m,&n)){
        mem(head,-1);
        cnt=0;
        rep(i,0,m){
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,0);
        }
        int ans=dinic(1,n);
        cout<<ans<<endl;
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值