Zju2314 (Reactor Cooling) is used to find a feasible stream with a lower limit on the traffic without a source or sink.

Source: Internet
Author: User
/*************************************** * ** Question: A capacity network with the upper and lower limits of traffic without the Source and Sink points is given. A Feasible stream meeting the traffic balance condition is obtained. The algorithm idea is to build the accompanied network G2: (1) add additional source point S and additional sink point t; for each point statistical inflow total lower limit sum1, and outflow total lower limit sum2; find the sum1-sum2, if greater than 0 is connected to the source point, capacity is sum1-sum2; if it is less than 0, it is connected to the sink, the capacity is poor sum2-sum1; (2) Keep each arc in the original network G1, the arc capacity is changed to C-B; find the maximum stream F of the accompanied network. If all the arcs flowing from the appended source point S are fully loaded, the original network has a feasible stream; otherwise, the original network does not have a feasible stream; if a viable flow exists in the original network, the traffic of each arc is the traffic that is accompanied by this arc in the network plus the lower threshold of the traffic of the original network arc; **************************************** * **/# include <iostream> # Include <cstdio> # include <cmath> # include <cstring> # include <cstdlib> # include <algorithm> # include <queue> using namespace STD; const int maxn = 210; const int INF = 0 xffffff; // traffic upper bound struct arc {int B, C, F when no arc connection exists between vertices; // lower bound of the arc traffic, int num of the upper bound and actual traffic; // the serial number of the arc}; ARC G1 [maxn] [maxn]; // original network arc G2 [maxn] [maxn]; // accompanied network int n, m; int flag [maxn]; // vertex status:-1 unlabeled, 0: the label is not checked. 1: the label has been checked. Int pre [maxn]; // the first component of the label, indicating the vertex from which the int A [maxn] is obtained. // The second component of the label, indicating that the label can be changed Incoming aint Q; // The Header element int CMP (const void * X1, const void * x2) {return (ARC *) x1) retrieved from the queue) -> num-(ARC *) x2)-> num;} // The basic algorithm int ford_fulkerson (arc g [] [maxn], int s, int t) {While (1) // number to the unrecoverable path {queue <int> q; memset (flag,-1, sizeof (FLAG); memset (PRE, -1, sizeof (pre); memset (A,-1, sizeof (a); flag [s] = pre [s] = 0; A [s] = inf; q. push (s); While (! Q. empty () & flag [T] =-1) {q = Q. front (); q. pop (); For (INT I = s; I <= T; I ++) {If (flag [I] =-1) {If (G [Q] [I]. c <INF & G [Q] [I]. F <G [Q] [I]. c) // forward and the traffic can be increased by {flag [I] = 0; Pre [I] = Q; A [I] = min (A [Q], G [Q] [I]. c-G [Q] [I]. f); q. push (I);} else if (G [I] [Q]. c <INF & G [I] [Q]. f> G [I] [Q]. b) // reverse and the traffic can be reduced {flag [I] = 0; Pre [I] =-Q; A [I] = min (A [Q], G [I] [Q]. f-g [I] [Q]. b); q. push (I) ;}} flag [Q] = 1; // vertex Q labeled checked} If (flag [T] =-1 | A [T] = 0) // when the vertex The number of items not obtained or the number of adjustment points is 0. then exit break; int k1 = T; int k2 = ABS (pre [k1]); int a1 = A [T]; // The amount of while (1) that can be improved) {If (G [k2] [k1]. F <inf) // forward G [k2] [k1]. F = G [k2] [k1]. F + A1; else // reverse G [k1] [k2]. F = G [k1] [k2]. f-a1; If (k2 = s) // adjust to source point break; k1 = k2; k2 = ABS (pre [k2]);} // algorithm end int max_f = 0; For (INT I = s; I <= T; I ++) {for (Int J = s; j <= T; j ++) {if (I = S & G [I] [J]. F <inf) // max_f + = G [I] [J]. f; if (I = S & G [J] [I]. F <inf) // max_f-= G [J] [I]. f ;}} // Cout <"max_f =" <max_f <Endl; return max_f;} // construct the adjoint matrix and find the feasible stream int accompany () {memcpy (G2, g1, sizeof (G1); int sum1, sum2; For (INT I = 1; I <= N; I ++) {sum1 = sum2 = 0; for (Int J = 1; j <= N; j ++) // counts the arc from vertex I and the arc into vertex I {If (G2 [I] [J]. b! = Inf) sum1 + = G2 [I] [J]. B; If (G2 [J] [I]. B! = Inf) sum2 + = G2 [J] [I]. b;} If (sum2> sum1) {G2 [0] [I]. C = sum2-sum1; G2 [0] [I]. B = G2 [0] [I]. f = 0;} else {G2 [I] [n + 1]. C = sum1-sum2; G2 [I] [n + 1]. B = G2 [I] [n + 1]. f = 0 ;}}for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {If (G2 [I] [J]. c! = Inf) // modify the arc {G2 [I] [J] in the original network. C = G2 [I] [J]. c-G2 [I] [J]. b; G2 [I] [J]. B = 0 ;}}ford_fulkerson (G2, 0, n + 1); // find the largest stream bool judge = 1 with the network; // determine whether a feasible stream exists (INT I = 0; I <= n + 1; I ++) {// cout <"G2 [0] [" <I <"]. F = "<G2 [0] [I]. F <"" <"G2 [0] [" <I <"]. C = "<G2 [0] [I]. c <Endl; If (G2 [0] [I]. c! = Inf & G2 [0] [I]. F! = G2 [0] [I]. c) Judge = 0;} If (Judge = 0) // no feasible stream {puts ("no"); Return 0;} For (INT I = 1; I <= N; I ++) // modify the arc of the original network {for (Int J = 1; j <= N; j ++) {If (G1 [I] [J]. c! = Inf) G1 [I] [J]. F = G2 [I] [J]. F + G1 [I] [J]. B ;}} puts ("yes"); qsort (G1, maxn * maxn, sizeof (G1 [0] [0]), CMP); For (INT I = 0; I <m; I ++) {printf ("% d \ n", G1 [I/M] [I % m]. f) ;}} int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt", "r", stdin ); int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M ); for (INT I = 0; I <maxn; I ++) {for (Int J = 0; j <maxn; j ++) G1 [I] [J]. B = G1 [I] [J]. C = G1 [I] [J]. F = G1 [I] [J]. num = inf ;}for (INT I = 1; I <= m; I ++) {int U, V, B, C; scanf ("% d", & U, & V, & B, & C); G1 [u] [v]. B = B; G1 [u] [v]. C = C; G1 [u] [v]. f = 0; G1 [u] [v]. num = I;} accompany (); printf ("\ n");} 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.