加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

zoj 2314 Reactor Cooling (无源汇有上下界的可行流)

发布时间:2020-12-15 07:37:39 所属栏目:百科 来源:网络整理
导读: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 plann
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



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

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

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

#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,sizeof(next));
		memset(remain,sizeof(remain));
		memset(num,sizeof(num));
		memset(in,sizeof(in));
		memset(out,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,sum[i]);
			else add(s,i+1,-sum[i]);
			mark[i]=tot;
		}
		isap(s,t);
		if (check()) {
			printf("YESn");
			for (int i=1;i<=m;i++) 
			 printf("%dn",remain[pipe[i]]+low[i]);
		}
	    else printf("NOn");
	    printf("n");
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读