最大流模板 hdu1532

来源:互联网 发布:嵌入式linux 快速启动 编辑:程序博客网 时间:2024/06/05 22:04

Drainage Ditches

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


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
 

Sample Output
50
#include<bits/stdc++.h>using namespace std;const int N = 1005;const int maxm = 1e5+6;const int inf = 0x3f3f3f3f;struct edg{    int u,v,w,next;}E[N*20];int cnt;int vis[N];int head[N];void Init(){    cnt=0;    memset(head,-1,sizeof(head));}void add(int u,int v,int w){    E[cnt].u=u;E[cnt].v=v;E[cnt].w=w;E[cnt].next=head[u];head[u]=cnt++;    E[cnt].u=v;E[cnt].v=u;E[cnt].w=0;E[cnt].next=head[v];head[v]=cnt++;}bool bfs(int s,int e){    memset(vis,0,sizeof(vis));    vis[s]=1;    queue<int>q;    q.push(s);    while(!q.empty())    {        int x=q.front();        q.pop();        for(int i=head[x];i!=-1;i=E[i].next)        {            int v=E[i].v,w=E[i].w;            if(!vis[v]&&w>0)            {                vis[v]=vis[x]+1;                q.push(v);            }        }    }    if(vis[e]==0)        return false;    return true;}int dfs(int s,int f,int e){    if(s==e)        return f;    int ans=0;    for(int i=head[s];i!=-1;i=E[i].next)    {        int v=E[i].v;        int w=E[i].w;        int p;        if(vis[v]==vis[s]+1&&w>0&&(p=(dfs(v,min(f,w),e))))//在跑dfs时w会改变所以w>0要判断        {            E[i].w-=p;            E[i^1].w+=p;            ans+=p;            f-=p;            if(f==0)                break;        }    }    if(ans==0)//不能跑到汇点的标记以后不用再跑了        vis[s]=-1;    return ans;}int dinic(int s,int e){    int ans=0;    while(bfs(s,e))    {        ans=ans+dfs(s,inf,e);    }    return ans;}int main(){    int m,n;    while(scanf("%d%d",&m,&n)!=EOF)    {        Init();        while(m--)        {            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            add(u,v,w);        }        int ans=dinic(1,n);        printf("%d\n",ans);    }    return 0;}


原创粉丝点击