微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【无源汇有上下界可行流】ZOJ-2314 Reactor Cooling

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:

f i,1+f i,2+...+f i,N= f 1,i+f 2,i+...+f N,i

Each pipe has some finite capacity,therefore for each i and j connected by the pipe must be fij<= cijwhere 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 lijfor 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


NO

YES
1
2
3
2
1
1

————————————————————熄灯的分割线————————————————————
前言:SGU194和这题一样。改一下输入输出
思路:容量带上下界的网络流问题。判断是否存在可行流。
构建一张伴随网络,在其上跑最大流。
伴随网络:原图中上下界的边,变为仅有上界(值为 上界 - 下界 )的边,之后增加一个源点S ' ,一个汇点T ' 。从S ' 向每个点连接一条边,值为“流入该点下界容量之和 - 流出该点下界容量之和”。
跑完最大流之后,判断S ' 出发的所有边是否全部满载(正向弧残留量为0),是则存在可行流(值为伴随网络最大流+下界),否则不存在。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 222,M = 44444;
struct Node {
	int u,v,w;
	int next;
}edge[M];
int n,m,S,T,tot,head[N],cur[N],lev[N],q[N],s[N];
int b[M],f[M];

void init()
{
	tot = 0; memset(head,-1,sizeof(head));
}

void add(int u,int v,int w)
{
	edge[tot].u = u; edge[tot].v = v; edge[tot].w = w;
	edge[tot].next = head[u]; head[u] = tot++;
}

bool bfs()
{
	memset(lev,sizeof(lev));
	int fron = 0,rear = 0;
	lev[S] = 0;
	q[rear++] = S;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		for(int i = head[u]; ~i; i = edge[i].next) {
			int v = edge[i].v;
			if(lev[v] == -1 && edge[i].w) {
				lev[v] = lev[u] + 1;
				q[rear%N] = v; rear++;
				if(v == T) return true;
			}
		}
	}
	return false;
}

void Dinic()
{
	while(bfs()) {
		memcpy(cur,head,sizeof(head));
		int u = S,top = 0;
		while(1) {
			if(u == T) {
				int mini = INF,loc;
				for(int i = 0; i < top; i++) {
					if(mini > edge[s[i]].w) {
						mini = edge[s[i]].w;
						loc = i;
					}
				}
				for(int i = 0; i < top; i++) {
					edge[s[i]].w -= mini;
					edge[s[i]^1].w += mini;
				}
				top = loc;
				u = edge[s[top]].u;
			}
			int &i = cur[u];
			for(; ~i; i = edge[i].next) {
				int v = edge[i].v;
				if(edge[i].w && lev[v] == lev[u] + 1) break;
			}
			if(~i) {
				s[top++] = i;
				u = edge[i].v;
			}
			else {
				if(!top) break;
				lev[u] = -1;//阻塞流,记得废除该点标号
				u = edge[s[--top]].u;
			}
		}
	}
}

bool judge()
{
	for(int i = head[S]; ~i; i = edge[i].next) if(edge[i].w) return false;
	puts("YES");
	for(int j = 0; j < m; j++) {
		printf("%d\n",edge[j<<1|1].w + b[j]);
	}
	return true;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in","r",stdin);
//	freopen(".out","w",stdout);
#endif
	int TT;
	scanf("%d",&TT);
	int kase = 0;
	while(TT--) {
		init();
		memset(f,sizeof(f));
		scanf("%d%d",&n,&m);
		S = 0; T = n+1;
		int u,c;
		for(int j = 0; j < m; j++) {
			scanf("%d%d%d%d",&u,&v,&b[j],&c);
			add(u,c - b[j]); add(v,u,0);
			//f : 流入下界 - 流出下界
			f[u] -= b[j];
			f[v] += b[j];
		}
		for(int k = 1; k <= n; k++) {
			if(f[k] > 0) {
				add(S,k,f[k]); add(k,0);
			}
			else if(f[k] < 0) {
				add(k,-f[k]); add(T,0);
			}
		}
		Dinic();
		if(kase) puts("");
		kase++;
		if(!judge()) puts("NO");
	}
	return 0;
}

原文地址:https://www.jb51.cc/react/308090.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