Acdream 1211 Reactor Cooling [upstream and downstream network stream + output traffic], acdreamcooling

Source: Internet
Author: User

Acdream 1211 Reactor Cooling [upstream and downstream network stream + output traffic], acdreamcooling

Title: acdream 1211 Reactor Cooling


Category: upstream and downstream network streams without Sink.


Question:

For n points and m pipe, each pipe is used to lay the liquid. One-way, each pipe is equal to the substance that flows in every moment, to make m pipe into a circular body, the stream is lying in the material.

The traffic limit for each pipe is also met. The range is [Li, Ri]. that is, the number of streams that come in at each time must not exceed Ri (the maximum flow problem), and the minimum value cannot be lower than Li.

For example:

46 (4 points, 6 pipe)
12 1 3 (1-> 2, the upper bound is 3, and the lower bound is 1)
23 1 3
3 4 1 3
4 1 1 3
1 3 1 3
4 2 1 3

Feasible stream:

 


For another example, if the upper bound of all pipe is 2 and the lower bound is 1, a feasible stream cannot be obtained.

 

Question:

The upper bound is represented by ci, and the lower bound is represented by bi.

The lower bound must be full. For each edge, after the lower bound is removed, the free stream is ci-bi.

Main Idea: the stream that comes in at each point = the stream that goes out

For each vertex I

Mi = sum (lower-bound stream of all streams from point I)-sum (lower-bound stream of all streams from point I)

If Mi is greater than 0, it indicates that the point must also flow out of the free stream of Mi, then we connect a Mi edge from the source point to the point.

If Mi is smaller than 0, it indicates that the point must also flow into the free stream of Mi, then we connect a Mi edge to the sink point from the point.

If you want to find the maximum stream of S-> T, check whether the stream is full (the adjacent edge of S is full ).

If the stream is full, there is a solution; otherwise, there is no solution.

 


 


AC code:

# Include <cstdio> # include <cstring> # include <string> # include <iostream> # include <algorithm> # include <vector> # include <map> # include <queue> # define Del (, b) memset (a, B, sizeof (a) using namespace std; const int inf = 0x3f3f3f3f; const int N = 250; struct Node {int from, to, cap, flow ;}; vector <int> v [N]; vector <Node> e; int vis [N]; // build a layered graph int cur [N]; void add_Node (int from, int to, int cap) {e. push_back (Node) {fro M, to, cap, 0}); e. push_back (Node) {to, from, 0, 0}); int tmp = e. size (); v [from]. push_back (tmp-2); v [to]. push_back (tmp-1);} bool bfs (int s, int t) {Del (vis,-1); queue <int> q; q. push (s); vis [s] = 0; while (! Q. empty () {int x = q. front (); q. pop (); for (int I = 0; I <v [x]. size (); I ++) {Node tmp = e [v [x] [I]; if (vis [tmp. to] <0 & tmp. cap> tmp. flow) // The second condition ensures {vis [tmp. to] = vis [x] + 1; q. push (tmp. to) ;}}} if (vis [t]> 0) return true; return false;} int dfs (int o, int f, int t) {if (o = t | f = 0) // returns f; int a = 0, ans = 0; for (int & I = cur [o]; I <v [o]. size (); I ++) // note the preceding '&', which is an important optimization {Node & tmp = e [v [o] [I]; if (vis [tmp. to] = (Vis [o] + 1) & (a = dfs (tmp. to, min (f, tmp. cap-tmp.flow), t)> 0) {tmp. flow + = a; e [v [o] [I] ^ 1]. flow-= a; // save graph mode ans + = a; f-= a; if (f = 0) // note break optimization;} return ans; // optimization} int dinci (int s, int t) {int ans = 0; while (bfs (s, t) {Del (cur, 0 ); int tm = dfs (s, inf, t); ans + = tm;} return ans;} void MP_clear (int n) {for (int I = 0; I <= n; I ++) v [I]. clear (); e. clear () ;}int come [N], to [N]; int flow [N] [N]; struct Node1 {int x, y;} n Um [N * N]; int main () {int n, m; int T; scanf ("% d", & T); while (T --) {scanf ("% d", & n, & m); Del (come, 0); Del (to, 0); Del (flow, 0 ); int s = 0, t = n + 1; for (int I = 0; I <m; I ++) {int x, y, mi, ma; scanf ("% d", & x, & y, & mi, & ma); num [I] = (Node1) {x, y }; flow [x] [y] + = mi; add_Node (x, y, ma-mi); come [x] + = mi; to [y] + = mi ;} int count = 0; for (int I = 1; I <= n; I ++) {int tmp = come [I]-to [I]; if (tmp <0) {count + = tmp; add_Node (s, I ,-Tmp);} if (tmp> 0) add_Node (I, t, tmp);} count =-count; int ans = dinci (s, t ); if (ans! = Count) puts ("NO"); else {puts ("YES"); for (int I = 1; I <= n; I ++) {for (int j = 0; j <v [I]. size (); j ++) {int f = v [I] [j]; flow [e [f]. from] [e [f]. to] + = abs (e [f]. flow); // printf ("xx % d \ n", e [f]. from, e [f]. to, e [f]. flow) ;}}for (int I = 0; I <m; I ++) {printf ("% d \ n", flow [num [I]. x] [num [I]. y]) ;}} if (T) puts (""); MP_clear (t);} 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.