HDU1532 Drainage Ditches(网络流模板)

来源:互联网 发布:java中import的用法 编辑:程序博客网 时间:2024/05/21 16:58

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532


Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9077    Accepted Submission(s): 4261


Problem 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 41 2 401 4 202 4 202 3 303 4 10


很简单的求最大流的问题==,看代码注释


//http://acm.hdu.edu.cn/showproblem.php?pid=1532#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define mmax 220#define INF 0x7fffffffusing namespace std;int cap[mmax][mmax];  //限制容量int f[mmax][mmax];  //当前容量int p[mmax];int next[mmax]; //记录父节点int n,m;int maxflow(int s,int t){    int flow=0;    memset(f,0,sizeof(f));        while(1)    {        queue <int>qu;        memset(p,0,sizeof(p));        p[s]=INF;  //将源点赋为无穷大                qu.push(s);        while(!qu.empty())  //在残量网络中找到一条增广路        {            int out = qu.front(); qu.pop();                        if(out==t)                break;                            for(int i=1;i<=m;i++)             {                if(!p[i] && f[out][i] < cap[out][i])  //找到新节点,保证该节点的流量小于限制流量和初次被访问                 {                    next[i]=out;  //记住父节点,                    p[i]= p[out] < cap[out][i]-f[out][i]? p[out]:cap[out][i]-f[out][i]; //找该增广路中的最小残留量                    qu.push(i);                }            }        }        if(!p[t])  //如果到不了汇点t,那么跳出循环,不再继续寻找增广路            break;        for(int i=t;i!=s;i=next[i])//从汇点开始逆向更新流量        {            f[i][next[i]] -= p[t]; //更新反向流量                        f[next[i]][i] += p[t]; //更新正向流量        }        flow+=p[t];    }    return flow;}int main(){    while(scanf("%d %d",&n,&m)!=EOF)    {        int a,b,c;        memset(cap,0,sizeof(cap));        for(int i =1 ; i <= n ; i++)        {            scanf("%d %d %d",&a,&b,&c);            cap[a][b] += c;  //为了防止重边,这里使用+=        }        printf("%d\n",maxflow(1,m));    }    return 0;}


0 0