POJ 1273 Drainage ditches (network flow max stream Base Edmonds_karp algorithm)

Source: Internet
Author: User


Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 59219 Accepted: 22740

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

Source

usaco

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

Topic: m ponds (1 as source, m as meeting point) n Canal, gives this n channel connected to the pond and the amount of water that can flow maximum capacity of water flowing through canals

Topic Analysis: Basic network Flow maximum flow problem, using EDMONDS_KARP algorithm, details see program comments

#include <cstdio> #include <cstring> #include <queue>using namespace std;int const MAX = 205;int m, n;int    C[max][max];    Edge capacity int F[max][max];       Edge actual flow int Pre[max];         Record the path of the augmented road int A[max];    Residual network int edmonds_karp (int s, int t) {int ans = 0;    Queue <int> q;        while (true) {memset (A, 0, sizeof (a)); A[s] = Int_max;                  The source point label is set to infinite large q.push (s);            while (!q.empty ())//using BFS to find the augmented path {int u = q.front ();            Q.pop (); for (int v = 1; v <= N; v++) {//A point is not augmented and the capacity of the point is greater than the flow if (!a[v] && C[u][v] & Gt                    F[u][v]) {Q.push (v); The maximum amount of improvement on the S-V path is obtained, i.e. minimum residue a[v] = A[u] < C[u][v]-f[u][v]?                    A[u]: c[u][v]-f[u][v];                PRE[V] = u;        }}//If the augmentation path is not found, exit if (a[t] = = 0) break; Go from the meeting point along the recorded augmented path to the source point for (int u = t; U! = S;   u = pre[u]) {F[pre[u]][u] + = a[t];   Update forward traffic f[u][pre[u]]-= a[t];    Update reverse Traffic} ans + = a[t]; Plus the amount of improvement} return ans;}    int main () {int u, V, W;        while (scanf ("%d%d", &m, &n)! = EOF) {memset (c, 0, sizeof (c));        memset (f, 0, sizeof (f));            Initializes the capacity network, starting with zero flow for (int i = 0; i < m; i++) {scanf ("%d%d%d", &u, &v, &w);   C[U][V] + = W;    There may be heavy edges, need to overlay} printf ("%d\n", Edmonds_karp (1, N)); }}


POJ 1273 Drainage ditches (network flow max stream Base Edmonds_karp algorithm)

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.