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

POJ 1273 Drainage Ditches 最大流

时间:2015-08-16 16:41:47      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 62877   Accepted: 24226

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

题意:告诉m条水渠,n个点,1是起点,n是终点,求终点的最大流量。

分析:比较基础的网络流求最大流。数据比较小,直接采用一般增广路算法即标号法。


#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#define INF 99999999
const int N = 222;

using namespace std;
int n,m;
int qu[N];
int pre[N];
int cus[N][N];
int flow[N][N];

int main()
{
    while(~scanf("%d%d",&m,&n))
    {
        int a,b,c;
        memset(cus,0,sizeof cus);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            cus[a][b]+=c;//注意每个点可能有多个边连接过来,所以最大值是求和
        }

        memset(flow,0,sizeof flow);

        while(1)
        {
            for(int i=1;i<=n;i++)
                pre[i]=-2;
                pre[1]=-1;
            int mmin=INF;

            int qs=0,qe=1;
            qu[qs]=1;

            while(qs<qe && pre[n]==-2)
            {
                int v=qu[qs++];
                for(int i=1;i<=n;i++)
                {
                    int p=cus[v][i]-flow[v][i];
                    if(p && pre[i]==-2)
                    {
                        pre[i]=v;
                        qu[qe++]=i;
                        mmin=min(mmin,p);
                    }
                }
            }

            if(pre[n]==-2)break;

            for(int i=pre[n],j=n;i!=-1;j=i,i=pre[i])
            {
                flow[i][j]+=mmin;
                flow[j][i]=-flow[i][j];
            }
        }

        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=flow[i][n];

        printf("%d\n",ans);
    }

    return 0;
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1273 Drainage Ditches 最大流

标签:

原文地址:http://blog.csdn.net/wust_zjx/article/details/47703109

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