HDU--杭电--1532--Drainage Ditches--最大流

来源:互联网 发布:mt管理器源码 编辑:程序博客网 时间:2024/06/05 19:07

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7486    Accepted Submission(s): 3524


Problem 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 41 2 401 4 202 4 202 3 303 4 10
 

Sample Output
50

题意:就是一个池塘里面的水流到小溪吧(其实我也不懂),告诉你沟与沟之间的过道数n和沟的数目m,然后告诉你每条过道的信息,要你求出从1到m的最大流


#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define Max 222using namespace std;int n,m,vis[Max],f[Max],v[Max],map[Max][Max];
//vis是标记数组,f是前驱,v是最小值,map是地图int EK(){    int i,j,k,l,cur,sum=0,mm,flag;
//cur是当前沟的号码,sum是最大流的值,flag是标记    while(true)    {        queue<int> q;//定义一个队列        memset(vis,0,sizeof(vis));        memset(f,0,sizeof(f));//初始化        v[1]=1<<30;//起点的最小值初始化为无穷大        q.push(1);        flag=1;        while(q.size()&&flag)//队列不为空且标记未找到路线        {            cur=q.front();//取当前点            q.pop();            for(i=2;i<=m;i++)//遍历所有点            if(!vis[i]&&map[cur][i])//标记未访问且有此路线            {                q.push(i);//入队                vis[i]=1;//标记已访问                f[i]=cur;//记录前驱                v[i]=min(map[cur][i],v[cur]);//最小值取当前路线与前驱之间的最小值                if(i==m){flag=0;break;}//如果找到终点就标记且结束            }        }        if(flag)return sum;//没找到路线就结束        for(i=m;i!=1;i=f[i])//找到路线就更新残余网络        {            map[f[i]][i]-=v[m];//正方向的减去最小值            map[i][f[i]]+=v[m];//反方向的加上最小值        }        sum+=v[m];//加上当前流    }}int main (void){    int i,j,k,l;    while(~scanf("%d%d",&n,&m))    {        memset(map,0,sizeof(map));        while(n--&&scanf("%d%d%d",&j,&k,&l))        map[j][k]+=l;        printf("%d\n",EK());    }    return 0;}

0 0
原创粉丝点击