Loj#116-[模板]有源汇有上下界最大流
正题
题目链接:https://loj.ac/p/116
题目大意
\(n\)个点\(m\)条边的一张图,每条边有流量上下限制,求源点到汇点的最大流。
解题思路
先别急着求上面那个,考虑一下怎么求无源点汇点的上下界可行流。
可以考虑先把下限流满,这样就会出现有的点流量不均衡的问题,考虑每个点除了下限以外还有附加流量,这些附加流量会最多占能每条边\(r-l\)这么多的流量,可以先建立一张每条流量都是\(r-l\)的图。
定义一个点的\(d_i\)为该点的入度减去出度(流入的流量减去流出的流量),然后对于一个点如果它的\(d_i\)大于\(0\),那么它需要向其他点补充流量,建立一个超级源点\(S\)向它连边,流量为\(d_i\)。同理如果一个点的\(d_i\)小于\(0\)就连向超级汇点\(T\)。
这样就搞定了无源点汇点的上下界可行流问题了。
然后考虑有源汇点\(s,t\)怎么办,那么也就是\(t\)可以无限接受,\(s\)可以无限输送。那么如果\(t\)向\(s\)连一条\(inf\)的边,那么就可以保证\(s,t\)的功能又能保证流量守恒了。
之后直接和无源点汇点的一样做就好了。
然后要求最大流,先跑一次有没有可行的再考虑流量能够浮动的范围,此时我们需要在刚刚的残量网络上找从\(s\)到\(t\)的增广路来增大\(s\)到\(t\)的流量,那么删掉刚刚\(t->s\)的边然后跑\(s->t\)的最大流就好了。
最小流的话就是从\(t->s\)跑最大流
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=210,inf=1e9;
struct node{
int to,next,w;
}a[41000];
int n,m,tot,in[N],out[N],d[N];
int ls[N],cur[N],dep[N];
queue<int> q;
void addl(int x,int y,int w){
a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=w;
a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;a[tot].w=0;
return;
}
bool bfs(int s,int t){
while(!q.empty())q.pop();q.push(s);
memset(dep,0,sizeof(dep));dep[s]=1;
for(int i=1;i<=t;i++)cur[i]=ls[i];
while(!q.empty()){
int x=q.front();q.pop();
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(dep[y]||!a[i].w)continue;
q.push(y);dep[y]=dep[x]+1;
if(y==t)return 1;
}
}
return 0;
}
int dinic(int x,int flow,int t){
if(x==t)return flow;
int rest=0,k;
for(int &i=cur[x];i;i=a[i].next){
int y=a[i].to;
if(dep[x]+1!=dep[y]||!a[i].w)continue;
rest+=(k=dinic(y,min(flow-rest,a[i].w),t));
a[i].w-=k;a[i^1].w+=k;
if(rest==flow)return rest;
}
if(!rest)dep[x]=0;
return rest;
}
int main()
{
int ans=0,sum=0,s,t,S,T;
scanf("%d%d%d%d",&n,&m,&S,&T);
s=n+1;t=s+1;tot=1;
for(int i=1;i<=m;i++){
int x,y,l,u;
scanf("%d%d%d%d",&x,&y,&l,&u);
addl(x,y,u-l);d[y]+=l;d[x]-=l;
}
for(int i=1;i<=n;i++)
if(d[i]>0)addl(s,i,d[i]),sum+=d[i];
else addl(i,t,-d[i]);
addl(T,S,inf);
while(bfs(s,t))
ans+=dinic(s,inf,t);
if(ans!=sum)
return puts("please go home to sleep");
ans=a[tot].w;a[tot].w=a[tot^1].w=0;
while(bfs(S,T))
ans+=dinic(S,inf,T);
printf("%d\n",ans);
}
Loj#116-[模板]有源汇有上下界最大流的更多相关文章
- LOJ.116.[模板]有源汇有上下界最大流(Dinic)
题目链接 http://blog.csdn.net/just_sort/article/details/75448403 有源汇有上下界网络流 通过添加一条(T->S,[0,INF])的边变成无 ...
- LOJ.117.[模板]有源汇有上下界最小流(Dinic)
题目链接 有源汇有上下界最小流 Sol1. 首先和无源汇网络流一样建图,求SS->TT最大流: 然后连边(T->S,[0,INF]),再求一遍SS->TT最大流,答案为新添加边的流量 ...
- LOJ.115.[模板]无源汇有上下界可行流(Dinic)
题目链接 参考:http://blog.csdn.net/clove_unique/article/details/54884437 http://blog.csdn.net/wu_tongtong/ ...
- loj #116. 有源汇有上下界最大流
题目链接 有源汇有上下界最大流,->上下界网络流 注意细节,重置cur和dis数组时,有n+2个点 #include<cstdio> #include<algorithm> ...
- LibreOJ #116. 有源汇有上下界最大流
二次联通门 : LibreOJ #116. 有源汇有上下界最大流 /* LibreOJ #116. 有源汇有上下界最大流 板子题 我也就会写写板子题了.. 写个板子第一个点还死活过不去... 只能打个 ...
- 【模板】有源汇有上下界最大流(网络流)/ZOJ3229
先导知识 无源汇有上下界可行流 题目链接 https://vjudge.net/problem/ZOJ-3229 https://www.luogu.com.cn/problem/P5192 (有改动 ...
- loj #117. 有源汇有上下界最小流
题目链接 有源汇有上下界最小流,->上下界网络流 注意细节,边数组也要算上后加到SS,TT边. #include<cstdio> #include<algorithm> ...
- 【LOJ116】有源汇有上下界最大流(模板题)
点此看题面 大致题意: 给你每条边的流量上下界,让你先判断是否存在可行流.若存在,则输出最大流. 无源汇上下界可行流 在做此题之前,最好先去看看这道题目:[LOJ115]无源汇有上下界可行流. 大致思 ...
- 【Loj116】有源汇有上下界最大流(网络流)
[Loj116]有源汇有上下界最大流(网络流) 题面 Loj 题解 模板题. #include<iostream> #include<cstdio> #include<c ...
- LOJ116 - 有源汇有上下界最大流
原题链接 Description 模板题啦~ Code //有源汇有上下界最大流 #include <cstdio> #include <cstring> #include & ...
随机推荐
- Mysql5.6.47开放远程访问(修改远程访问密码)
1.登录mysql mysql -u root -p 然后输入密码,登录进去mysql: 2.切换数据库 use mysql; 3.修改权限 GRANT ALL PRIVILEGES ON *.* T ...
- @NotNull-JSR-303验证
背景 Spring Boot 引入Hibernate Validator 机制来支持 JSR-303 验证规范 实现 请求参数model类: package com.wzq.test.model; i ...
- vue-bluJavascript - Vue - 插件(swiper、vue-preview)
swiper swiper是一个支持滑动效果的js插件,它也支持在vue中使用,主要用于移动端的触摸滑动操作.Swiper中文网. 安装和导入插件 npm i vue-awesome-swiper - ...
- Ajax 与 Struts 1
Xml配置 <action path="/product/product/validateCurrencyDecimalSupport" type="com.neu ...
- jQuery中ajax请求的六种方法(三、三):$.post()方法
3.$.post()方法 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- Mybatis源码解析4——SqlSession
上一篇文章中,我们介绍了 SqlSessionFactory 的创建过程,忘记了的,可以回顾一下,或者看下下面这张图也行. 接下来,可乐讲给大家介绍 Mybatis 中另一个重量级嘉宾--SqlSes ...
- MyBatis学习总结(三)——MyBatis配置文件配置的优化
一.连接数据库的配置单独放在一个properties文件中 上文 连接数据库的配置写在 mybatisConf.xml中,本文直接放在 db.properties 中, 在mybatisConf.xm ...
- Redis的持久化机制与内存管理机制
1.概述 Redis的持久化机制有两种:RDB 和 AOF ,这两种机制有什么区别?正式环境应该采用哪种机制? 我们的服务器内存资源是有限的,如果内存被Redis的缓存占满了怎么办?这就要看Redis ...
- 20210719 noip20
考场 后两题是原题,教练说不用写了(ycx 不讲武德) T1 先手模了 \(n\le5\) 的情况,尝试找规律失败.那就只能 DP 了,最终没搞出来. 记忆化搜索打了 \(n\le20\) 的表,交了 ...
- Python常见问题 - python3 使用requests发送HTTPS请求报certificate verify failed 错误
当你使用 requests 发送HTTPS请求时 requests.get(url, parmas=parmas, headers=header, cookies=cookie) 出现了以下错误 HT ...