[loj#115] 无源汇有上下界可行流 网络流
#115. 无源汇有上下界可行流
题目描述
这是一道模板题。
n nn 个点,m mm 条边,每条边 e ee 有一个流量下界 lower(e) \text{lower}(e)lower(e) 和流量上界 upper(e) \text{upper}(e)upper(e),求一种可行方案使得在所有点满足流量平衡条件的前提下,所有边满足流量限制。
输入格式
第一行两个正整数 n nn、m mm。
之后的 m mm 行,每行四个整数 s ss、t tt、lower \text{lower}lower、upper \text{upper}upper。
输出格式
如果无解,输出一行 NO
。
否则第一行输出 YES
,之后 m mm 行每行一个整数,表示每条边的流量。
样例
样例输入 1
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
样例输出 1
NO
样例输入 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
样例输出 2
YES
1
2
3
2
1
1
数据范围与提示
1≤n≤200,1≤m≤10200 1 \leq n \leq 200, 1 \leq m \leq 102001≤n≤200,1≤m≤10200
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define maxm 12000
#define maxn 500
using namespace std;
int read() {
int x=,f=;char ch=getchar();
while(!isdigit(ch)){ch=getchar();}
while(isdigit(ch)){x=x*+ch-'';ch=getchar();}
return x;
}
struct data {
int to,next,w,f;
}e[maxm*];
int head[maxn],cnt;
int cur[maxn];
void add(int u,int v,int w,int f){e[cnt].next=head[u];e[cnt].to=v;e[cnt].w=w;e[cnt].f=f;head[u]=cnt++;}
int n,m,s,t;
int q[maxn];
bool vis[maxn];
int dis[maxn];
bool bfs() {
memset(dis,-,sizeof(dis));
int h=,tt=;
q[h]=t;
vis[t]=;
dis[t]=;
while(h!=tt) {
int now=q[h];h++;vis[now]=;if(h==maxn) h=;
for(int i=head[now];i>=;i=e[i].next) {
int to=e[i].to;
if(e[i^].w&&dis[to]<-) {
dis[to]=dis[now]-;
if(!vis[to]){
vis[to]=;
q[tt++]=to;if(tt==maxn) tt=;
}
}
}
}
return dis[s]>=-;
}
int dfs(int now,int a) {
if(now==t||a==) return a;
int flow=,f;
for(int i=cur[now];i>=;i=e[i].next) {
int to=e[i].to;
if(dis[to]==dis[now]+&&e[i].w>&&(f=dfs(to,min(a,e[i].w)))) {
e[i].w-=f;
e[i^].w+=f;
flow+=f;
a-=f;
if(a==) return flow;
}
cur[now]=i;
}
if(!flow) dis[now]=-;
return flow;
}
int main() {
memset(head,-,sizeof(head));
n=read(),m=read(),s=,t=n+;
int tot=;
for(int i=;i<=m;i++) {
int u=read(),v=read(),lw=read(),w=read();
tot+=lw;
add(u,v,w-lw,w);add(v,u,,);
add(,v,lw,lw);add(v,,,lw);
add(u,n+,lw,lw);add(n+,u,,);
}
int ans=;
while(bfs()){
for(int i=;i<=n+;i++) cur[i]=head[i];
ans+=dfs(s,);
}
if(ans==tot) {
printf("YES\n");
for(int i=;i<m*;i+=) {printf("%d\n",e[i].f-e[i].w);}
return ;
}
printf("NO\n");
}
[loj#115] 无源汇有上下界可行流 网络流的更多相关文章
- LOJ [#115. 无源汇有上下界可行流](https://loj.ac/problem/115)
#115. 无源汇有上下界可行流 先扔个板子,上下界的东西一点点搞,写在奇怪的合集里面 Code: #include <cstdio> #include <cstring> # ...
- 2018.08.20 loj#115. 无源汇有上下界可行流(模板)
传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...
- loj#115. 无源汇有上下界可行流
\(\color{#0066ff}{ 题目描述 }\) 这是一道模板题. \(n\) 个点,\(m\) 条边,每条边 \(e\) 有一个流量下界 \(\text{lower}(e)\) 和流量上界 \ ...
- LibreOJ #115. 无源汇有上下界可行流
二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...
- 【LOJ115】无源汇有上下界可行流(模板题)
点此看题面 大致题意: 给你每条边的流量上下界,让你判断是否存在可行流.若有,则还需输出一个合法方案. 大致思路 首先,每条边既然有一个流量下界\(lower\),我们就强制它初始流量为\(lower ...
- Zoj 2314 Reactor Cooling(无源汇有上下界可行流)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向 ...
- 无源汇有上下界可行流(ZQU 1590)
无源汇有上下界可行流(也就是循环流) 模型:一个网络,求出一个流,使得每条边的流量必须>=Li且<=Hi, 每个点必须满足总流入量=总流出量(流量守恒)(这个流的特点是循环往复,无始无终) ...
- 【模板】无源汇有上下界可行流(网络流)/ZOJ2314
先导知识 网络最大流 题目链接 https://vjudge.net/problem/ZOJ-2314 题目大意 多组数据,第一行为数据组数 \(T\). 对于每一组数据,第一行为 \(n,m\) 表 ...
- ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...
随机推荐
- Windows IRP
IRP(I/O Request Packet),是由IO manager发起的对device的IO请求. 当用户调用系统API,如createFile类似的函数,其实是会交给IO manager来做相 ...
- Shiro 的 HelloWorld
密码文件 [users] zhang=123 wang=123 测试 package org.zln.hello; import org.apache.log4j.LogManager; import ...
- android开发中常犯的几个错误整理
新手程序猿,在开发中难免会犯各种各样的错误,以下是整理的一些android开发中常见的错误,一起来看看吧. 1.避免将多个类放在一个文件夹里面,除非是一次性使用的内部类. 就是一个文件,最好给分它同名 ...
- P4462 [CQOI2018]异或序列
题目描述 已知一个长度为n的整数数列 a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an ,给定查询参数l.r,问在 al,al+1,...,ara_l,a_{l+1 ...
- 后缀数组 模板题 hdu1403(最长公共(连续)子串)
好气啊,今天没有看懂后缀树和后缀自动机 只能写个后缀数组发泄一下了orz #include <cstdio> #include <cstring> *; int wa[N], ...
- TextView AutoLink, ClikSpan 与长按事件冲突的解决
前言 首先,我们先来复习一下 autoLink 和 ClickableSpan 是干什么用的. autoLink 当中有五个属性值:分别是 phone.email.map.web.all 和 none ...
- HDU4289:Control(最小割)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem
D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...
- Hadoop NameNode元数据相关文件目录解析
在<Hadoop NameNode元数据相关文件目录解析>文章中提到NameNode的$dfs.namenode.name.dir/current/文件夹的几个文件: 1 current/ ...
- gitlab迁移升级
一.迁移步骤 1.首先安装最新版本gitlab(gitlab7.2安装) 2.停止旧版本gitlab服务 3.将旧的项目文件完整导入新的gitlab bundle exec rake gitlab:i ...