POJ 1273 Drainage Ditches my first network stream-Maximum Flow Problem

Source: Internet
Author: User

The problem is not to mention the biggest flow problem in the network flow is a bare, you can use the augmented circuit algorithm to do, that is, the EDMONDS-KARP algorithm. Multiple groups of data will be read. Each group has N and M first. N indicates the number of edges in the graph, M indicates the number of nodes in the graph, and N rows of data are followed. Each row has three data sets (S, e, C indicates the start point, end point, and edge capacity of an edge. Note that there is a directed edge and a duplicate edge;

Let's talk about this algorithm. It is actually very violent .. Every time he finds a path with BFS, this makes the residual traffic on each side of the road (** that is, the capacity and traffic (** that is, the actual used capacity has positive and negative, if the traffic from A to B is c, then the traffic from B to A is-c because it is in the opposite direction, right? **) The difference is greater than 0 (*******, where the residual traffic is the remaining available traffic on the edge after use, yes. For example, the capacity from v to u is c, the capacity from u to v is zero, and the traffic from v to u is F, the traffic from u to v is-F, so the residual traffic from v to u is c-F, and the residual traffic from u to v is 0-(-F) = F (************* the residual traffic actually increases, right? Because we couldn't ship things from U to V, but after you ship things from v to u, I can think that things can be shipped from u to v. Why? It's actually very easy to think about, because if you want to ship five things from v to u, but you actually only run two things, then I can say that, you run five things from v to u and three things from u to v. Is the result equivalent ???? Yes, this is the magic of residual traffic ***********). You must be clear about these concepts and know how to calculate them *****)

Ah, there seems to be a slight amount of comments. Let's move on to that and find a path with the residual traffic greater than 0. What does this mean? This path? That is, what can be shipped on this road? How many items can be shipped? Of course it's the smallest residue on this road, right? It's actually very easy to think about. If it's not the smallest, then when you ship things to the edge of the smallest residual traffic, he will certainly not be able to run it.

In this way, you can know how much traffic is to be transported. During the operation, you can update the traffic on the entire road. Pay attention to the need to update both the positive and negative traffic.

Then, repeat the process of finding the path. Until the minimum residual traffic of the entire path is greater than 0, the algorithm ends.

 

/******* PRO: POJ rj3tit: Drainage DitchesDAT: 2013-08-12AUT: UKeanEMA: huyocan@163.comALG: edmonds_Karp *********/# include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <queue> # define INF 1e9using namespace std; queue <int> que; // The queue int N and M to be used for extensive search; // N indicates the number of edges, and M indicates the number of points int s and t; // source point and sink point int flow [205] [205]; // traffic int p [205]; // The parent node array of the wide search record path int a [205]; // The minimum residual traffic in the path int cap [205] [205]; // The capacity network int ans; int Read () {memset (cap, 0, sizeof (cap); for (int I = 0; I <N; I ++) {int sta, en, ca; cin> sta> en> ca; cap [sta] [en] + = ca; // You can process the duplicate edge} s = 1; t = M; // s is the Source Vertex, t is the return 1;} void deal () // maximum stream algorithm {memset (flow, 0, sizeof (flow )); // initialization traffic ans = 0; // maximum stream while (1) {memset (a, 0, sizeof (a); a [s] = INF; que. push (s); while (! Que. empty () // elasticsearch finds the augmented path, that is, the path with the minimum residual traffic greater than zero {int u = que. front (); que. pop (); for (int v = 1; v <= M; v ++) if (! A [v] & cap [u] [v]-flow [u] [v]> 0) // cap [u] [v]-flow [u] [v] indicates residual traffic. // expand a new node! A [v] ensures that the node has not been accessed, and the residual traffic is greater than 0. Make sure that the path to be found meets the requirements {p [v] = u; // records the father of v, save path que. push (v); // a [v] = min (a [u], cap [u] [v]-flow [u] [v]); // record the minimum residual traffic on the path} if (a [t] = 0) break; // if the path with the minimum residual traffic greater than 0 cannot be found, end the algorithm for (int u = t; u! = S; u = p [u]) // iterative retrieval path modification. traffic on the path {flow [p [u] [u] + = a [t]; // update forward traffic flow [u] [p [u]-= a [t]; // update direction traffic} ans + = a [t]; // Add the maximum stream} cout <ans <endl;} int main () {while (cin> N> M) {read (); deal ();} return 0 ;}

 

Related Article

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.