假设初始流为每条边的下界。但这样可能流量会不守恒,我们需要在上面加上一个附加流使流量守恒。只要让每个点开始的出/入流量与原初始流相等就可以求出附加流了。那么新建超源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 无源汇有上下界可行流(上下界网络流)的更多相关文章

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

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

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

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

  3. ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...

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

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

  5. hdu 4940 Destroy Transportation system (无源汇上下界可行流)

    Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 ...

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

    Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...

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

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

  8. zoj2314 无源汇上下界可行流

    题意:看是否有无源汇上下界可行流,如果有输出流量 题解:对于每一条边u->v,上界high,下界low,来说,我们可以建立每条边流量为high-low,那么这样得到的流量可能会不守恒(流入量!= ...

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

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

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

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

随机推荐

  1. oa tomcat 代码处理跨域问题

    meta标签处理http.https跨域 <!-- 将http请求转成https请求 --> <meta http-equiv="Content-Security-Poli ...

  2. [01-01] 示例:用Java爬取新闻

    1.分析url <空港双流>数字报刊,访问地址为:http://epaper.slnews.net.cn,现在为了抓取每篇新闻的网页内容. 在浏览器访问该链接后,发现链接出现了变化,看样子 ...

  3. [拍摄]日本AVENIR 6-36mm老式变焦镜头拆解 型号SSL06036M

    老式监控摄像头的变焦镜头,做工不错,拆了分享一下 品牌:AVENIR型号:SSL06036M光圈:1:1.2产地:日本焦距:6-36mm 外观 图片:QQ截图20141104125759.jpg 图片 ...

  4. ASP.NET Core MVC中URL和数据模型的匹配

    Http GET方法 首先我们来看看GET方法的Http请求,URL参数和ASP.NET Core MVC中Controller的Action方法参数匹配情况. 我定义一个UserController ...

  5. Elastic 技术栈之快速入门

    Elastic 技术栈之快速入门 概念 ELK 是什么 ELK 是 elastic 公司旗下三款产品 ElasticSearch .Logstash .Kibana 的首字母组合. ElasticSe ...

  6. git log 的常用选项

  7. Centos7 -- glibc 升级失败、意外删除、故意删除后的处理方法

    第一部分:测试(如果不是想测试效果,可以直接跳到第三部分) 鉴于不久前 glibc-2.29 升级失败导致一系列的工具无法正常使用,‘’ 本着研究精神的我决定删除 glibc及其库文件 ,测试影响范围 ...

  8. 记一次MongoDB裸奔

    导言 大意失荆州,裸奔的 MongoDB 被黑了.虽然并不是什么非常重要的数据,但也给自己敲响的一个警钟.虽然我们平时不容易接触到数据安全,但我们在开发,部署项目的时候,一定要养成良好的安全意识. 根 ...

  9. spring boot 集成Druid

    Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...

  10. python-小知识点-14

    ''' python2 python3 ''' #python2 print() print 'abc' range() xrange() 生成器 raw_input() #python3 print ...