\(\color{#0066ff}{ 题目描述 }\)

这是一道模板题。

\(n\) 个点,\(m\) 条边,每条边 \(e\) 有一个流量下界 \(\text{lower}(e)\) 和流量上界 \(\text{upper}(e)\),求一种可行方案使得在所有点满足流量平衡条件的前提下,所有边满足流量限制。

$\color{#0066ff}{ 输入格式 } $

第一行两个正整数 \(n\)、\(m\)。

之后的 \(m\) 行,每行四个整数 \(s\)、\(t\)、\(\text{lower}\)、\(\text{upper}\)。

\(\color{#0066ff}{输出格式}\)

如果无解,输出一行 NO

否则第一行输出 YES,之后 \(m\) 行每行一个整数,表示每条边的流量。

\(\color{#0066ff}{输入样例}\)

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

\(\color{#0066ff}{输出样例}\)

NO

YES
1
2
3
2
1
1

\(\color{#0066ff}{数据范围与提示}\)

1≤n≤200,1≤m≤10200

\(\color{#0066ff}{ 题解 }\)

无源汇有上下界可行流判断

无源汇是没有s和t

有上下界是指每个边的流量要在一个\([l,r]\)内

可行流,指的是每条边都要有一个合法流,使得对于任意一个点入流=出流

这个要怎么求?

对于一条边\(x\to y\),上下界为[l,r]

显然如果成立,这条边最少流l

把一条边拆成两条,一条容量为r-l, 一条容量为l,那么容量为l的那条边是一定要流满的

我们建立一个超级源s和超级汇t

对于\(x\to y\)

从x到y连容量为r-l的边, 向y连容量为l的边, x向t连容量为l的边

即强制给yl的流,通过一些环(其它路径)流到x

最后只需判断s出去的边的所有容量和与最大流是否相等即可

当且仅当所有l的边都流满了才有解,对于那些r-l的边,随便流多少,一定在范围内的

最后实际上每条边的流量就是r-l的边的流量+l

#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 3e4 + 10;
struct node {
int to, dis, id;
node *nxt, *rev;
node(int to = 0, int dis = 0, int id = 0, node *nxt = NULL, node *rev = NULL)
: to(to), dis(dis), id(id), nxt(nxt), rev(rev) {}
void *operator new(size_t) {
static node *S = NULL, *T = NULL;
return (S == T) && (T = (S = new node[1024]) + 1024), S++;
}
}*head[maxn], *cur[maxn];
int n, m, s, t, dep[maxn], ans[maxn], d[maxn];
void add(int from, int to, int c, int id) {
head[from] = new node(to, c, id, head[from], NULL);
}
void link(int from, int to, int c, int id) {
add(from, to, c, 0);
add(to, from, 0, id);
head[from]->rev = head[to];
head[to]->rev = head[from];
}
bool bfs() {
std::queue<int> q;
for(int i = s; i <= t; i++) dep[i] = 0, cur[i] = head[i];
dep[s] = 1;
q.push(s);
while(!q.empty()) {
int tp = q.front(); q.pop();
for(node *i = head[tp]; i; i = i->nxt)
if(!dep[i->to] && i->dis)
dep[i->to] = dep[tp] + 1, q.push(i->to);
}
return dep[t];
}
int dfs(int x, int change) {
if(x == t || !change) return change;
int flow = 0, ls;
for(node *i = cur[x]; i; i = i->nxt) {
cur[x] = i;
if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(change, i->dis)))) {
flow += ls;
change -= ls;
i->dis -= ls;
i->rev->dis += ls;
if(!change) break;
}
}
return flow;
}
int dinic() {
int flow = 0;
while(bfs()) flow += dfs(s, 0x7ffffff);
return flow;
} int main() {
n = in(), m = in();
s = 0, t = n + 1;
int x, y, l, r, tot = 0;
for(int i = 1; i <= m; i++) {
x = in(), y = in(), l = in(), r = in();
d[i] = l;
link(x, y, r - l, i);
link(s, y, l, 0);
link(x, t, l, 0);
tot += l;
}
if(tot == dinic()) {
for(int i = 1; i <= n; i++)
for(node *j = head[i]; j; j = j->nxt)
if(j->id)
ans[j->id] = d[j->id] + j->dis;
printf("YES\n");
for(int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
else printf("NO");
return 0;
}

loj#115. 无源汇有上下界可行流的更多相关文章

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

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

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

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

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

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

  4. LibreOJ #115. 无源汇有上下界可行流

    二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...

  5. 【LOJ115】无源汇有上下界可行流(模板题)

    点此看题面 大致题意: 给你每条边的流量上下界,让你判断是否存在可行流.若有,则还需输出一个合法方案. 大致思路 首先,每条边既然有一个流量下界\(lower\),我们就强制它初始流量为\(lower ...

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

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

  7. 无源汇有上下界可行流(ZQU 1590)

    无源汇有上下界可行流(也就是循环流) 模型:一个网络,求出一个流,使得每条边的流量必须>=Li且<=Hi, 每个点必须满足总流入量=总流出量(流量守恒)(这个流的特点是循环往复,无始无终) ...

  8. 【模板】无源汇有上下界可行流(网络流)/ZOJ2314

    先导知识 网络最大流 题目链接 https://vjudge.net/problem/ZOJ-2314 题目大意 多组数据,第一行为数据组数 \(T\). 对于每一组数据,第一行为 \(n,m\) 表 ...

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

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

随机推荐

  1. PHP类(一)-类的实例化

    类的实例化就是对象.一个类可以分成两个部分,一个是静态描述,就是类里的成员属性.第二个是动态描述,就是类里的成员方法,也就是对象的功能. 声明一个类,可以在class前加一些关键字,如abstract ...

  2. CentOS 7.2 部署Rsync + Lsyncd服务实现文件实时同步/备份 (二)

    发送端配置: 一.配置密钥 1. 主/从服务器之间启用基于密钥的身份验证.登录发送端服务器并用 " ssh-keygen " 命令生成公共或私有的密钥. 2. 使用 " ...

  3. C#理解泛型(源代码)及 default(T)

    1.类型不安全.且代码无法遍历重用的源代码. 2.泛型源代码 源代码下载: http://files.cnblogs.com/files/qqhfeng/ConsoleApplication1.rar

  4. nginx注册成服务

    http://blog.csdn.net/t37240/article/details/51727563

  5. C++深度解析教程学习笔记(1)C到C++的升级

    1.现代软件产品架构图 比如商场收银系统 2.C 到 C++ 的升级 2.1变量的定义 C++中所有的变量都可以在需要使用时再定义,而 C 语言中的变量都必须在作用域开始位置定义. 2.2 regis ...

  6. python dict.fromkeys()研究

    def unique(seq): #return [x for x in my_list if x not in locals()['_[1]']] return {}.fromkeys(seq).k ...

  7. ssh框架搭建实例代码教程步骤

    http://blog.csdn.net/u010539352/article/details/49255729

  8. Bytes和bits的区别(字节和位的区别)

    基本概念 Bit意为“位”或“比特”,是计算机运算的基础,属于二进制的范畴: Byte意为“字节”,是计算机文件大小的基本计算单位: 这两者应用的场合不同.通常用bit来作数据传输的单位,因为物理层, ...

  9. 【263】Linux 添加环境变量 & 全局 shell 脚本

    Linux电脑添加环境变量 方法一:通过修改 profile 文件添加环境变量 1. 打开终端,输入[vi /etc/profile],如下所示,点击回车 [ocean@ygs-jhyang-w1 L ...

  10. C++的继承与接口

    1.继承方式 三种继承方式,public,private,protected.注意,继承方式是相对于某一层类的方法而言,并不是相对于子类的对象而言.对于外部世界()对象来说,protected和pri ...