Poj1273 Drainage Ditches

Source: Internet
Author: User

Drainage DitchesTime Limit: 1000 MS Memory Limit: 10000 KTotal Submissions: 49853 Accepted: 18918 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 str Eam 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 <= 2 00) 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 fr Om 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 Input5 41 2 401 4 202 4 202 3 303 4 10 Sample Output50SourceUSACO 93 the largest stream in the gray classic. I use the dinic algorithm. We can see that this algorithm is very fast, first use bfs and then use dfs, which is very effective!

#include <iostream>  #include <stdio.h>  #include <string.h>  using namespace std;  #define typec int  const typec inf=0x3f3f3f3f;  #define E 402  #define N 200  struct edge{int x,y,nxt;typec c;}bf[E];  int ne,head[N],cur[N],ps[N],dp[N],dep[N];  void addedge(int x,int y,typec c)  {      bf[ne].x=x;bf[ne].y=y;bf[ne].c=c;      bf[ne].nxt=head[x];head[x]=ne++;      bf[ne].x=y;bf[ne].y=x;bf[ne].c=0;      bf[ne].nxt=head[y];head[y]=ne++;  }  typec flow(int n,int s,int t)  {      typec tr,res=0;      int i,j,k,f,r,top;     while(1)     {         memset(dep,-1,n*sizeof(int));         for(f=dep[ps[0]=s]=0,r=1;f!=r;)         {             for(i=ps[f++],j=head[i];j;j=bf[j].nxt)             {                 if(bf[j].c&&-1==dep[k=bf[j].y])                 {                     dep[k]=dep[i]+1;ps[r++]=k;                     if(k==t)                     {                         f=r;break;                     }                 }             }         }         if(-1==dep[t])break;         memcpy(cur,head,n*sizeof(int));         for(i=s,top=0;;)         {             if(i==t)             {                 for(k=0,tr=inf;k<top;++k)                 {                     if(bf[ps[k]].c<tr)                     tr=bf[ps[f=k]].c;                   }                 for(k=0;k<top;k++)                 {                     bf[ps[k]].c-=tr,bf[ps[k]^1].c+=tr;                 }                 res+=tr;i=bf[ps[top=f]].x;             }          for(j=cur[i];cur[i];j=cur[i]=bf[cur[i]].nxt)              if(bf[j].c&&dep[i]+1==dep[bf[j].y])break;          if(cur[i])          {             ps[top++]=cur[i];             i=bf[cur[i]].y;          }          else          {              if(0==top)break;              dep[i]=-1;i=bf[ps[--top]].x;          }         }      }     return res;  }  int main()  {      int n,m,i,s,e,val;      while(scanf("%d%d",&n,&m)!=EOF)      {          memset(head,0,sizeof(head));          ne=2;          for(i=0;i<n;i++)          {              scanf("%d%d%d",&s,&e,&val);              s--,e--;              addedge(s,e,val);          }          printf("%d\n",flow(m,0,m-1));      }      return 0;  }  

 


Related Article

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.