POJ 1273 Drainage ditches (maximum flow)

Source: Internet
Author: User

http://poj.org/problem?id=1273

Drainage ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 62708 Accepted: 24150

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

Main topic: M edge, each edge has a flow value, n points, the maximum flow of 1 to N

Dinic templates:
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<queue>#include<algorithm>#defineN 210#defineINF 0x3f3f3f3fusing namespacestd;intG[n][n], vis[n], layer[n];intN;BOOLBFS ()//Tiered Processing{deque<int>Q;//defining a double-ended queuememset (Layer,-1,sizeof(layer)); Q.push_back (1);//source point in queue (one element x is added to the tail of the double-ended queue)layer[1] =1;//Mark Source Point     while(!Q.empty ()) {        intU =Q.front (), I; Q.pop_front ();//Delete the first element in a double-ended queue         for(i =1; I <= N; i++)//traverse points to determine if they can be layered        {            if(G[u][i] >0&& Layer[i] = =-1)//when u point to I point this edge has flow and I point has not been layered{Layer[i]= Layer[u] +1;//The I-point is layered                if(i = = N)//layering succeeds when meeting point is reached                    return true; ElseQ.push_back (i);//otherwise continue            }        }    }    return false;}intDinic () {intMaxflow =0;  while(BFS () = =true) {deque<int>p; memset (Vis,0,sizeof(VIS)); Q.push_back (1); vis[1] =1;  while(!Q.empty ()) {            intv =Q.back (), I; if(V! =N) { for(i =1; I <= N; i++)                {                    if(G[v][i] >0&& Layer[v] +1= = Layer[i] &&!vis[i])//if v to I has flow and I point is the augmented path of the V point and point I has not been accessed{Vis[i]=1; Q.push_back ((i));//point I enters the queue (that is, point I is a point on the augmented road)                         Break; }                }                if(i > N)//if no augmented path is found at the next level after traversing all points, exit this layer and continue looking for the nextQ.pop_back ();//Delete the last element in a double-ended queue}//find an augmented road            Else            {                intMinflow =INF, NV; intLen = Q.size ();//Enter the number of points in the queue                 for(inti =1; i < Len; i++)                {                    intx = Q[i-1];//a previous point                    inty = q[i];//after a point                    if(Minflow >G[x][y]) {Minflow=G[x][y]; NV= x;//NV Record front-end points}//Find minimum value to honor traffic} maxflow+=Minflow;  for(inti =1; i < Len; i++)//Update Traffic                {                    intx = Q[i-1]; inty =Q[i]; G[x][y]-= Minflow;//Update forwardG[Y][X] + = Minflow;//Reverse Increase                }                 while(! Q.empty () && q.back ()! =NV) Q.pop_back ();//out Team            }        }    }    returnMaxflow;}intMain () {intA, B, C, M;  while(~SCANF ("%d%d", &m, &N)) {memset (G,0,sizeof(G));  while(m--) {scanf ("%d%d%d", &a, &b, &c); G[A][B]+ = C;//Handling Heavy Edges} printf ("%d\n", Dinic ()); }    return 0;}/*2 101 3 102 4 202 5 203 6 203 7 204 8 305 8 306 8 307 8*/
View Code

POJ 1273 Drainage ditches (maximum 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.