最大流学习 hdu1532 Drainage Ditches 最大流 bfs

首先贴上原文地址:

[最大流问题]

kuangbin已经讲的很清楚了,在阅读代码的期间我再做些补充
下面是原文和我的笔记:

最近又复习了下最大流问题,每次看这部分的内容都会有新的收获。可以说最大流问题的资料网上一搜一大把,根本没有必要自己写;但是大部分资料上的专业术语太多了,初学很难理解,至少我当年学这部分的时候前几次就没有看懂。所以我准备备份一点个人的理解。
图-1 图-1
如图-1所示,在这个运输网络中,源点S和汇点T分别是1,7,各边的容量为C(u,v)。图中红色虚线所示就是一个可行流。标准图示法如图-2所示:
图-2
其中p(u,v) / c(u,v)分别表示该边的实际流量与最大容量

关于最大流

熟悉了什么是网络流,最大流也就很好理解了。就是对于任意的u∈V-{s},使得p(s,u)的和达到最大。上面的运输网络中,最大流如图-3所示:MaxFlow=p(1,2)+p(1,3)=2+1=3。
图3
在介绍最大流问题之前,先介绍几个概念:残余网络,增广路径,反向弧,最大流定理以及求最大流的Ford-Fulkerson

残余网络
增广路径
反向弧

观察下图-4,这种状态下它的残余网络如图-5所示:
图4
图5
也许现在你已经知道什么是残余网络了,对于已经找到一条从S 到T的路径的网络中,只要在这条路径上,把C(u,v)的值更新为C(u,v)-P(u,v),并且添加反向弧C(v,u)。对应的增广路径Path为残留网络上从S到T的一条简单路径。图-4中1,2,4,7就是一条增广路径,当然还有1,3,4,7。
此外在未做任何操作之前,原始的有向图也是一个残余网络,它仅仅是未做任何更新而已。

最大流定理

如果残留网络上找不到增广路径,则当前流为最大流;反之,如果当前流不为最大流,则一定有增广路径。

Ford-Fulkerson方法

介绍完上面的概念之后,便可以用Ford-Fulkerson方法求最大流了。为什么叫Ford-Fulkerson方法而不是算法,原因在于可以用多种方式实现这一方法,方式并不唯一。下面介绍一种基于广度优先搜索(BFS)来计算增广路径P的算法:Edmonds-Karp算法。

也就是每次都在题中给的图里找一条能够从源点通往汇点的路,在这记录最重流到汇点的水量。接着,把这个图上的每一条边(它代表的是该边能够流过的最大水量)减去我们现在找到的增广路径流到汇点的水量。为什么不是每个点减去上一个点流过来的水呢?因为这个边等下可能还会用到,我们要保证它利用率最高,那么只要看最后流到汇点的水量是多少就可以了。一条增光路径上的水流量是不增的。
下一步,检查是否存在其他的增广路径,重复以上过程。

算法流程如下:

设队列Q:存储当前未访问的节点,队首节点出队后,成为已检查的标点;
Path数组:存储当前已访问过的节点的增广路径;
Flow数组:存储一次BFS遍历之后流的可改进量;

  Repeat:

    Path清空;

    源点S进入Path和Q,Path[S]<-0,Flow[S]<-+∞;

    While Q非空 and 汇点T未访问 do

        Begin

            队首顶点u出对;

            For每一条从u出发的弧(u,v) do

                If v未访问 and 弧(u,v) 的流量可改进;

                Then Flow[v]<-min(Flow[u],c[u][v]) and v入队 and Path[v]<-u;

    End while



    If(汇点T已访问)

    Then 从汇点T沿着Path构造残余网络;

  Until 汇点T未被访问

应用实例

这是一道最大流的入门题,题目如下:

http://acm.pku.edu.cn/JudgeOnline/problem?id=1273

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
就不放kuangbin的代码了,因为我的是照着他的写的,下面是我的ac代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
//设队列Q:存储当前未访问的节点,队首节点出队后,成为已检查的标点;
//
//Path数组:存储当前已访问过的节点的增广路径;
//
//Flow数组:存储一次BFS遍历之后流的可改进量;
const int maxn=500;
const int INF=0x7FFFFFFF;
int map[maxn][maxn],path[maxn],flow[maxn];
int n,m,start,endd;
queue<int> que;
int bfs(){
    int i,q;
    while(!que.empty()){
        que.pop();
    }
    memset(path, -1, sizeof(path));
    path[start]=0;
    flow[start]=INF;
    que.push(start);
    while(!que.empty()){
        q=que.front();
        que.pop();
        if(q==endd){
            break;
        }
        for(i=1;i<=m;i++){
            if(i!=start&&map[q][i]&&path[i]==-1){
                flow[i]=(flow[q]<map[q][i]?flow[q]:map[q][i]);
                //cout<<flow[i]<<" "<<flow[i]<<" "<<map[q][i]<<" "<<endl;
                que.push(i);
                path[i]=q;
            }
        }
    }
    if(path[endd]==-1){
        return -1;
    }
    return flow[endd];
}
int Edmonds_Karp(){
    int result=0,a_flow=0,now,pre;
    while(bfs()!=-1){
        a_flow=bfs();
        result+=a_flow;
        now=endd;
        while(now!=start){
            pre=path[now];
            map[pre][now] -= a_flow;
            map[now][pre] += a_flow;
            now = pre;
        }
        //cout<<bfs()<<endl;
    }
    return result;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        int i,j,k;
        memset(map, 0, sizeof(map));
        for(i=1;i<=n;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            map[a][b]+=c;//可能两点之间有很多条流
        }
        start = 1;
        endd = m;
        printf("%d\n",Edmonds_Karp());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值