POJ 1273 Drainage Ditches

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 40193   Accepted: 14964

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

 
//昨天的11级暑期训练赛上见到这题、一直是从没接触过的、就把前些天在lrj白皮书上的模板搬上去,
//这算是我的第一个网络流算法了、昨晚再看了下算法导论,今天自己再写一下
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#define V 203
#define inf 10000003
using namespace std;
int main()
{
    int N,M;
    int f[V][V],c[V][V];
    int fa[V],r[V];// r 数组记录残留网络的单边最小流,fa数组记录父亲节点、供回溯时更新流使用
    __int64 sum;
    int i,u,v,va;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        memset(f,0,sizeof(f));
        memset(c,0,sizeof(c));
        for(i=0;i<N;i++)
        {
           scanf("%d%d%d",&u,&v,&va);
           c[u][v]+=va;//昨天开始直接上模板,被多重变坑了下、唉!要给裸模板给新手呀
        }sum=0;
        for(;;)
        {
            memset(r,0,sizeof(r));
            queue<int> Q;
            r[1]=inf;
            Q.push(1);
            while(!Q.empty())
            {
                u=Q.front();Q.pop();
                for(v=2;v<=M;v++)
                if(!r[v]&&c[u][v]>f[u][v])//r数组兼职,用来确定BFS中某个点是否被用过
                {
                    r[v]=r[u]<=c[u][v]-f[u][v]?r[u]:c[u][v]-f[u][v];
                    fa[v]=u;
                    Q.push(v);
                }
            }
            if(r[M]==0)
             break;
           for(u=M;u!=1;u=fa[u])//回溯时更新流
           {
               f[fa[u]][u]+=r[M];
               f[u][fa[u]]-=r[M];
           }
           sum+=r[M];
        }
        printf("%I64d\n",sum);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/372465774y/p/2585750.html