POJ 1273 Drainage Ditches【最大流EK算法模板题】

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

题目大意:很容易理解,求出开始点到终点的最大流。
思路: 1》:从源点出发,广搜一条最短的可行路,每次到达一点用数组pre[]记下是由哪条边来的;
    2》:到达汇点时,按照pre[]记录的从可行路径找出容量最少的容量;
    3》:重复1》, 2》, 知道无法到达汇点;
概念: 1》:点连通:N个点的图中, 去点k-1个顶点,子图仍然连通, 去点k个顶点后不连通, 则是k连通图,连通度为k;
    2》:独立轨:这能经过一次的边,即独立轨的个数等于连通度;
    3》:最小点割集:从源点到汇点至少删去多少个点使得图不连通, 这个问题就是点连通度,又叫最小点割集;
等价关系: 1》:求边的连通度给每条边赋值为1, 任意选择源点, 枚举汇点, 依次求最小割, 取其中的最小值;
      2》:求最小割要用到最大流最小割定理,即一个图的最小割等于其源点与汇点之间的最大流;
代码如下:
View Code
#include<iostream>
#include<string.h> 
#include<queue>
#include<stdio.h> 
using namespace std;
#define N 201
int MAX=0x7fffffff;
int map[N][N], flow[N], pre[N], n,m;
queue<int>Q;
int BFS(int st,int end)
{
    int i,j;
    while(!Q.empty())  
        Q.pop();
    for(i=1;i<m+1;++i)
        pre[i]=-1;
    pre[st]=0;
    flow[st]=MAX;
    Q.push(st);
    while(!Q.empty())
    {
        int cux=Q.front();
        Q.pop();
        if(cux==end)       
            break;
        for(i=1; i<m+1; ++i)
        {
            if(i!=st && map[cux][i]>0 && pre[i]==-1)
            {
                 pre[i]=cux;
                 flow[i]=min(map[cux][i],flow[cux]); 
                 Q.push(i);               
            }
        }
    } 
    if(pre[end]==-1)  
        return -1;
    else
        return flow[end];
} 
void maxFlow(int st, int end)
{
    int in=0;
    int sum=0;
    while((in=BFS(st, end))!=-1)
    {
         int k=end;          
         while(k!=st)
         {
              int last=pre[k];
              map[last][k]-=in;
              map[k][last]+=in; 
              k=last;
         }
         sum+=in;
    }    
    printf("%d\n", sum); 
}
int main()
{
    int i,j;
    int st, end, c;
    while(scanf("%d%d", &n, &m)!=EOF) 
    { 
        memset(map, 0, sizeof(map)); 
        memset(flow,0,sizeof(flow));
        for(i=0;i<n;++i)
        {
            scanf("%d%d%d", &st, &end, &c);
            map[st][end]+=c;     
        }
        maxFlow(1, m);
    }
    return 0;
}

转载于:https://www.cnblogs.com/Hilda/archive/2012/08/23/2653259.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值