Hdu1532_drainage ditches (network stream/EK template)

Source: Internet
Author: User
Drainage ditches Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 8599 accepted submission (s): 4005


Problem descriptionevery 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 ction, but there might be a way that water can flow in a circle.
 
Inputthe input parameter des 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.
 
Outputfor 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
 
The EK template of the sourceusaco 93 solution report, not to mention.
#include <iostream>#include <cstring>#include <cstdio>#include <queue>#define inf 99999999#define N 220#define M 220using namespace std;int n,m,edge[N][N],flow,a[N],p[N];queue<int>Q;void ek(){    while(1)    {        while(!Q.empty())        Q.pop();        memset(a,0,sizeof(a));        memset(p,0,sizeof(p));        Q.push(1);        a[1]=inf;        p[1]=1;        while(!Q.empty())        {            int u=Q.front();            Q.pop();            for(int v=1;v<=n;v++)            {                if(!a[v]&&edge[u][v]>0)                {                    a[v]=min(a[u],edge[u][v]);                    p[v]=u;                    Q.push(v);                }            }            if(a[n])break;        }        if(!a[n])break;        for(int u=n;u!=1;u=p[u])        {            edge[p[u]][u]-=a[n];            edge[u][p[u]]+=a[n];        }        flow+=a[n];    }}int main(){    int i,j,u,v,w;    while(~scanf("%d%d",&m,&n))    {        flow=0;        memset(edge,0,sizeof(edge));        for(i=0;i<m;i++)        {            scanf("%d%d%d",&u,&v,&w);            edge[u][v]+=w;        }        ek();        printf("%d\n",flow);    }}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.