Zoj 2314 Reactor cooling (passive sink with upstream and downstream feasible streams)

Source: Internet
Author: User

Question link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 2314.

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.

Solution:

From: https://www.cnblogs.com/WABoss/p/5371871.html

In essence, it is to find a feasible stream of a capacity network with a higher or lower bound traffic, because each vertex on the capacity network with no source sink meets the traffic balance condition, that is, the inbound traffic of all points = the outbound traffic. It can be seen that the flow inside is circulating, similar to the directed graph Euler loop.

However, the method for determining a feasible network flow with a lower bound is to modify the original network according to the condition that a stream in the network flow is a sufficient and necessary condition for a feasible stream-the restriction condition and the balance condition, it is converted into a capacity network without a lower bound. It is not difficult to understand those proofs of mathematical models. For more information, see the paper "a simple method to solve Network Flow Problems with upper and lower limits of traffic".

The transformation methods seem to be quite popular. I use the following methods:

  • Set d [u] to the lower bound of vertex u's outbound edge and the lower bound of-Inbound edge to create Source and Sink points.
  • The original network's arc <u, v> capacity is set to its upper-lower bound
  • For each vertex u, if d [u] is <0, the Source Vertex is connected to the edge of the capacity-d [u], otherwise the Source Vertex is connected to the edge of the capacity d [u ].
  • Finally, if the arc related to the source point is full, a feasible stream exists, and the traffic at each edge + its lower bound in the original network is a solution.

Code

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 # include <queue> 7 # include <vector> 8 # define ll long 9 # define PII pair <int, int> 10 # define PLL pair <long, long> 11 # define rep (I, a, B) for (INT I = A; I <= B; I ++) 12 # define per (I, a, B) for (INT I = A; I> = B; I --) 13 # define fast_io IOs :: sync_with_stdio (false); cin. tie (0); 14 # define bug cout <"aaa Aaaaaaaaaaaaaaaaaaaaaaaaaaaa "<Endl; 15 # define bugc (_) cout <(# _) <" = "<(_) <Endl; 16 using namespace STD; 17 const int n = 2e2 + 5; 18 const int M = 4e4 + 5; 19 const int INF = 0x3f3f3f3f; 20 21 struct node {22 int to, next, flow; 23} edge [M * 2]; 24 25 int CNT, St, en; 26 int head [N], DEP [N], d [N], low [m]; // d [u] is the sum of the lower bound of vertex u's outbound edge and the lower bound of-Inbound edge. Low [I] records the lower bound of vertex I 27 28 void Init () {29 CNT = 2; 30 memset (Head, 0, sizeof (head )); 31 memset (D, 0, sizeof (d); 32} 33 34 void Link (int u, int V, int flow) {35 edge [CNT] = node {v, head [u], flow}; 36 head [u] = CNT ++; 37 edge [CNT] = node {u, head [v], 0}; 38 head [v] = CNT ++; 39} 40 41 int BFS () {42 memset (DEP, 0, sizeof (DEP )); 43 Dep [st] = 1; 44 queue <int> q; 45 Q. push (ST); 46 While (! Q. empty () {47 int u = Q. front (); 48 Q. pop (); 49 for (INT I = head [u]; I; I = edge [I]. next) {50 node T = edge [I]; 51 if (T. flow &&! Dep [T. to]) {52 Dep [T. to] = Dep [u] + 1; 53 Q. push (T. to); 54} 55} 56} 57 Return Dep [En]; 58} 59 60 int DFS (int u, int FL) {61 If (en = U) return FL; 62 int TMP = 0; 63 for (INT I = head [u]; I & FL; I = edge [I]. next) {64 node & t = edge [I]; 65 if (T. flow & Dep [T. to] = Dep [u] + 1) {66 int x = DFS (T. to, min (T. flow, FL); 67 If (x> 0) {68 TMP + = x; 69 FL-= x; 70 t. flow-= x; 71 edge [I ^ 1]. flow + = x; 72} 73} 74} 75 if (! TMP) Dep [u] =-2; 76 return TMP; 77} 78 79 int dinic () {80 int ans = 0; 81 While (BFS ()) {82 while (INT d = DFS (St, INF) 83 ans + = D; 84} 85 return ans; 86} 87 88 int main () {89 int T; 90 scanf ("% d", & T); 91 while (t --) {92 int n, m; 93 scanf ("% d", & N, & M); 94 Init (); 95 ST = 0, en = n + 1; 96 for (INT I = 1; I <= m; I ++) {97 int U, V, C; 98 scanf ("% d", & U, & V, & low [I], & C ); 99 Link (u, v, C-low [I]); 100 d [u] + = low [I]; 101 d [v] -= Low [I]; 102} 103 int sum = 0; 104 for (INT I = 1; I <= N; I ++) {105 if (d [I] <0) Link (St, I,-d [I]); 106 else {107 sum + = d [I]; 108 Link (I, en, d [I]); 109} 110} 111 If (sum! = Dinic () puts ("no"); 112 else {113 puts ("yes"); 114 for (INT I = 2; I <= 2 * m; I + = 2) {115 printf ("% d \ n", edge [I ^ 1]. flow + low [I> 1]); 116} 117} 118 puts (""); 119} 120 return 0; 121}

 

Zoj 2314 Reactor cooling (passive sink with upstream and downstream feasible streams)

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.