poj1273 Drainage Ditches (最大流)

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39519   Accepted: 14695

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;

const int MAX = 300;
const int INF = INT_MAX;

int map[MAX][MAX];
int flow[MAX];
int father[MAX];

int m, n;

int bfs()
{
    queue<int> q;
    memset(father, -1, sizeof(father));
    memset(flow, 0, sizeof(flow));
    father[1]=1;
    flow[1]=INF;
    q.push(1);
    while(!q.empty())
    {
        int k=q.front();
        q.pop();
        if(k==m)
            break;
        for(int i=1;i<=m;i++)
        {
            if(i!=1 && father[i]==-1 && map[k][i]!=0)
            {
                if(map[k][i]<flow[k])
                    flow[i]=map[k][i];
                else
                    flow[i]=flow[k];
                father[i]=k;
                q.push(i);
            }
        }
    }
    if(father[m]==-1)
        return -1;
    else
        return flow[m];
}

int ek()
{
    int mmax = 0;
    int res;
    int now, pre;
    while((res=bfs())!=-1)
    {
        mmax+=res;
        now=m;
        while(now!=1)
        {
            pre = father[now];
            map[now][pre]+=res;
            map[pre][now]-=res;
            now=pre;
        }
    }
    return mmax;
}

int main()
{
    freopen("e:\\data.txt", "r", stdin);   
    freopen("e:\\out.txt", "w", stdout);
    while(cin>>n>>m)
    {
        memset(map, 0, sizeof(map));
        while(n--)
        {
            int a, b;
            int temp;
            cin>>a>>b>>temp;
            map[a][b]+=temp;
        }
        cout<<ek()<<endl;
    }
    return 0;
}

 

posted @ 2012-06-05 10:39  w0w0  阅读(140)  评论(0编辑  收藏  举报