当前位置: 首页 > news >正文

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

#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 102001n200,1m10200

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define maxm 12000
 8 #define maxn 500
 9 using namespace std;
10 int read() {
11     int x=0,f=1;char ch=getchar();
12     while(!isdigit(ch)){ch=getchar();}
13     while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
14     return x;
15 }
16 struct data {
17     int to,next,w,f;
18 }e[maxm*16];
19 int head[maxn],cnt;
20 int cur[maxn];
21 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++;}
22 int n,m,s,t;
23 int q[maxn];
24 bool vis[maxn];
25 int dis[maxn];
26 bool bfs() {
27     memset(dis,-97,sizeof(dis));
28     int h=0,tt=1;
29     q[h]=t;
30     vis[t]=1;
31     dis[t]=0;
32     while(h!=tt) {
33         int now=q[h];h++;vis[now]=0;if(h==maxn) h=0;
34         for(int i=head[now];i>=0;i=e[i].next) {
35             int to=e[i].to;
36             if(e[i^1].w&&dis[to]<-1000000) {
37                 dis[to]=dis[now]-1;
38                 if(!vis[to]){
39                     vis[to]=1;
40                     q[tt++]=to;if(tt==maxn) tt=0;
41                 }
42             }
43         }
44     }
45     return dis[s]>=-1000000;
46 }
47 int dfs(int now,int a) {
48     if(now==t||a==0) return a;
49     int flow=0,f;
50     for(int i=cur[now];i>=0;i=e[i].next) {
51         int to=e[i].to;
52         if(dis[to]==dis[now]+1&&e[i].w>0&&(f=dfs(to,min(a,e[i].w)))) {
53             e[i].w-=f;
54             e[i^1].w+=f;
55             flow+=f;
56             a-=f;
57             if(a==0) return flow;
58         }
59         cur[now]=i;
60     }
61     if(!flow) dis[now]=-1;
62     return flow;
63 }
64 int main() {
65     memset(head,-1,sizeof(head));
66     n=read(),m=read(),s=0,t=n+1;
67     int tot=0;
68     for(int i=1;i<=m;i++) {
69         int u=read(),v=read(),lw=read(),w=read();
70         tot+=lw;
71         add(u,v,w-lw,w);add(v,u,0,0);
72         add(0,v,lw,lw);add(v,0,0,lw);
73         add(u,n+1,lw,lw);add(n+1,u,0,0);
74     }
75     int ans=0;
76     while(bfs()){
77         for(int i=0;i<=n+1;i++) cur[i]=head[i];
78         ans+=dfs(s,2147483647);
79     }
80     if(ans==tot) {
81         printf("YES\n");
82         for(int i=0;i<m*6;i+=6) {printf("%d\n",e[i].f-e[i].w);}
83         return 0;
84     }
85     printf("NO\n");
86 }
View Code

 

转载于:https://www.cnblogs.com/wls001/p/7867630.html

相关文章:

  • php程序设计形成性手册,PHP动态网站设计(专,2020春)形成性考核_第6章 单元测试0...
  • linux命令行动态输出,Linux top实时显示process的动态命令详解
  • 我的cheatsheet
  • linux文件赋予用户权限,Linux 给用户赋予操作权限
  • Ubuntu 16.04安装JAD反编译工具(Java)
  • 查询linux命令位置,查看登录过Linux的IP的地理位置(基于last命令)
  • [poj] 3974 Palindrome
  • linux遍历目录删除指定文件,shell脚本删除目录下的指定文件
  • 【转】VC++计算当前时间点间隔N天的时间(不使用CTimeSpan类)
  • linux下新建shell命令接口,Linux Shell(脚本)编程入门
  • Ubuntu下搭建基于apache2的gerrit+gitweb服务器
  • Linux每个用户单独配置ssh,linux – 每个用户的SSH MOTD
  • linux针对内存uce隔离内存,Linux运维知识之在linux系统中,iomem_resource的信息被输出到/proc/iomem中...
  • intellij IDEA里各图标对应的文件类型
  • linux目录中grid,用MongoDB基于GridFS存储文件
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • angular2 简述
  • eclipse(luna)创建web工程
  • Elasticsearch 参考指南(升级前重新索引)
  • extjs4学习之配置
  • Java精华积累:初学者都应该搞懂的问题
  • Js基础知识(一) - 变量
  • Less 日常用法
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • OSS Web直传 (文件图片)
  • Promise面试题2实现异步串行执行
  • tweak 支持第三方库
  • 二维平面内的碰撞检测【一】
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 如何合理的规划jvm性能调优
  • 我与Jetbrains的这些年
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • 异常机制详解
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • ionic入门之数据绑定显示-1
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • 浅谈sql中的in与not in,exists与not exists的区别
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • ​马来语翻译中文去哪比较好?
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (13)Hive调优——动态分区导致的小文件问题
  • (2)STL算法之元素计数
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (LeetCode C++)盛最多水的容器
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • ./configure、make、make install 命令