Description
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear reactor to produce plutonium for the nuclear bomb they are planning to create. Being the wicked computer genius of this group, you are responsible for developing the cooling system for the reactor.
The cooling system of the reactor consists of the number of pipes that special cooling liquid flows by. Pipes are connected at special points, called nodes, each pipe has the starting node and the end point. The liquid must flow by the pipe from its start point to its end point and not in the opposite direction.
Let the nodes be numbered from \(1\) to \(N\). The cooling system must be designed so that the liquid is circulating by the pipes and the amount of the liquid coming to each node (in the unit of time) is equal to the amount of liquid leaving the node. That is, if we designate the amount of liquid going by the pipe from \(i-th\) node to \(j-th\) as \(f_{ij}\), (put \(f_{ij}= 0\) if there is no pipe from node \(i\) to node \(j\)), for each \(i\) the following condition must hold: \[f_{i,1}+f_{i,2}+\cdots +f_{i,N} = f_{1,i}+f_{2,i}+\cdots + f_{N,i}\]
Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be \(f_{ij} \le c_{ij}\) where \(c_{ij}\) is the capacity of the pipe. To provide sufficient cooling, the amount of the liquid flowing by the pipe going from i-th to j-th nodes must be at least \(l_{ij}\), thus it must be \(f_{ij} \ge l_{ij}\).
Given \(c_{ij}\) and \(l_{ij}\) for all pipes, find the amount \(f_{ij}\), satisfying the conditions specified above.
This problem contains multiple test cases!
The first line of a multiple input is an integer \(N\), then a blank line followed by \(N\) input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of \(N\) output blocks. There is a blank line between output blocks.
Input
The first line of the input file contains the number \(N (1 \le N \le 200)\) - the number of nodes and and \(M\) - the number of pipes. The following \(M\) lines contain four integer number each - \(i, j, l_{ij}\) and \(c_{ij}\) each. There is at most one pipe connecting any two nodes and \(0 \le l_{ij} \le c_{ij} \le 10^5\) for all pipes. No pipe connects a node to itself. If there is a pipe from \(i-th\) node to \(j-th\), there is no pipe from \(j-th\) node to \(i-th\).
Output
On the first line of the output file print YES if there is the way to carry out reactor cooling and NO if there is none. In the first case M integers must follow, \(k-th\) number being the amount of liquid flowing by the \(k-th\) pipe. Pipes are numbered as they are given in the input file.
Sample Input
2
4 6
1 2 1 2
2 3 1 2
3 4 1 2
4 1 1 2
1 3 1 2
4 2 1 2
4 6
1 2 1 3
2 3 1 3
3 4 1 3
4 1 1 3
1 3 1 3
4 2 1 3
Sample Output
NO
YES
1
2
3
2
1
1
Solution
给 \(n\) 个点,及 \(m\) 根 pipe ,每根pipe用来流躺液体的,单向的,每时每刻每根 pipe 流进来的物质要等于流出去的物质,要使得 \(m\) 条 pipe 组成一个循环体,里面流躺物质。
并且满足每根 pipe 一定的流量限制,范围为 \([L_i,R_i]\).即要满足每时刻流进来的不能超过 \(R_i\) (最大流问题),同时最小不能低于 \(L_i\) 。
无源无汇上下界网络流裸题。基本思路是把无源无汇上下界网络流转成有源有汇网络流。
对于原图中的每条边 \(<u,v>:capacity=[lw,up]\) ,我们把它转成 \(<u,v>:capacity=up-lw\) ,这样就可以用一般的网络流做法来做。可是这样一来流量就不守恒,所以新设源点 \(S\) ,汇点 \(T\) ,同时开一个数组 \(du[]\) 来记录每个点的流量情况,对于点 \(i\) ,\(du[i] = inLow[i] - outLow[i]\) 。如果 \(du[i] > 0\) ,则连一条边 \(<S, i>:capacity=du[i]\) ;否则连一条边 \(<i,T>:capacity=-du[i]\) 。
有可行流的条件是每条与 \(S\) 有关的边全部满流。
#include<bits/stdc++.h>
using namespace std;
#define N 250
#define INF 2000000000
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
inline int read() {
int x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (!(ch ^ '-')) flag = -1; ch = getchar(); }
while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * flag;
}
int n, m;
int S, T;
struct edge { int v, c, next; }e[100001];
int head[N], tot = 1;
int q[N], dep[N], du[N], lw[100001], mch[100001];
inline void insert(int u, int v, int c) { e[++tot].v = v, e[tot].c = c, e[tot].next = head[u]; head[u] = tot; }
inline void add(int u, int v, int c) { insert(u, v, c), insert(v, u, 0); }
inline bool bfs() {
memset(dep, 0, sizeof dep); dep[S] = 1;
int l = 1, r = 1; q[1] = S;
while (l <= r) {
int u = q[l++];
for (int i = head[u], v; i; i = e[i].next) if (e[i].c && !dep[v = e[i].v]) {
dep[v] = dep[u] + 1, q[++r] = v;
if (!(v ^ T)) return 1;
}
}
return 0;
}
int dfs(int u, int dist) {
if (!(u ^ T) || !dist) return dist;
int ret = 0;
for (int i = head[u], v, c; i; i = e[i].next) if ((c = e[i].c) && !(dep[v = e[i].v] ^ (dep[u] + 1))) {
int d = dfs(v, min(dist, c));
dist -= d, ret += d;
e[i].c -= d, e[i ^ 1].c += d;
if (!dist) break;
}
return ret;
}
int dinic() { int ret = 0; while (bfs()) ret += dfs(S, INF); return ret; }
int main() {
int Case = read();
while (Case--) {
memset(head, 0, sizeof head); tot = 1; memset(du, 0, sizeof du);
n = read(), m = read(); T = n + 1;
rep(i, 1, m) {
int u = read(), v = read(); lw[i] = read(); int up = read();
add(u, v, up - lw[i]), du[u] -= lw[i], du[v] += lw[i], mch[i] = tot - 1;
}
int sum1 = 0, sum2 = 0;
rep(i, 1, n) if (du[i] > 0) add(S, i, du[i]), sum1 += du[i]; else add(i, T, -du[i]), sum2 -= du[i];
if (dinic() != sum1) { puts("NO"); continue; }
puts("YES");
rep(i, 1, m) printf("%d\n", e[mch[i] ^ 1].c + lw[i]);
}
return 0;
}