LOJ115 无源汇有上下界可行流(上下界网络流)
假设初始流为每条边的下界。但这样可能流量会不守恒,我们需要在上面加上一个附加流使流量守恒。只要让每个点开始的出/入流量与原初始流相等就可以求出附加流了。那么新建超源S超汇T,令degree[i]表示流入i的边的下界之和-从i流出的边的下界之和。
若degree[i]>0,则表示需要有额外degree[i]的流量流入i来达到流量平衡,那么从S向i连上界为degree[i]的边。
若degree[i]<0,则表示需要有额外degree[i]的流量从i流出来达到流量平衡,那么从i向T连上界为-degree[i]的边。
跑最大流就可以求出附加流。显然maxflow<=sigma(degree[i])。如果maxflow=sigma(degree[i]),那么有可行流。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 210
#define M 50000
#define S 0
#define T 201
#define inf 1000000000
int n,m,t=-,p[N],degree[N],l[M],tot=;
int cur[N],d[N],q[N],ans=;
struct data{int to,nxt,cap,flow;
}edge[M];
void addedge(int x,int y,int z)
{
t++;edge[t].to=y,edge[t].nxt=p[x],edge[t].cap=z,edge[t].flow=,p[x]=t;
t++;edge[t].to=x,edge[t].nxt=p[y],edge[t].cap=,edge[t].flow=,p[y]=t;
}
bool bfs()
{
memset(d,,sizeof(d));d[S]=;
int head=,tail=;q[]=S;
do
{
int x=q[++head];
for (int i=p[x];~i;i=edge[i].nxt)
if (d[edge[i].to]==-&&edge[i].flow<edge[i].cap)
{
d[edge[i].to]=d[x]+;
q[++tail]=edge[i].to;
}
}while (head<tail);
return ~d[T];
}
int work(int k,int f)
{
if (k==T) return f;
int used=;
for (int i=cur[k];~i;i=edge[i].nxt)
if (d[k]+==d[edge[i].to])
{
int w=work(edge[i].to,min(f-used,edge[i].cap-edge[i].flow));
edge[i].flow+=w,edge[i^].flow-=w;
if (edge[i].flow<edge[i].cap) cur[k]=i;
used+=w;if (used==f) return f;
}
if (used==) d[k]=-;
return used;
}
void dinic()
{
while (bfs())
{
memcpy(cur,p,sizeof(p));
ans+=work(S,inf);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("loj115.in","r",stdin);
freopen("loj115.out","w",stdout);
const char LL[]="%I64d";
#else
const char LL[]="%lld";
#endif
n=read(),m=read();
memset(p,,sizeof(p));
for (int i=;i<=m;i++)
{
int x=read(),y=read(),low=read(),high=read();
addedge(x,y,high-low);
degree[y]+=low,degree[x]-=low;
l[i]=low;
}
for (int i=;i<=n;i++)
if (degree[i]>) addedge(S,i,degree[i]),tot+=degree[i];
else if (degree[i]<) addedge(i,T,-degree[i]);
dinic();
if (ans<tot) cout<<"NO";
else
{
cout<<"YES\n";
for (int i=;i<=m;i++)
printf("%d\n",edge[i-<<].flow+l[i]);
}
return ;
}
LOJ115 无源汇有上下界可行流(上下界网络流)的更多相关文章
- 【LOJ115】无源汇有上下界可行流(模板题)
点此看题面 大致题意: 给你每条边的流量上下界,让你判断是否存在可行流.若有,则还需输出一个合法方案. 大致思路 首先,每条边既然有一个流量下界\(lower\),我们就强制它初始流量为\(lower ...
- LOJ [#115. 无源汇有上下界可行流](https://loj.ac/problem/115)
#115. 无源汇有上下界可行流 先扔个板子,上下界的东西一点点搞,写在奇怪的合集里面 Code: #include <cstdio> #include <cstring> # ...
- ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...
- 2018.08.20 loj#115. 无源汇有上下界可行流(模板)
传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...
- hdu 4940 Destroy Transportation system (无源汇上下界可行流)
Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 ...
- zoj 2314 Reactor Cooling (无源汇上下界可行流)
Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...
- [loj#115] 无源汇有上下界可行流 网络流
#115. 无源汇有上下界可行流 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据 题 ...
- zoj2314 无源汇上下界可行流
题意:看是否有无源汇上下界可行流,如果有输出流量 题解:对于每一条边u->v,上界high,下界low,来说,我们可以建立每条边流量为high-low,那么这样得到的流量可能会不守恒(流入量!= ...
- loj#115. 无源汇有上下界可行流
\(\color{#0066ff}{ 题目描述 }\) 这是一道模板题. \(n\) 个点,\(m\) 条边,每条边 \(e\) 有一个流量下界 \(\text{lower}(e)\) 和流量上界 \ ...
- Zoj 2314 Reactor Cooling(无源汇有上下界可行流)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向 ...
随机推荐
- not annotated with HTTP method type (ex. GET, POST) 问题解决
大多数情况是真的而没有写method = RequestMethod.GET.POST等注解, 有时这么写了也报类似异常,如下 @FeignClient("microservice-prov ...
- TCP/IP协议--TCP的交互数据流和成块数据流
前边讲了TCP连接的建立和终止,分别要三次握手和四次通信.这些报文段都只包含首部,没有数据部分. 这里就讲讲数据传送的一些细节.一个TCP连接建立成功以后,就可以开始传送数据了~ 一般TCP数据 ...
- Unity报错 GameObject is already being activated or deactivated
unity 在OnDisable 方法里设置父节点会报这个错. void OnDisable() { // transform.parent = oldParent; transform.SetPar ...
- javascript闭包的使用--按钮切换
闭包实现按钮状态切换 看下面的代码: var toggleBtn = document.getElementById('toggle'); var toggleFun = (function() { ...
- ThinkPad T43续命记
// Description: 原作于2016年8月25日. Mr. Robot 最近有部叫<黑客军团>(Mr. Robot)的戏比较火.目前第二季已经出到一大半了,深得技术宅和技术宅仰慕 ...
- VitualBox安装linux记录
下载镜像 CentOS 7镜像下载 阿里云站点:http://mirrors.aliyun.com/centos/7/isos/x86_64/ VirtualBox安装linux https://ww ...
- js值----你所不知道的JavaScript系列(6)
1.数组 在 JavaScript 中,数组可以容纳任何类型的值,可以是字符串.数字.对象(object),甚至是其他数组(多维数组就是通过这种方式来实现的) .----<你所不知道的JavaS ...
- Quartz_简单使用
第一步:安装 新建一个QuartzDemo项目后,安装下面的程序包 Install-Package Quartz Install-Package Common.Logging.Log4Net1211 ...
- [转载]sql 盲注之正则表达式攻击
[转载]sql 盲注之正则表达式攻击 -----------------------------------------MYSQL 5+-------------------------------- ...
- open-falcon ---安装Dashboard时候报错"SSLError: The read operation timed out"
在部署open-falcon环境过程中,安装Dashboard时候报错"SSLError: The read operation timed out".如下: [root@open ...