题目链接: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. 曹工说Spring Boot源码(6)-- Spring怎么从xml文件里解析bean的

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  2. Python3 批量修改JPG图片尺寸

    功能 批量修改当前文件夹下的jpg图片到设置的尺寸 将修改后的图片移动到 new_img 这个文件夹下 导入库 from PIL import Image # 处理图片模块 import os imp ...

  3. Linux中安装软件和各种常用命令

    1.Centos7中安装mysql5.7的链接:http://blog.csdn.net/fanpeizhong/article/details/73557202 2.修改mysql默认密码的链接:h ...

  4. CentOS 下 git 401 Unauthorized while accessing 问题解决

    The requested URL returned error: 401 Unauthorized while accessing 这个一般是旧版git的问题,需要安装新版的.CentOS 想下载最 ...

  5. $ CometOJ-Contest\#11\ D$ $Kruscal$重构树

    正解:$Kruscal$重构树 解题报告: 传送门$QwQ$ 发现一个图上搞就很麻烦,考虑变为生成树达到原有效果. 因为在询问的时候是要求走到的点编号尽量小,发现这个时候点的编号就成为限制了,于是不难 ...

  6. 洛谷$P2617\ Dynamic\ Rankings$ 整体二分

    正解:整体二分 解题报告: 传送门$w$ 阿查询带修区间第$k$小不显然整体二分板子呗,,, 就考虑先按时间戳排序(,,,其实并不需要读入的时候就按着时间戳排的鸭$QwQ$ 每次二分出$mid$先把所 ...

  7. 洛谷$P3308\ [SDOI2014]LIS$ 网络流

    正解:网络流 解题报告: 传送门$QwQ$ 恩先不考虑关于那个附加属性的限制,考虑这题怎么做? 首先这题从名字开始就让人忍不住联想起网络流24题里的那个最长不下降子序列?于是同样考虑预处理一个$f$呗 ...

  8. JVM性能优化系列-(1) Java内存区域

    1. Java内存区域 1.1 运行时数据区 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.主要包括:程序计数器.虚拟机栈.本地方法栈.Java堆.方法区(运 ...

  9. 客户端进行定位(无地图API)

    需求: 根据用户浏览的所在城市加载相应的县级列表 思路: 使用搜索的服务找出当前用户的IP,然后使用百度的服务通过IP进行定位 源码: <!DOCTYPE html> <html&g ...

  10. echarts圆饼图设置默认选中项并在中间显示文字

    效果: 代码: var myChart = echarts.init(document.getElementById('quanshi-echarts-two')); option = { grid: ...