(Maximum stream-simple question) poj 1273 drainage ditches

Source: Internet
Author: User
Drainage ditches
Time limit:1000 ms   Memory limit:10000 K
Total submissions:24043   Accepted:8706

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
/* Question: a farmer has dug many troughs for drainage. 1 is pond, and m is stream. Q: the question of "maximum displacement" is purely about the maximum flow. PS: The first time I got confused, I copied the reverse side, causing tangle. Another point of attention to this question is that parallel edges may be generated. Source codeproblem: 1273 User: wawadimu memory: 564 K time: 16 Ms language: C ++ result: accepted source code */# include <iostream> # include <queue> using namespace STD; # define maxn 220 # define INF int_max/* struct node {int U, V ;} E [maxn]; */INT n, m; // Number of edges, number of vertices // int first [maxn], next [maxn]; int flow [maxn] [maxn], Cap [maxn] [maxn]; int A [maxn], pre [maxn]; int maxflow (int s, int T) {int max = 0; while (1) {memset (A, 0, sizeof (a); [S] = inf; queue <int> q; q. Push (s); While (! Q. empty () {int u = Q. front (); q. pop (); For (INT v = 1; v <= m; V ++) {If (! A [v] & Cap [u] [v]> flow [u] [v]) {pre [v] = u; A [v] = A [u]> CAP [u] [v]-flow [u] [v]? Cap [u] [v]-flow [u] [v]: A [u]; q. Push (v) ;}} if (! A [T]) break; For (INT x = T; X! = S; X = pre [x]) {flow [pre [x] [x] + = A [T]; flow [x] [pre [x]-= A [T];} // cout <max <Endl; Max ++ = A [T];} return Max;} int main () {// freopen ("Release 3.txt"," r ", stdin); int S, T; int I, j; int C, TMP, U, v; while (scanf ("% d", & N, & M )! = EOF) {memset (Cap, 0, sizeof (CAP); memset (flow, 0, sizeof (flow); for (I = 1; I <= N; I ++) {scanf ("% d", & U, & V); scanf ("% d", & C ); cap [u] [v] + = C; // multiple edges} s = 1; t = m; int ans = maxflow (S, T ); printf ("% d/N", ANS);} 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.