题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314

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:

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

解题思路:设d[u]为顶点u入边下界和-出边下界和,新建源点、汇点,原网络的弧<u,v>容量设置成其上界-下界,对于每一个顶点u,如果d[u]>0则源点向其连容量d[u]的边,否则其向汇点连容量-d[u]的边,最后如果和源点相关的弧都满流则存在可行流,而各条边的流量+其在原网络的下界就是一个解。——https://www.cnblogs.com/WABoss/p/5371871.html

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define INF 0x3f3f3f3f
const ll MAXN = + ;
const ll MAXM = 1e5 + ;
const ll MOD = 1e9 + ;
const double pi = acos(-);
int cnt = -, head[MAXM], dis[MAXN], cur[MAXM];
int n, m;
struct edge
{
int to, value, net, num;
} e[MAXM << ]; ///共有n*2条边
void add(int from, int to, int value, int num)
{ ///链式前向星
cnt++;
e[cnt].to = to;
e[cnt].num = num;
e[cnt].value = value;
e[cnt].net = head[from];
head[from] = cnt;
}
int bfs(int st, int ed)
{ ///建立层次图
queue<int> que;
memset(dis, -, sizeof(dis));
dis[st] = ;
que.push(st);
while (!que.empty())
{
int x = que.front();
que.pop();
for (int i = head[x]; i > -; i = e[i].net)
{
int now = e[i].to;
if (dis[now] == - && e[i].value)
{
que.push(now);
dis[now] = dis[x] + ;
}
}
}
return dis[ed] != -;
}
int dfs(int x, int t, int maxflow)
{
if (x == t)
return maxflow;
int ans = ;
for (int i = cur[x]; i > -; i = e[i].net)
{ ///当前弧优化
int now = e[i].to;
if (dis[now] != dis[x] + || e[i].value == || ans >= maxflow)
continue;
cur[x] = i;
int f = dfs(now, t, min(e[i].value, maxflow - ans));
e[i].value -= f;
e[i ^ ].value += f; ///反向边加流量
ans += f;
}
if (!ans)
dis[x] = -; ///炸点优化
return ans;
}
int Dinic(int st, int ed)
{
int ans = ;
while (bfs(st, ed))
{
memcpy(cur, head, sizeof(head));
int k;
while ((k = dfs(st, ed, INF)))
ans += k;
}
return ans;
}
int lowf[MAXM], totflow[MAXN];
int ans[MAXM];
void init()
{
cnt = -;
memset(head, -, sizeof(head));
memset(totflow, , sizeof(totflow));
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
init();
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++)
{
int from, to, v;
scanf("%d%d%d%d", &from, &to, &lowf[i], &v);
add(from, to, v - lowf[i], i);
add(to, from, , i); //建图 令每条边的流量等于流量下限后的残量网络
totflow[from] -= lowf[i]; //流出
totflow[to] += lowf[i]; //流入
//流量守恒定律:每个点的总流入量=总流出量
}
int sumflow = ;
for (int i = ; i <= n; i++)
{
if (totflow[i] > )
{
add(, i, totflow[i], );
add(i, , , );
sumflow += totflow[i];
}
else
{
add(i, n + , -totflow[i], );
add(n + , i, , );
}
}
if (Dinic(, n + ) == sumflow) //总入流=总流量 则可行
{
printf("YES\n");
for (int i = ; i <= n; i++)
{
for (int j = head[i]; j > -; j = e[j].net)
{
if (e[j].num == || j % == )
continue;
else
ans[e[j].num] = e[j].value + lowf[e[j].num];
}
}
for (int i = ; i <= m; i++)
printf("%d\n", ans[i]);
}
else
printf("NO\n");
if (t)
printf("\n");
}
return ;
}

