ZOJ 2314 Reactor Cooling 有下界的网络流

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zy704599894/article/details/83028209

Reactor Cooling


Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge


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 fij, (put fij = 0 if there is no pipe from node i to node j), for each i the following condition must hold:

fi,1+fi,2+...+fi,N = f1,i+f2,i+...+fN,i

Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be fij <= cij where cij 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 lij, thus it must be fij >= lij.

Given cij and lij for all pipes, find the amount fij, 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 <= N <= 200) - the number of nodes and and M - the number of pipes. The following M lines contain four integer number each - i, j, lij and cij each. There is at most one pipe connecting any two nodes and 0 <= lij <= cij <= 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 Input

NO

YES
1
2
3
2
1
1

题意:给以个流量图,每个边的流量必须为[l,r],没有源汇点,问是否能跑出一个可行流并输出每条边的流量。

无源汇点有下界可行流建图方法:

对于每个点,求出流入流量下界之和和流出流量下界之和,所有边u->v的流量下界变位0,上界变位high-low。但是这样不能保证流量守恒,所以需要建立附加流量,设源点s和汇点t,对于所有下界流入量大于流出量的点,s与之建边,流量为两者之车,对于所有下界流入量小于流出量的点,与t建边,流量为两者之差。此时跑一边最大流,如果从源点流出的流量之和等于最大流,就说明可以流量守恒即存在可行流,那么没条边的流量等于该条边的流量下界+附加流跑出的流量。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#include<queue>
using namespace std;
const int maxn = 205;
const int maxm = 50005;
const int INF = 1e9 + 7;
struct node
{
	int u, v, flow, next, low;
}edge[maxm];
int n, m, s, t, cnt;
int head[maxn], dis[maxn], pre[maxm], cur[maxm], in[maxn], out[maxn];
char str[205][205];
void init()
{
	cnt = s = 0, t = n + 1;
	memset(head, -1, sizeof(head));
	memset(in, 0, sizeof(in));
	memset(out, 0, sizeof(out));
}
void add(int u, int v, int flow, int low)
{
	edge[cnt].u = u, edge[cnt].v = v, edge[cnt].flow = flow, edge[cnt].low = low;
	edge[cnt].next = head[u], head[u] = cnt++;
	edge[cnt].u = v, edge[cnt].v = u, edge[cnt].flow = 0, edge[cnt].low = low;
	edge[cnt].next = head[v], head[v] = cnt++;
}
int bfs()
{
	queue<int>q;
	memset(dis, -1, sizeof(dis));
	dis[s] = 0;
	q.push(s);
	while (!q.empty())
	{
		int u = q.front();q.pop();
		for (int i = head[u];i != -1;i = edge[i].next)
		{
			int v = edge[i].v;
			if (dis[v] == -1 && edge[i].flow)
			{
				dis[v] = dis[u] + 1;
				q.push(v);
			}
		}
	}
	if (dis[t] == -1) return 0;
	return 1;
}
int dfs(int u, int flow)
{
	if (u == t) return flow;
	for (int &i = cur[u];i != -1;i = edge[i].next)
	{
		int v = edge[i].v;
		if (dis[v] == dis[u] + 1 && edge[i].flow)
		{
			int d = dfs(v, min(edge[i].flow, flow));
			if (d > 0)
			{
				edge[i].flow -= d;
				edge[i ^ 1].flow += d;
				return d;
			}
		}
	}
	return 0;
}
int dinic()
{
	int ans = 0, d;
	while (bfs())
	{
		for (int i = s;i <= t;i++) cur[i] = head[i];
		while (d = dfs(s, INF))
			ans += d;
	}
	return ans;
}
int main()
{
	int i, j, k, sum, x, T;
	int u, v, low, high;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &n, &m);
		init();sum = 0;
		for (i = 1;i <= m;i++)
		{
			scanf("%d%d%d%d", &u, &v, &low, &high);
			add(u, v, high - low, low);
			in[v] += low, out[u] += low;
		}
		for (i = 1;i <= n;i++)
		{
			if (in[i] > out[i])
			{
				sum += in[i] - out[i];
				add(s, i, in[i] - out[i], 0);
			}
			else add(i, t, out[i] - in[i], 0);
		}
		if (dinic() != sum)
			printf("NO\n");
		else
		{
			printf("YES\n");
			for (i = 0;i < m * 2;i += 2)
				printf("%d\n", edge[i ^ 1].flow + edge[i].low);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zy704599894/article/details/83028209