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



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("YES\n");
			for (int i=1;i<=m;i++) 
			 printf("%d\n",remain[pipe[i]]+low[i]);
		}
	    else printf("NO\n");
	    printf("\n");
	}
}

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...