Reactor Cooling

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


一道无汇源上下界可行流。

具体建图:

  1. 原图每条边的上下界为l,r ,那么新图的此边的流量为 r-l ,并且记录每个点的流入下和界和流出下界和。
  2. 对于每个点的流出下界和,若大于0则让S连向此点流量为和,若小于0则连向T,流量为和。
  3. 跑S到T的最大流,若为S流出之和则有解,否则无解。
  4. 输出每条边的实际流量:下界+反向边的流量。

AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=210,M=1e6+10,inf=0x3f3f3f3f;
int T,n,m,d[N],h[N],ss,tt=201,l[M],sum;
int head[N],nex[M],to[M],w[M],tot,cur[N];
inline void ade(int a,int b,int c){
	to[++tot]=b; nex[tot]=head[a]; w[tot]=c; head[a]=tot;
}
inline void add(int a,int b,int c){
	ade(a,b,c); ade(b,a,0);
}
inline void init(){
	tot=1; memset(head,0,sizeof head); memset(d,0,sizeof d); sum=0;
}
inline int bfs(){
	queue<int> q; q.push(ss); memset(h,0,sizeof h); h[ss]=1;
	while(q.size()){
		int u=q.front(); q.pop();
		for(int i=head[u];i;i=nex[i]){
			if(!h[to[i]]&&w[i]){
				h[to[i]]=h[u]+1;	q.push(to[i]); 
			}
		}
	}
	return h[tt];
}
int dfs(int x,int f){
	if(x==tt)	return f; int fl=0;
	for(int i=head[x];i&&f;i=nex[i]){
		if(w[i]&&h[to[i]]==h[x]+1){
			int mi=dfs(to[i],min(f,w[i]));
			w[i]-=mi; w[i^1]+=mi; fl+=mi; f-=mi;
		}
	}
	if(!fl) h[x]=-1;
	return fl;
}
int dinic(){
	int res=0;
	while(bfs())	res+=dfs(ss,inf);
	return res;
}
signed main(){
	scanf("%d",&T);
	while(T--){	
		scanf("%d %d",&n,&m);	init();
		for(int i=1;i<=m;i++){
			int a,b,r;	scanf("%d %d %d %d",&a,&b,&l[i],&r);
			add(a,b,r-l[i]);	d[a]-=l[i];	d[b]+=l[i]; 
		}
		for(int i=1;i<=n;i++){
			if(d[i]>0)	add(ss,i,d[i]),sum+=d[i];
			else if(d[i]<0)	add(i,tt,-d[i]);
		}
		if(dinic()==sum){
			puts("YES");
			for(int i=1;i<=m;i++){
				printf("%d\n",l[i]+w[(i<<1)^1]);
			}
		}else	puts("NO");
	}
	return 0;
}
全部评论

相关推荐

10-13 22:56
门头沟学院 C++
rt,鼠鼠的浪潮网签明天过期,鼠鼠是山东人,好像自己也能接受。之前的面试大厂基本挂干净了,剩下小米二面后在泡,问了下面试官没有挂,但要泡。还有海信似乎也通过了,不过在深圳,鼠鼠也不是很想去。其它还有一些公司应该陆陆续续还有一些面试,现在有些纠结是直接签了还是再等再面呢?大佬们能不能给鼠鼠提一些意见,万分感谢!!!
牛客78696106...:浪潮可不是开摆,当初我还是开发的时候我组长跟我说他们组有段时间天天1,2点走,早上5点就来,全组肝出来心肌炎,浪潮挣钱省立花可不是说说,当然也看部门,但是浪潮普遍就那dio样,而且你算下时薪就知道不高,没事也是9点半走,不然算你旷工
投递小米集团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务