POJ 1273 Drainage Ditches network flow maximum flow basis

Source: Internet
Author: User
Tags define local

Drainage ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 59176 Accepted: 22723

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie ' s favorite Clover patch. This means, the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John had built a set of drainage ditches so that Bessie ' s clover Patch was never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John have also installed regulators at the beginning of all ditch, so he can control at what Rate water flows to that ditch.
Farmer John knows not only what many gallons of water each ditch can transport per minute but also the exact layout of the Ditches, which feed out of the the pond and to each other and stream in a potentially complex network.
Given All this information, determine the maximum in which water can be transported off of the pond and into the stre Am. For any given ditch, water flows on only one direction, but there might is a-a-to-a-water can flow in a circle.

Input

The input includes several cases.For each case, the first line contains the space-separated integers, n (0 <= n <=) and M (2 <= M <= 200). N is the number of ditches this 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 would flow through this ditch from Si to Ei. CI (0 <= ci <= 10,000,000) is the maximum rate at which water would flow through the ditch.

Output

For each case, output a single integer and 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 <iostream>#include<queue>using namespacestd;intmaximum[202][202];//sideinthead[202];//record the predecessor nodes of the visited nodeintflow[202];//Updatable capacityintStard,end;//starting point, end PointintN,m;//number of edges and nodesqueue<int>Q;intBFS ()//Search for augmented roads with wide searches{     while(!q.empty ()) Q.pop ();//Empty the queue (keep the rigor of the program, or you can define local variables in the function, so you don't have to empty)    inti,t;  for(i=2; i<=m;i++) head[i]=-1;//Initialize head arrayhead[1]=0; flow[1]=0x7fffffff;    Q.push (Stard);  while(!Q.empty ()) {i=Q.front ();        Q.pop (); if(I==end) Break;  for(t=1; t<=m;t++)        {            if(t!=stard&&head[t]==-1&AMP;&AMP;MAXIMUM[I][T])//there is no access and the edge can be selected{Flow[t]=flow[i]<maximum[i][t]?flow[i]:maximum[i][t];//Select the smallest updatable capacityHead[t]=i;//record the precursor node.Q.push (t); }        }    }    if(head[end]==-1)return-1;//If the end is not visited, there is no augmented path .    returnflow[end];}voidf () {intnow,step,max_flow=0, I;  while((Step=bfs ())!=-1) {Max_flow+=step;//calculate the maximum flownow=end;  while(Now!=stard)//update the capacity of an edge{i=Head[now]; Maximum[i][now]-=step; Maximum[now][i]+=step;//increase the reverse edgenow=i; }} cout<<max_flow<<Endl;}intMain () {inti,j,k,w;  while(SCANF ("%d%d", &n,&m)!=eof)//methods to resolve input of indeterminate group data (Key Note)    {        //Initializing edges         for(i=1; i<=m;i++)             for(j=i;j<=m;j++) Maximum[j][i]=maximum[i][j]=0;  for(k=0; k<n;k++) {cin>>i>>j>>W; MAXIMUM[I][J]+=w;//focus to prevent the appearance of heavy edges} stard=1; End=m;        f (); }    return 0;}

POJ 1273 Drainage Ditches network flow maximum flow basis

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.