题目描述

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.

输入

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.

输出

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.

样例输入

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


题目大意

给你一张n个点和m条边的图,每条边有[li,ri]的容量,求是否有可行流?有则输出一组方案。

题解

有上下界网络流无源汇可行流模板题,题意都很直白。

转化为最大流。

假设有一条容量为[l,r]的路径连通x->y,那么进行如下操作:

1.记录路径的l(求总流量时会用到)

2.加入x->y,容量为r-l的边

3.将x的流入总数in[x]减去l,将y的流入总数in[y]加上l。

处理完所有路径后,再建立超级源点和超级汇点,并扫一遍每个点。

对于点x,如果in[x]>0,则加S->x,容量为in[x]的边,否则加x->T,容量为-in[x]的边。

跑一遍最大流,如果满流则有解,否则无解。

有解时,对于每条通道i,它的总流量为下界low[i]加上新图的流出量val[i<<1|1]。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
queue<int> q;
int head[210] , to[100000] , val[100000] , next[100000] , cnt , low[100000] , in[210] , dis[100000] , s , t;
void add(int x , int y , int z)
{
to[++cnt] = y;
val[cnt] = z;
next[cnt] = head[x];
head[x] = cnt;
}
bool bfs()
{
int x , i;
while(!q.empty()) q.pop();
memset(dis , 0 , sizeof(dis));
dis[s] = 1;
q.push(s);
while(!q.empty())
{
x = q.front();
q.pop();
for(i = head[x] ; i ; i = next[i])
{
if(val[i] && !dis[to[i]])
{
dis[to[i]] = dis[x] + 1;
if(to[i] == t) return 1;
q.push(to[i]);
}
}
}
return 0;
}
int dinic(int x , int l)
{
if(x == t) return l;
int temp = l , k , i;
for(i = head[x] ; i ; i = next[i])
{
if(val[i] && dis[to[i]] == dis[x] + 1)
{
k = dinic(to[i] , min(temp , val[i]));
if(!k) dis[to[i]] = 0;
val[i] -= k , val[i ^ 1] += k;
if(!(temp -= k)) break;
}
}
return l - temp;
}
int main()
{
int T;
scanf("%d" , &T);
while(T -- )
{
int n , m , i , x , y , z , sum = 0 , maxflow = 0;
scanf("%d%d" , &n , &m);
memset(head , 0 , sizeof(head));
memset(in , 0 , sizeof(in));
cnt = 1;
s = 0 , t = n + 1;
for(i = 1 ; i <= m ; i ++ )
{
scanf("%d%d%d%d" , &x , &y , &low[i] , &z);
in[x] -= low[i] , in[y] += low[i];
add(x , y , z - low[i]) , add(y , x , 0);
}
for(i = 1 ; i <= n ; i ++ )
{
if(in[i] > 0) sum += in[i] , add(s , i , in[i]) , add(i , s , 0);
else if(in[i] < 0) add(i , t , -in[i]) , add(t , i , 0);
}
while(bfs()) maxflow += dinic(s , 0x7fffffff);
if(maxflow != sum) printf("NO\n");
else
{
printf("YES\n");
for(i = 1 ; i <= m ; i ++ )
printf("%d\n" , val[i << 1 | 1] + low[i]);
}
printf("\n");
}
return 0;
}

【zoj2314】Reactor Cooling 有上下界可行流的更多相关文章

  1. ZOJ2314 Reactor Cooling(无源汇上下界可行流)

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

  2. ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...

  3. zoj 2314 Reactor Cooling (无源汇上下界可行流)

    Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...

  4. Zoj 2314 Reactor Cooling(无源汇有上下界可行流)

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

  5. zoj2314 无源汇上下界可行流

    题意:看是否有无源汇上下界可行流,如果有输出流量 题解:对于每一条边u->v,上界high,下界low,来说,我们可以建立每条边流量为high-low,那么这样得到的流量可能会不守恒(流入量!= ...

  6. ZOJ_2314_Reactor Cooling_有上下界可行流模板

    ZOJ_2314_Reactor Cooling_有上下界可行流模板 The terrorist group leaded by a well known international terroris ...

  7. 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]

    题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...

  8. poj2396 Budget(有源汇上下界可行流)

    [题目链接] http://poj.org/problem?id=2396 [题意] 知道一个矩阵的行列和,且知道一些格子的限制条件,问一个可行的方案. [思路] 设行为X点,列为Y点,构图:连边(s ...

  9. POJ2396 Budget [有源汇上下界可行流]

    POJ2396 Budget 题意:n*m的非负整数矩阵,给出每行每列的和,以及一些约束关系x,y,>=<,val,表示格子(x,y)的值与val的关系,0代表整行/列都有这个关系,求判断 ...

随机推荐

  1. Linux学习-rsyslog.service :记录登录文件的服务

    rsyslog.service 的配置文件:/etc/rsyslog.conf 我们现在知道 rsyslogd 可以负责主机产生的各个信息的登录,而这些信息本身是有『严重等级』之分的, 而且, 这些资 ...

  2. BZOJ1053_反素数_KEY

    题目传送门 初看这道题,以为是一道挺难的题目,但仔细看发现,不是只要爆搜就好了吗? 只需要对前12个素数进行爆搜即可. 一个数的因数个数=素数次数+1全部乘起来. code: /*********** ...

  3. 食物链_KEY

    食物链 (eat.pas/c/cpp) [ 问题描述] 动物王国中有三类动物 A,B,C, 这三类动物的食物链构成了有趣的环形. A 吃 B, B 吃C, C 吃 A.现有 N 个动物, 以 1-N ...

  4. Uber优步北京第二、三组奖励政策

    优步北京第二.三组: 定义为​2015年6月1日至今激活的司机(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机(全国版最新最 ...

  5. Objective-C 内存管理和ARC

    内存管理 范围: 任何继承了NSObject的对象 对基本数据类型无效 原理: 每个对象内部都保存了一个与之相关联的整数 称为引用计数器 1.计数器的基本操作 当使用alloc new或者copy创建 ...

  6. informix如何查询第一条记录

    1.select first 1 * from shop; 正序查询第一条数据 2.select first 1 * from shop order by create_time desc; 按创建时 ...

  7. lintcode702 连接两个字符串中的不同字符

    连接两个字符串中的不同字符   给出两个字符串, 你需要修改第一个字符串,将所有与第二个字符串中相同的字符删除, 并且第二个字符串中不同的字符与第一个字符串的不同字符连接 思路:遍历两个字符串,找到互 ...

  8. 【20180807模拟测试】t1 function

    low逼的我也只能写这样的水题... 题面 对于一个整数,定义 f(x)为他的每个数位的阶乘的乘积.例如 f(135)=1! * 3! * 5! =720.给出一个数 a(可以包含前缀零),a 满足他 ...

  9. mac os x下应用endnote异常解决办法

    最近在用Office+Endnote写论文,使用拼音输入法换字时会出现重字和拼音的情况,比如我想打“桥连”,最终出现的是"qiao'lian桥lian桥连”.后来发现这个问题时由endnot ...

  10. opencv-学习笔记(5)形态学转变

    opencv-学习笔记(4)形态学转变 本章讲了几种形态学操作 腐蚀erode 膨胀dilate 开运算MORPH_OPEN 闭运算MORPH_CLOSE 形态学梯度MORPH_GRADIENT 礼帽 ...