HDU 1532 Drainage Ditches(最大流+EK算法模板题)

来源:互联网 发布:网络验证系统哪个好 编辑:程序博客网 时间:2024/06/04 20:03

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
Sample Output
50

题解:

源点为1,汇点为n,给num个流,让你求最大流。。。EK模板题目,虽然网络流刚刚入门的我不太懂但是直接按着紫书上的模板打了一遍,修改了些就ac了

模板会用就行了qwq

代码:

#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<vector>#include<deque>#include<algorithm>using namespace std;#define INF 100861111#define ll long long#define eps 1e-5#define maxn 205struct edge//存边{    int from,to,cap,flow;//cap为容量,flow为流量    edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}};vector<edge>ed;vector<int>G[maxn];//存第i个点连接的第j条边的标号int a[maxn];//存起点到i的可改进量。。不太懂,照着紫书打的int p[maxn];//最短路树上p的入弧编号int m,n;void init(){    for(int i=1;i<=n;i++)        G[i].clear();    ed.clear();}void addEdge(int from,int to,int cap){    ed.push_back(edge(from,to,cap,0));    ed.push_back(edge(to,from,0,0));//反向加边    m=ed.size();    G[from].push_back(m-2);    G[to].push_back(m-1);}int Maxflow(int s,int t)//s为源点,t为汇点{    int flow=0;    while(1)    {        memset(a,0,sizeof(a));        queue<int>q;        q.push(s);        a[s]=INF;        while(!q.empty())        {            int x=q.front();            q.pop();            for(int i=0;i<G[x].size();i++)            {                edge &e=ed[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 e=t;e!=s;e=ed[p[e]].from)        {            ed[p[e]].flow+=a[t];            ed[p[e]^1].flow-=a[t];        }        flow+=a[t];    }    return flow;}int main(){    int i,j,x,y,c,num;    while(scanf("%d%d",&num,&n)!=EOF)    {        init();        for(i=0;i<num;i++)        {            scanf("%d%d%d",&x,&y,&c);            addEdge(x,y,c);        }        printf("%d\n",Maxflow(1,n));    }    return 0;}



阅读全文
0 0