Poj 1273 drainage ditches (network stream-maximum Stream)

Source: Internet
Author: User

// Network stream -- maximum stream <br/> // The condition for termination is that the path cannot be added to the residual network. <br/> // you need to consider multiple sides. <br/> # include <iostream> <br/> # include <queue> <br/> # include <cstring> <br/> # define INF 2000000000 <br/> using namespace STD; <br/> int n, m, U, V, X, Y, C, St, Ed; <br/> int flow [205] [205], cap [205] [205]; // current traffic, maximum capacity <br/> int rflow [205], pre [205]; // rflow indicates the residual traffic of each node, by the way, when the VIS array <br/> int maxflow; <br/> int main () <br/> {<br/> // freopen ("in.txt", "r ", stdin); <br/> whi Le (scanf ("% d", & M, & N )! = EOF) <br/>{< br/> maxflow = 0; <br/> ST = 1; <br/> ED = N; <br/> memset (flow, 0, sizeof (flow); <br/> memset (Cap, 0, sizeof (CAP); <br/> for (INT I = 0; I <m; + + I) <br/>{< br/> scanf ("% d", & X, & Y, & C ); <br/> CAP [x] [Y] + = C; // as multiple edges appear in the question, it is easier to save them using the adjacent matrix, so use + = to overlay the maximum traffic <br/>}< br/> for (;) <br/>{< br/> queue <int> q; <br/> memset (rflow, 0, sizeof (rflow); <br/> rflow [st] = inf; // set the starting stream to unlimited. <br/> q. push (ST); <br/> while (! Q. empty () <br/>{< br/> U = Q. front (); <br/> q. pop (); <br/> for (V = 1; v <= N; ++ v) <br/> {<br/> If (! Rflow [v] & Cap [u] [v]> flow [u] [v]) // if the residual traffic is equal to 0 (it indicates that it has not been updated) if the current capacity is greater than the current traffic, the change point can be expanded <br/>{< br/> pre [v] = u; // The precursor pointer record <br/> q. push (V); // Add to the queue <br/> rflow [v] = min (rflow [u], cap [u] [v]-flow [u] [v]); // The residual traffic at this point is (the residual traffic at the inbound point) and (the difference between the current capacity and the current traffic) <br/>}< br/> <br/> If (rflow [ed] = 0) break; // unable to find the augmented path to the settlement point, jump out <br/> for (u = Ed; u! = ST; u = pre [u]) // update traffic back from the sink <br/>{< br/> flow [pre [u] [u] + = rflow [ed]; // update forward traffic <br/> flow [u] [pre [u]-= rflow [ed]; // update reverse traffic <br/>}< br/> maxflow + = rflow [ed]; // update the total maximum stream <br/>}< br/> printf ("% d/N", maxflow); <br/>}< br/> return 0; <br/>} 

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.