Drainage Ditches

来源:互联网 发布:淘宝直通车在哪开通 编辑:程序博客网 时间:2024/06/14 07:29

题目描述

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. 

输入描述:

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.       

输出描述:

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

输入

5 41 2 401 4 202 4 202 3 303 4 10

输出

50
#include<iostream>#include<cstring>#include<queue>using namespace std;const int MAXM=203;int capacity[MAXM][MAXM];int flow[MAXM];int pre[MAXM];int n,m;int bfs(){    memset(pre,0,sizeof(pre));    flow[1]=11000000;    queue<int> q;    q.push(1);    while(!q.empty())    {        int s=q.front();        q.pop();        for(int i=2;i<=m;++i)            if(capacity[s][i]>0&&pre[i]==0)            {                flow[i]=min(flow[s],capacity[s][i]);                pre[i]=s;                if(i==m)                    return flow[m];                q.push(i);            }    }    return 0;}int maxFlow(){    int increase;    int sumflow=0;    while(increase=bfs())    {        sumflow+=increase;        for(int i=m;i;i=pre[i])        {            capacity[pre[i]][i]-=increase;            capacity[i][pre[i]]+=increase;        }    }    return sumflow;}void show(){    for(int i=1;i<=m;++i)    {        for(int j=1;j<=m;++j)            cout<<capacity[i][j]<<" ";        cout<<endl;    }}int main(){    int a,b,c;    while(cin>>n>>m)    {        memset(capacity,0,sizeof(capacity));        for(int i=1;i<=n;++i)        {            cin>>a>>b>>c;            capacity[a][b]+=c;        }        cout<<maxFlow()<<endl;    }    return 0;}


原创粉丝点击