题目链接: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. 还在拼字符串?试试HTML5的template标签

    HTML5中<template>标签的详细介绍(图文) 这篇文章主要介绍了HTML5中的template标签,是HTML5入门中的重要知识,需要的朋友可以参考 一.HTML5 templa ...

  2. 22.json&pickle&shelve

    转载:https://www.cnblogs.com/yuanchenqi/article/5732581.html json 之前我们学习过用eval内置方法可以将一个字符串转成python对象,不 ...

  3. 使用 Git和Github

    本地库初始化 git init 效果: 注意: .git 目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡 乱修改. 设置签名 其实就是为了区分不同开发人员的身份需要设置一下自己的信息,包括 ...

  4. Spring Boot 整合 slf4j+log4j 实现日志管理

    一:首先新建一个jar项目,如下图: 二:添加log4j的依赖,如下pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0 ...

  5. DOCKER学习_010:Docker的文件系统以及制作镜像

    一 文件系统简介 1.1 Linux文件系统 LInux空间组成分为内核空间和用户空间(使用rootfs) linux文件系统由 bootes和 rootfs组成, bootes主要包含boot1 o ...

  6. 超详细!如何利用Huginn制作专属RSS

    前言 本文首发于个人网站,欢迎订阅.本篇博文接上利用Feed43为网站自制RSS源,上一篇讲解了RSS的简介以及利用Feed43自制专属RSS,Feed43有其优势,缺陷也很明显,不能高度自定义.有的 ...

  7. 组件与props简解

    一.创建组件 1.函数式创建 特点: 组件不能访问this对象 组件无法访问生命周期的方法 无状态组件只能访问输入的props,无副作用 function Title() { return <h ...

  8. Dockerfile文件记录(用于后端项目部署)

    Dockerfile文件记录(用于后端项目部署) 本教程依据个人理解并经过实际验证为正确,特此记录下来,权当笔记. 注:基于linux操作系统(敏感信息都进行了处理) 此文结合另一篇博客共同构成后端服 ...

  9. P2722 总分 Score Inflation (完全背包模板)

    题目传送门:P2722 总分 Score Inflation 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类"是指一个竞赛题目的集合,解决集合中的题目需要相同多的时 ...

  10. 《C++Primer》第五版习题答案--第二章【学习笔记】

    C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...