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 <= 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



Author:  Andrew Stankevich
Source:  Andrew Stankevich's Contest #1
Submit     Status

题目大意:一个核反应堆的冷却系统有n个结点,有m条单向的管子连接它们,管子内流量有上下界的要求,问能否使液体在整个系统中循环流动。

题解:无源汇有上下界的可行流

所为无源汇就是没有明确的起点和终点,流在网络中循环流动。有上下界就是每条边有一个流量的限制[bi,ci]。

可行流就是使每条边满足流量限制的一组方案(可能有多种),且每个点的流入量严格等于流出量。

下面给出论文中的构图证明:


解决无源汇有上下界的可行流的关键就是利用流量平衡,附加源汇的作用也是为了保证流量平衡,这样我们才能将网络中的每一条边变成无下界的,然后利用最大流求解。

总结一下无源汇有上下界的可行流的构造:

原图中x->y的限制为[b,c],在重构图中就有边x->y容量为c-b

设附加源点为S,附加汇点为T,M(i)表示流入节点i的下界总和-流出节点i的下界总和

如果M(i)非负 S->i 容量为M(i)

否则 i->T 容量为-M(i)

如果与S,T相连的边都能满留,就说明所有的下界都满足了,那么一定存在可行流。

现在做完最大流的网络就可以转化成一个可行流的方案。流经x->y这条边的实际流量就是反向边的残余流量+这条边的流量下界。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define N 200000
#define inf 1000000000
using namespace std;
int n,m;
int s,t,tot; 
int point[N],next[N],v[N],remain[N],in[N],out[N],low[N];
int last[N],mark[N],pipe[N],sum[N],deep[N],num[N],cur[N];
void add(int x,int y,int z)
{
	tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z;
	tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
	//cout<<x<<" "<<y<<" "<<z<<endl;
}
int addflow(int s,int t)
{
	int ans=inf; int now=t;
	while (now!=s) {
		ans=min(ans,remain[last[now]]);
		now=v[last[now]^1];
	}
	now=t;
	while (now!=s) {
		remain[last[now]]-=ans;
		remain[last[now]^1]+=ans;
		now=v[last[now]^1];
	}
	return ans;
}
void bfs(int s,int t)
{
	for (int i=s;i<=t;i++) deep[i]=t;
	deep[t]=0;
	queue<int> p;
	p.push(t);
	while (!p.empty()){
		int x=p.front(); p.pop();
		for (int i=point[x];i!=-1;i=next[i])
		 if (deep[v[i]]==t&&remain[i^1])
		  deep[v[i]]=deep[x]+1,p.push(v[i]);
	}
}
void isap(int s,int t)
{
	bfs(s,t); int ans=0;
	for (int i=s;i<=t;i++) num[deep[i]]++;
	for (int i=s;i<=t;i++) cur[i]=point[i];
	int now=s;
	while (deep[s]<t) {
		if (now==t) {
			ans+=addflow(s,t);
			now=s;
		}
		bool pd=false;
		for (int i=cur[now];i!=-1;i=next[i])
		 if (remain[i]&&deep[v[i]]+1==deep[now]){
		 	cur[now]=i;
		 	last[v[i]]=i;
		 	now=v[i];
		 	pd=true;
		 	break;
		 }
		if (!pd) {
			int minn=t;
			for (int i=point[now];i!=-1;i=next[i])
			 if (remain[i]) minn=min(minn,deep[v[i]]);
			if (!--num[deep[now]]) break;
			deep[now]=minn+1;
			num[deep[now]]++;
			cur[now]=point[now];
			if (now!=s) {
				now=v[last[now]^1];
			}
 		}
	}
}
bool check()
{
	for (int i=1;i<=n;i++)
	 if (abs(sum[i])!=remain[mark[i]]) return false;
	return true;
}
int main()
{
	freopen("a.in","r",stdin);
	int T;
	scanf("%d",&T);
	for (int t1=1;t1<=T;t1++){
		tot=-1;
		memset(point,-1,sizeof(point));
		memset(next,-1,sizeof(next));
		memset(remain,0,sizeof(remain));
		memset(num,0,sizeof(num));
		memset(in,0,sizeof(in));
		memset(out,0,sizeof(out));
		scanf("%d%d",&n,&m);
		s=1; t=n+2;
		for (int i=1;i<=m;i++) {
			int x,y,l,r;
			scanf("%d%d%d%d",&x,&y,&l,&r);
			add(x+1,y+1,r-l);
			pipe[i]=tot;
			in[y]+=l; out[x]+=l; low[i]=l;
		}
		//for (int i=1;i<=n;i++) cout<<in[i]<<" "<<out[i]<<endl;
		for (int i=1;i<=n;i++) {
			sum[i]=out[i]-in[i];
			if (sum[i]>0) add(i+1,t,sum[i]);
			else add(s,i+1,-sum[i]);
			mark[i]=tot;
		}
		isap(s,t);
		if (check()) {
			printf("YES\n");
			for (int i=1;i<=m;i++) 
			 printf("%d\n",remain[pipe[i]]+low[i]);
		}
	    else printf("NO\n");
	    printf("\n");
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值