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:

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

大意:

一个没有源点汇点的图,每条边有最小和最大流量,流在图中循环。求是否存在符合要求(每条边的流量在最小和最大限制之间)的流,输出方案,spj。

题解:

将问题转化为有源点汇点的图

设超级源点S,超级汇点T

将一条流量边  a->b   [max ,  min]  (a到b,最大流量max,最小流量min)拆为三条边:

S->b  min

a->T  min

a->b max-min

个人理解,对于这一条边,跑最大流的时候需要满足从b流出的流量为min,到a的流量为min。

如果跑完最大流后满流,则存在方案,因为从S的出边流量和到T的流量相等,都等于sigma(min)

如果满流,则最小流量条件能够满足。

/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
inline int mymin(int xx,int yy)
{if(xx<yy)return xx;return yy;}
const int maxn=;
int N,M,T,t1,t2,t3,t4,sum,ans;
int ss,tt,mp[][];
int lin[maxn],len;
struct edge{
int y,next,flow;
}e[];
inline void insert(int xx,int yy,int ff){
e[++len].next=lin[xx];
lin[xx]=len;
e[len].y=yy;
e[len].flow=ff;
}
inline void ins(int xx,int yy,int ff)
{insert(xx,yy,ff),insert(yy,xx,);}
int q[maxn],head,tail,level[maxn];
bool makelevel(){
memset(level,-,sizeof(level));
head=tail=;
q[head]=ss;
level[ss]=;
for(;head<=tail;head++){
for(int i=lin[q[head]];i;i=e[i].next)
if(level[e[i].y]==-&&e[i].flow){
level[e[i].y]=level[q[head]]+;
q[++tail]=e[i].y;
}
}
return level[tt]!=-;
}
int max_flow(int x,int flow){
if(x==tt)
return flow;
int d,maxflow=;
for(int i=lin[x];i&&maxflow<flow;i=e[i].next)
if(level[e[i].y]==level[x]+&&e[i].flow){
d=max_flow(e[i].y,mymin(e[i].flow,flow-maxflow));
if(d){
maxflow+=d;
e[i].flow-=d;
if(i&)
e[i+].flow+=d;
else
e[i-].flow+=d;
}
}
if(!maxflow)
level[x]=-;
return maxflow;
}
void dinic(){
ans=;
while(makelevel()){
int d=;
while(d){
d=max_flow(ss,<<);
ans+=d;
}
}
if(ans==sum){
printf("YES\n");
for(int i=;i<=M;i++)
printf("%d\n",e[mp[i][]].flow+mp[i][]);
}
else
printf("NO\n\n");
}
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
T=read();
while(T--){
N=read(),M=read();
ss=N+,tt=ss+;
memset(lin,,sizeof(lin));len=;sum=;
for(int i=;i<=M;i++){
t1=read(),t2=read(),t3=read(),t4=read();
ins(t1,t2,t4-t3);
mp[i][]=len;mp[i][]=t3;
ins(ss,t2,t3);
ins(t1,tt,t3);
sum+=t3;
}
dinic();
}
return ;
}

ZOJ 2314 无源汇可行流(输出方案)的更多相关文章

  1. ZOJ 2314 Reactor Cooling | 无源汇可行流

    题目: 无源汇可行流例题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题解: 证明什么的就算了,下面给出一种建图方式 ...

  2. ZOJ 1314 Reactor Cooling | 上下界无源汇可行流

    ZOJ 1314 Reactor Cooling | 上下界无源汇可行流 题意 有一个网络,每条边有流量的上界和下界,求一种方案,让里面的流可以循环往复地流动起来. 题解 上下界无源汇可行流的模型: ...

  3. 算法复习——无源汇可行流(zoj2314)

    题目: The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nucl ...

  4. sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] ...

  5. SGU 194 无源无汇可行流求解

    题意:n个点,m条边,每条边有容量限制 l--c,每个点满足容量平衡(流入等于流出),求可行解 无源无汇可行流问题,建立以一个超级源点和超级汇点,由于原来最大流问题时候,流量下界其实为0, 所以要转化 ...

  6. ZOJ 3229 Shoot the Bullet | 有源汇可行流

    题目: 射命丸文要给幻想乡的居民照相,共照n天m个人,每天射命丸文照相数不多于d个,且一个人n天一共被拍的照片不能少于g个,且每天可照的人有限制,且这些人今天照的相片必须在[l,r]以内,求是否有可行 ...

  7. BZOJ.1927.[SDOI2010]星际竞速(无源汇上下界费用流SPFA /最小路径覆盖)

    题目链接 上下界费用流: /* 每个点i恰好(最少+最多)经过一次->拆点(最多)+限制流量下界(i,i',[1,1],0)(最少) 然后无源汇可行流 不需要源汇. 注: SS只会连i',求SS ...

  8. 【zoj2314】Reactor Cooling 有上下界可行流

    题目描述 The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuc ...

  9. [BZOJ3698]XWW的难题解题报告|上下界网络流|有源汇最大流

    XWW是个影响力很大的人,他有很多的追随者.这些追随者都想要加入XWW教成为XWW的教徒.但是这并不容易,需要通过XWW的考核.XWW给你出了这么一个难题:XWW给你一个N*N的正实数矩阵A,满足XW ...

随机推荐

  1. jsp%不能解析

    做一个传值问题时 遇到错误 百度了一下是百分号不能解析,实在搞不明白为什么,以前这样做好好的,这次就不行了,不知道为什么,后来偶然一次把标签删了 错误居然没了,难道struts2的这个标签不支持这样传 ...

  2. 整合springboot,angular2,可以前后台交互数据

    改造了一下angular2官方文档中的hero项目,让其可以进行后台的交互, https://github.com/DACHUYIN 源码在上面...博客就不写了....

  3. C# windform自定义控件的属性小知识

    word中的加粗变斜之类的一直让我以为是button,直到我接触了自定义控件,才发现实现这种机能最好的是CheckBox,然后我们在做一个系统的时候,这种控件有可能要用好多次,总不能在用一次的时候,就 ...

  4. html5——多媒体(三)

    自定义进度条 1.video标签是内联块,可以设置宽高,但是需要用大盒子将其包裹起来进行定位 2.小盒子设计成进度条样式,并进行定位 3.进度条样式中的特殊按钮可以用web字体 4.通过点击实现视频的 ...

  5. mysql命令行导出数据

    1. 包含表头 mysql -h${1} -P${2} -u${3} -p${4} -Dpom_${5} --default-character-set=utf8 -B -e > result. ...

  6. 浏览器的 local storage

    浏览器 local storage      本地存储 session storage    会话存储 cookies                  本地存储 1.     local stora ...

  7. 为什么阿里规约手册要求谨慎使用Arrays.asList方法

    前言 在开发中,有时候会碰到把多个参数,或者说把数组转成List的需求,通常我们会使用 Arrays.asList()方法.但是该方法在使用的过程中,稍有不慎就会出现严重的异常.有如下代码: @Tes ...

  8. @Override注解

    @Override注解对于代码可读性的提升十分巨大 而且良好的可读性是一个优秀程序员必备的基础素养

  9. Luogu P2922 秘密消息

    原题 P2922 [USACO08DEC]秘密消息Secret Message 题目描述 Bessie is leading the cows in an attempt to escape! To ...

  10. js中阻止事件冒泡和浏览器默认行为

    在使用javascript编程时会遇到一个问题,就是当你给html添加事件时,由于浏览器默认的为冒泡型事件触发机制,所以会触发你不想触发的事件.那么通过如下的函数可以解决这个问题.[兼容IE和FF] ...