码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1273 Drainage Ditches 网络流最大流基础

时间:2015-03-04 20:55:41      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

 

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 59176   Accepted: 22723

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<iostream>
#include<queue>
using namespace std;

int maximum[202][202];//
int head[202];//记录访问过的节点的前驱结点
int flow[202];//可更新容量

int stard,end;//起点,终点
int n,m;//边数和节点数
queue<int>q;

int bfs()//用广搜搜索增广路
{
    while(!q.empty())q.pop();//清空队列(保持程序的严谨性,也可以在函数中定义局部变量,就不用清空了)
    int i,t;
    for(i=2;i<=m;i++)head[i]=-1;//初始化head数组
    head[1]=0;
    flow[1]=0x7fffffff;
    q.push(stard);
    while(!q.empty())
    {
        i=q.front();
        q.pop();
        if(i==end)break;
        for(t=1;t<=m;t++)
        {
            if(t!=stard&&head[t]==-1&&maximum[i][t])//没访问过且有则边可以选取
            {
                flow[t]=flow[i]<maximum[i][t]?flow[i]:maximum[i][t];//选择最小的可更新容量
                head[t]=i;//记录前驱结点
                q.push(t);
            }
        }
    }
    if(head[end]==-1)return -1;//如果终点没访问则无增广路
    return flow[end];
}
void f()
{
    int now,step,max_flow=0,i;
    while((step=bfs())!=-1)
    {
        max_flow+=step;//计算最大流
        now=end;
        while(now!=stard)//更新边的容量
        {
            i=head[now];
            maximum[i][now]-=step;
            maximum[now][i]+=step;//增加反向边
            now=i;
        }
    }
    cout<<max_flow<<endl;
}

int main()
{
    int i,j,k,w;
    while(scanf("%d %d",&n,&m)!=EOF)//解决输入不定组数数据的方法(重点注意)
    {
        //初始化边
        for(i=1;i<=m;i++)
            for(j=i;j<=m;j++)
                maximum[j][i]=maximum[i][j]=0;

        for(k=0;k<n;k++)
        {
            cin>>i>>j>>w;
            maximum[i][j]+=w;//重点,防止重边的出现
        }
        stard=1;
        end=m;
        f();    
    }
    return 0;
}

 

poj 1273 Drainage Ditches 网络流最大流基础

标签:

原文地址:http://www.cnblogs.com/qscqesze/p/4314220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!