Poj 1273 drainage ditches-maxflow maximum stream

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 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 stream 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.

Input

The input parameter des several cases.For each case, the first line contains two space-separated integers, n (0 <= n <= 200) 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 from Si to EI. CI (0 <= CI <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, 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
The EK algorithm is used only for the typical maximum network stream algorithm. Note that there is a duplicate edge, and the edge has a direction.
# Include <cstdio> # include <cstring> # include <iostream> # include <queue> # include <fstream> using namespace STD; const int Nmax = 202; long long C [Nmax] [Nmax]; int vis [Nmax], pre [Nmax]; int n, m; bool augmenting_path (int src, int dec) {queue <int> q; memset (VIS, 0, sizeof (VIS); q. push (SRC); vis [SRC] = 1; while (! Q. empty () {int cur = Q. front (); q. pop (); For (INT I = 1; I <= m; I ++) {If (! Vis [I] & C [cur] [I]> 0) {q. push (I); vis [I] = 1; Pre [I] = cur; if (I = dec) return 1 ;}} return 0 ;} int EK (int src, int dec) {int flow = 0, _ min; while (1) {If (! Augmenting_path (SRC, DEC) break; _ min = 1 <30; int I = Dec; while (I! = SRC) {If (_ min> C [pre [I] [I]) _ min = C [pre [I] [I]; I = pre [I];} I = Dec; while (I! = SRC) {C [pre [I] [I]-= _ min; C [I] [pre [I] + = _ min; I = pre [I];} flow + = _ min;} return flow;} int main () {While (CIN> N> m) {int Si, EI; long long ci; memset (C, 0, sizeof (c); For (INT I = 0; I <n; I ++) {CIN> SI> EI> Ci; C [Si] [ei] + = CI; // C [ei] [Si] + = CI; // edge direction} int src = 1, dec = m; cout <EK (SRC, DEC) <Endl ;} return 0 ;}

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.