首页 > 代码库 > POJ 1273 Drainage Ditches (网络最大流)

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 55235 Accepted: 21104

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

Source

USACO 93



n条边,m个点,1是源点,m是汇点,给出各有向边容量,求最大流。


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm 207<<2
#define maxn 207

using namespace std;

int fir[maxn],d[maxn];
int u[maxm],v[maxm],cap[maxm],flow[maxm],rev[maxm],nex[maxm];
int e_max;
int q[maxm];
int p[maxn];
int n,m;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    while (~scanf("%d %d",&n,&m))
    {
        e_max=0;
        memset(fir,-1,sizeof fir);
        for (int i=0;i<n;i++)
        {
            int e=e_max++;
            scanf("%d %d %d",u+e,v+e,cap+e);
            nex[e]=fir[u[e]];fir[u[e]]=e;rev[e]=e+1;
            e=e_max++;
            u[e]=v[e-1];v[e]=u[e-1];cap[e]=0;
            nex[e]=fir[u[e]];fir[u[e]]=e;rev[e]=e-1;
        }//建图

        int s=1,t=m,total_flow=0;
        memset(flow,0,sizeof flow);

        for (;;)
        {
            int f,r;
            memset(d,0,sizeof d);
            d[s]=INF;
            q[f=r=0]=s;
            while (f<=r)
            {
                int x=q[f++];
                for (int e=fir[x];~e;e=nex[e])
                {
                    if (!d[v[e]] && cap[e]>flow[e])
                    {
                        d[v[e]]=min(d[u[e]],cap[e]-flow[e]);
                        p[v[e]]=e;
                        q[++r]=v[e];
                    }
                }
            }//BFS找增广路

            if (d[t]==0)    break;//流量为0,无残量
            
            //更新路径上的流量
            for (int e=p[t];;e=p[u[e]])
            {
                flow[e]+=d[t];
                flow[rev[e]]-=d[t];
                if (u[e]==s)    break;
            }

            total_flow+=d[t];
        }

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


    return 0;
}