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

有上下界的网络流可行流:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=;
int Laxt[maxn],Next[maxn],To[maxn],Cap[maxn],cnt;
int dis[maxn],nd[maxn],S,T,num,ans,q[maxn],qnum[maxn],top;
void init()
{
cnt=;ans=num=top=;
memset(Laxt,,sizeof(Laxt));
memset(dis,,sizeof(dis));
memset(nd,,sizeof(nd));
}
int add(int u,int v,int c)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Cap[cnt]=c; Next[++cnt]=Laxt[v];
Laxt[v]=cnt;
To[cnt]=u;
Cap[cnt]=;
}
int sap(int u,int flow)
{
if(u==T||flow==) return flow;
int delta=,tmp;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(dis[v]+==dis[u]&&Cap[i]>){
tmp=sap(v,min(Cap[i],flow-delta));
delta+=tmp;
Cap[i]-=tmp;
Cap[i^]+=tmp;
if(flow==delta||dis[]>=T) return delta;
}
}
nd[dis[u]]--;
if(nd[dis[u]]==) dis[]=T;
nd[++dis[u]]++;
return delta;
}
int main()
{
int Case,n,i,j,m,u,v,x,y;
scanf("%d",&Case);
while(Case--){
init();
scanf("%d%d",&n,&m);
S=;T=n+;
for(i=;i<=m;i++){
scanf("%d%d%d%d",&u,&v,&x,&y);
u++;v++;num+=x;
add(u,v,y-x);
q[++top]=cnt;
qnum[top]=x;
add(S,v,x);
add(u,T,x);
}
while(dis[S]<T) {
ans+=sap(S,inf);
}
if(num!=ans) printf("NO\n");
else {
printf("YES\n");
for(i=;i<=top;i++) printf("%d\n",Cap[q[i]]+qnum[i]);
}
if(T) printf("\n");
}
return ;
}

分享三张图

看完后应该就能理解了。具体的以后再慢慢整理咯。

ZOJ2314 Reactor Cooling(有上下界的网络流)的更多相关文章

  1. ZOJ 2314 Reactor Cooling 带上下界的网络流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的, ...

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

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

  3. SGU 194 Reactor Cooling (无源上下界网络流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

  4. 【ZOJ2314】Reactor Cooling(有上下界的网络流)

    前言 话说有上下界的网络流好像全机房就我一个人会手动滑稽,当然这是不可能的 Solution 其实这道题目就是一道板子题,主要讲解一下怎么做无源无汇的上下界最大流: 算法步骤 1.将每条边转换成0~u ...

  5. SGU 194. Reactor Cooling(无源汇有上下界的网络流)

    时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...

  6. ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)

    //有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...

  7. ZOJ 2314 有上下界的网络流

    problemCode=2314">点击打开链接 题意:给定m条边和n个节点.每条边最少的流量和最多的流量.保证每一个节点的出入流量和相等,问能够形成吗,能够则输出每条边的流量 思路: ...

  8. poj_2396 有上下界的网络流

    题目大意 一个mxn的矩阵,给出矩阵中每一行的和sh[1,2...m]以及每一列的数字的和目sv[1,2...n],以及矩阵中的一些元素的范围限制,比如a[1][2] > 1, a[2][3] ...

  9. 【BZOJ2502】清理雪道 有上下界的网络流 最小流

    [BZOJ2502]清理雪道 Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降 ...

随机推荐

  1. Python基础(15)_python模块、包

    一.模块 1.什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀 模块的本质:模块的本质是一个py文件 2.模块分为三类:1)内置模块:2)第三方模块: ...

  2. JavaScript:基础扩展(1)——JSON

    JavaScript:扩展知识(1)——JSON 理解: 关于 JSON,最重要的是要理解它是一种数据格式,不是一种编程语言.虽然具有相同的语法形式,但 JSON 并不从属于 JavaScript.而 ...

  3. openstack ocata版(脚本)计算节点安装

    一.初始化环境: 1.安装软件包: yum -y install centos-release-openstack-ocata yum -y upgrade yum -y install python ...

  4. php备份mysql数据库

    <?php /*程序功能:mysql数据库备份功能*/ ini_set('max_execution_time','0'); ini_set('memory_limit','1024M');// ...

  5. 转:ADO,OLEDB,ODBC,DAO的区别

    ODBC(Open Database Connectivity,开放数据库互连) 1992年,微软公司开放服务结构(WOSA,Windows Open Services Architecture)中有 ...

  6. android 获取视频缩略图终极解决方案(ffmpeg)

    http://blog.csdn.net/u010499721/article/details/50338623 前些天有个师弟(在做一个仿LinkInEyes行车记录仪的app)问我怎么获取视频缩略 ...

  7. Django框架之自定义分页

    分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置. 1.设定每页显示数据条数 2.用户输入页码(第一页.第二页...) 3.根据设定的每页显示条数和当 ...

  8. bash脚本之读取数据

    题目: 一个tab间隔的文件,读取时一行为一个循环,依次读取每行的参数. 比如第一行为:a b c ,输出为a+b+c #/bin/bash while read id do a=($id) b=${ ...

  9. Centos 一次卸载多个RPM包

    rpm 不支持通配符,可以使用 xargs来接收多个变量 示例,一次性卸载所有 erlang先关的rpm包: rpm -qa | grep erlang | xargs rpm -e --nodeps

  10. ggplot笔记002——qplot()函数

    qplot()函数 一年前就听说过ggplot,很多人都说ggplot强大,ggplot无所不能,从今天开始就让我们一起来见证一下这个神奇的R包. 首先要加载ggplot2: 1 if(!suppre ...