POJ_P1273 Drainage Ditches(模板题+网络流)

来源:互联网 发布:大富豪源码 编辑:程序博客网 时间:2024/06/05 19:15

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 64717 Accepted: 24953

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

译文:
题目描述 Description
在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水。这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间。因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪)。作为一名一流的技师,农夫约翰已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量。

农夫约翰知道每一条排水沟每分钟可以流过的水量,和排水系统的准确布局(起点为水潭而终点为小溪的一张网)。需要注意的是,有些时候从一处到另一处不只有一条排水沟。

根据这些信息,计算从水潭排水到小溪的最大流量。对于给出的每条排水沟,雨水只能沿着一个方向流动,注意可能会出现雨水环形流动的情形。

输入描述 Input Description
第1行: 两个用空格分开的整数N (0 <= N <= 200) 和 M (2 <= M <= 200)。N是农夫John已经挖好的排水沟的数量,M是排水沟交叉点的数量。交点1是水潭,交点M是小溪。

第二行到第N+1行: 每行有三个整数,Si, Ei, 和 Ci。Si 和 Ei (1 <= Si, Ei <= M) 指明排水沟两端的交点,雨水从Si 流向Ei。Ci (0 <= Ci <= 10,000,000)是这条排水沟的最大容量。

输出描述 Output Description
输出一个整数,即排水的最大流量

#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<climits>#include<iostream>using namespace std;#define N 205struct Edge{    int fr,to,cap,flow;    Edge(int u,int v,int c,int f):fr(u),to(v),cap(c),flow(f){}};struct EdmondsKarp{    int n,m;    vector<Edge> edges;    vector<int> G[N];    int a[N],p[N];    void init(int n){        for(int i=0;i<n;i++) G[i].clear();        edges.clear();    }    void Add_Edge(int fr,int to,int cap){        edges.push_back(Edge(fr,to,cap,0));        edges.push_back(Edge(to,fr,0,0));        m=edges.size();        G[fr].push_back(m-2);        G[to].push_back(m-1);    }    int Max_Flow(int s,int t){        int flow=0;        for(;;){            memset(a,0,sizeof(a));            queue<int> Q;            Q.push(s);            a[s]=INT_MAX;            while(!Q.empty()){                int x=Q.front();Q.pop();                for(int i=0;i<G[x].size();i++){                    Edge& e=edges[G[x][i]];                    if(!a[e.to]&&e.cap>e.flow){                        p[e.to]=G[x][i];                        a[e.to]=min(a[x],e.cap-e.flow);                        Q.push(e.to);                    }                }                if(a[t]) break;            }            if(!a[t]) break;            for(int u=t;u!=s;u=edges[p[u]].fr){                edges[p[u]].flow+=a[t];                edges[p[u]^1].flow-=a[t];            }            flow+=a[t];        }        return flow;    }}f;int main(){    int x,y,z;int n,m;    while(scanf("%d%d",&n,&m)!=EOF){        f.n=n,f.init(n);        for(int i=0;i<f.n;i++){            scanf("%d%d%d",&x,&y,&z);            f.Add_Edge(x,y,z);        }        printf("%d\n",f.Max_Flow(1,m));    }    return 0;}
0 0
原创粉丝点击