POJ 1273 Drainage ditches (network flow template)

Source: Internet
Author: User

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
Test instructions: There are now M channels, n channels can reach the point, and each channel can transport the maximum flow of water, the same channel may have different traffic, the 1th point is the source of water, the nth point for the water sinks, now ask the minimum flow from the source to the meeting point is how much.

1.edmonds-karp Shortest augmented path algorithm:
#include <stdio.h>#include<queue>#include<string.h>#include<algorithm>#defineINF 0x3f3f3f3f#defineN 210using namespacestd;intG[n][n], f[n];intN;intBFS (intStart,intEND)//Find out if you can reach a meeting point from a source point{    inti, u; Memset (F,0,sizeof(f));//The F array holds the starting point of a channelQueue<int>p;    Q.push (Start);  while(!Q.empty ()) {u=Q.front ();        Q.pop (); if(U = = End)return 1;  for(i =1; I <= N; i++)        {            if(G[u][i] &&!f[i])//If the channel still has traffic and has not traversed that point{Q.push (i); F[i]= u;//make its parent node the starting point for that channel, making it easy to add reverse edges            }        }    }    return 0;}intEK (intStart,intEnd) {    intMax =0, Min, I;  while(BFS (Start, End))//always find a meeting point not reachable{Min=INF;  for(i = End; I! = Start; i =F[i]) Min= min (min, g[f[i]][i]);//calculate the maximum amount of traffic that can be reached per path (depending on the one with the least traffic per segment)         for(i = End; I! = Start; i =F[i]) {G[f[i]][i]-=Min; G[i][f[i]]+ = Min;//Add a reverse edge} Max+=Min; }    returnMax;//Save the maximum flow to a meeting point}intMain () {intm, A, B, C, ans;  while(SCANF ("%d%d", &m, &n)! =EOF) {memset (G,0,sizeof(G));  while(m--) {scanf ("%d%d%d", &a, &b, &c); G[A][B]+ = C;//There may be duplicate channels where the maximum traffic needs to be added to ensure maximum flow to the meeting point} ans= EK (1, N); printf ("%d\n", ans); }    return 0;}

2.Dinic Fast network stream algorithm:

#include <stdio.h>#include<queue>#include<string.h>#include<algorithm>using namespacestd;Const intinf=0x3f3f3f3f;Const intn= About;intG[n][n], layer[n], N;//The layer array holds the point on the first level of the BFS searchintBFS (intStart,intEnd) {    inti, u; memset (layer,-1,sizeof(layer));//re-framing every time you query, so you need to initialize it every time.Queue<int>p;    Q.push (Start); Layer[start]=1;//let the source point be the first layer     while(!Q.empty ()) {u=Q.front ();        Q.pop (); if(U = = N)return 1;//at this point the meeting point has been reached and can no longer be searched         for(i =1; I <= N; i++)        {            if(Layer[i] = =-1&& G[u][i])//If the point is not traversed and has the maximum flow, the point layer +1{Layer[i]= layer[u]+1;            Q.push (i); }        }    }    return 0;}intDasointUintMini) {    intI, ans; if(U = = N)returnMini//If the point is a meeting point, the minimum value for this path has been found     for(i =1; I <= N; i++)    {        if(G[u][i] && layer[i] = = layer[u]+1&& (ans = DFS (i, min (Mini, g[u][i]))))//the value of ans must not be 0, which is the minimum value of the final path .{G[u][i]-= ans;//because ans=0 indicates that the point does not reach the meeting point, back to for the bad, only to reach the meeting point can be traced back here, and when ANS is the minimum valueG[i][u] + = ans;//Add reverse Edge            returnans; }    }    return 0;}intDinic (intStart,intEnd) {    intAns =0, num;  while(BFS (Start, End))//Until you reach the meeting point.    {         while(1) {num= DFS (1, INF); if(num = =0) Break;//equals 0 indicates that the meeting point cannot be reachedans+ = num;//Add the minimum value after each re-composition        }    }    returnAns//Get maximum Flow}intMain () {intm, A, B, C, ans;  while(SCANF ("%d%d", &m, &n)! =EOF) {memset (G,0,sizeof(G));  while(m--) {scanf ("%d%d%d", &a, &b, &c); G[A][B]+ = C;//prevent heavy edges} ans= Dinic (1, N); printf ("%d\n", ans); }    return 0;}

POJ 1273 Drainage ditches (network flow template)

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.