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

内存限制:256 MiB时间限制:1000 ms标准输入输出
题目类型:传统评测方式:Special Judge
上传者: 匿名

题目描述

这是一道模板题。

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] 无源汇有上下界可行流 网络流的更多相关文章

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

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

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

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

  3. loj#115. 无源汇有上下界可行流

    \(\color{#0066ff}{ 题目描述 }\) 这是一道模板题. \(n\) 个点,\(m\) 条边,每条边 \(e\) 有一个流量下界 \(\text{lower}(e)\) 和流量上界 \ ...

  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. D - 小木棒

    D - 小木棒 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem Descri ...

  2. JAVA第八周课堂笔记

  3. about !dbgprint to analyze BSOD dump file.

    基本规则: 只有debug mode enable的机器,产生的dump file才会保存dbgprint的buffer. 默认!dbgprint的buffer size是4k. 增加buffer s ...

  4. Flink之状态之状态存储 state backends

    流计算中可能有各种方式来保存状态: 窗口操作 使用 了KV操作的函数 继承了CheckpointedFunction的函数 当开始做checkpointing的时候,状态会被持久化到checkpoin ...

  5. CF#366 704D Captain America 上下界网络流

    CF上的题,就不放链接了,打开太慢,直接上题面吧: 平面上有n个点, 第 i 个点的坐标为 ($X_i ,Y_i$), 你需要把每个点染成红色或者蓝色, 染成红色的花费为 r , 染成蓝色的花费为 b ...

  6. [Leetcode] 3sum 三数和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  7. 论文讨论&&思考《Deformable Convolutional Networks》

    这篇论文真是让我又爱又恨,可以说是我看过的最认真也是最多次的几篇paper之一了,首先deformable conv的思想我觉得非常好,通过end-to-end的思想来做这件事也是极其的make se ...

  8. php模式-数据映射模式

    概念:简言之,数据映射模式就是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作. 深入理解:数据映射,是在持久化数据存储层(一般是关系型数据库)和驻于内存的数据表现层之间进行双向数据 ...

  9. Cannot load project: com.intellij.ide.plugins.PluginManager$StartupAbortedException

    今天电脑突然蓝屏,idea异常关闭,开机重启后,打开idea,点击项目出现 Cannot load project: com.intellij.ide.plugins.PluginManager$St ...

  10. 100个Swift必备Tips(第二版)

    100个Swift必备Tips(第二版) 新年第一天,给大家一本电子书,希望新的一年里,步步高升. GitHub