1. 程式人生 > >[zoj2314] Reactor Cooling

[zoj2314] Reactor Cooling

code 範圍 ++ tip hold condition head from -i

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;
}

[zoj2314] Reactor Cooling