ZJU-Reactor Cooling(无源汇有上下界最大流)的更多相关文章

  1. ZOJ 2314 (sgu 194) Reactor Cooling (无源汇有上下界最大流)

    题意: 给定n个点和m条边, 每条边有流量上下限[b,c], 求是否存在一种流动方法使得每条边流量在范围内, 而且每个点的流入 = 流出 分析: 无源汇有上下界最大流模板, 记录每个点流的 in 和 ...

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

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

  3. SGU 194 Reactor Cooling 无源汇带上下界可行流

    Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output ...

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

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

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

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

  6. HDU 4940 Destroy Transportation system(无源汇有上下界最大流)

    看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...

  7. hdu 4940 无源汇有上下界最大流

    /* <img src="http://img.blog.csdn.net/20140823174212937?watermark/2/text/aHR0cDovL2Jsb2cuY3N ...

  8. LOJ [#115. 无源汇有上下界可行流](https://loj.ac/problem/115)

    #115. 无源汇有上下界可行流 先扔个板子,上下界的东西一点点搞,写在奇怪的合集里面 Code: #include <cstdio> #include <cstring> # ...

  9. 2018.08.20 loj#115. 无源汇有上下界可行流(模板)

    传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...

  10. [loj#115] 无源汇有上下界可行流 网络流

    #115. 无源汇有上下界可行流 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据   题 ...

随机推荐

  1. 解读中兴通信在物联网行业如何践行DDD

    此前,在由 ThoughtWorks 举办的领域驱动设计峰会 DDD-China 2019 上,InfoQ 记者就开发团队为何需要 DDD.目前业界实践 DDD 的挑战等问题对中兴通讯资深软件架构师张 ...

  2. Java 从入门到进阶之路(十九)

    在之前的文章我们介绍了一下 Java 中的Object,本章我们来看一下 Java 中的包装类. 在 Java 中有八个基本类型:byte,short,int,long,float,double,ch ...

  3. 【温故知新】Java web 开发(三)Form表单与上传下载文件

    简介:在一和二的基础之上,这次来记录下如何在页面提交表单数据,以及文件的上传和下载整个流程,请求也不仅限于GET了,也有POST了. 1. 为了方便,在 webapp 下直接新建一个 index.ht ...

  4. hexo+next 详细搭建

    安装node node下载地址:http://nodejs.cn/download/ 具体安装方法,这里不做详写 安装完成可以通过node -v 查看安装是否生效和node的版本 我这里使用的是v10 ...

  5. $ [Contest \#4]$求和 思博题

    正解: 解题报告: 传送门$QwQ$ 一道看起来是数位$dp$其实并不是的题$QwQ$ 首先求$\sum_{l}^r$就变成$\sum_1^r-\sum_1^{l-1}$不说$QwQ$.现在就只要求$ ...

  6. 洛谷$P$2468 粟粟的书架 $[SDOI2010]$ 主席树

    正解:主席树 解题报告: 传送门! 题目大意是说,给定一个矩形,然后每次会给一个,这个大矩形中的一个小矩形,询问从小矩形中最少选多少个数字能满足它们之和大于等于给定数字$x$ 看起来很神的样子,完全不 ...

  7. JVM探秘:垃圾收集器

    本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. 垃圾收集器 垃圾收集算法是是内存回收的方法论,垃圾收集器是内存回收的具体实现.不同的虚 ...

  8. 从桌面到 Web -- 领域模型

    让我们暂时告别一下 ASP.NET Core 先介绍一下这个虚拟项目.因为我的主要目的是通过一个项目,全面学习一下 ASP.NET Core,所以这个项目时一个很简单的,不具备实际应用价值的虚拟项目, ...

  9. 关于MySQL幻读的实验

    该实验基于 CentOS 7 + MySQL 5.7 进行 打开两个窗口连接到MySQL 第一个连接的事务我们命名为  T1 第二个连接的事务我们命名为 T2 T2 发生在 T1 的 O1 操作结束以 ...

  10. Spring Boot中利用递归算法查询到所有下级用户,并手动进行分页

    Spring Boot中利用递归算法查询到所有下级用户,并手动进行分页 前提:语言用的是kotlin(和Java一样,但更简洁),写下这篇文章用来记录编程过程中遇到的一些难点 1.功能需求 前端用户A ...