Bzoj 2502: 清理雪道 有上下界网络流_最小流
好长时间没有写网络流了,感觉好手生.
对于本题,设一个源点 $s$ 和 $t$.
1.由 $s$ 向每个点连一条没有下界,容量为无限大的边,表示以该点为起点.
2.由每个点向 $t$ 连一条没有下界,容量为无限大的边,表示以该点为终点.
为了保证每条原图中得边都能被覆盖掉,再将原图中的边连一条无上界,下界为 1 的边.
最后,跑一遍最小流即可.
Code:
// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 2000
#define inf 1000000
int tot;
void ini(){ tot=1000; }
int newnode(){ return ++tot; }
struct Edge{
int from,to,cap;
Edge(int a=0,int b=0,int c=0):from(a),to(b),cap(c){}
};
vector<int>G[maxn];
vector<Edge>edges;
void addedge(int u,int v,int c){
edges.push_back(Edge(u,v,c));
edges.push_back(Edge(v,u,0));
int m=edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
namespace maxflow{
int s,t;
int d[maxn],vis[maxn];
int current[maxn];
queue<int>Q;
int BFS(){
memset(vis,0,sizeof(vis));
vis[s]=1,d[s]=0;
Q.push(s);
while(!Q.empty()){
int u=Q.front();Q.pop();
int sz=G[u].size();
for(int i=0;i<sz;++i){
Edge v=edges[G[u][i]];
if(!vis[v.to]&&v.cap>0) {
d[v.to]=d[u]+1,vis[v.to]=1;
Q.push(v.to);
}
}
}
return vis[t];
}
int dfs(int x,int cur){
if(x!=t){
int f,flow=0,sz=G[x].size();
for(int i=current[x];i<sz;++i){
current[x]=i;
Edge r = edges[G[x][i]];
if(d[r.to]==d[x]+1&&r.cap>0) {
f=dfs(r.to,min(cur,r.cap));
if(f>0){
flow+=f,cur-=f;
edges[G[x][i]].cap-=f,edges[G[x][i]^1].cap+=f;
}
}
if(!cur) break;
}
return flow;
}
else return cur;
}
int GET(){
int ans=0;
while(BFS()) {
memset(current,0,sizeof(current));
ans+=dfs(s,inf);
}
return ans;
}
};
int main(){
//setIO("input");
ini();
int n,s,t,ss,tt;
s=0,t=newnode(),ss=newnode(),tt=newnode();
scanf("%d",&n);
for(int i=1,m=0,v;i<=n;++i) {
scanf("%d",&m);
while(m--){
scanf("%d",&v);
addedge(i,v,inf-1),addedge(s,i,inf),addedge(v,t,inf);
addedge(ss,v,1),addedge(i,tt,1);
}
}
int rec=edges.size();
addedge(t,s,inf);
maxflow::s=ss,maxflow::t=tt;
maxflow::GET();
int ans1=edges[rec^1].cap;
edges[rec^1].cap=edges[rec].cap=0;
maxflow::s=t, maxflow::t=s;
ans1-=maxflow::GET();
printf("%d",ans1);
return 0;
}
Bzoj 2502: 清理雪道 有上下界网络流_最小流的更多相关文章
- [BZOJ2502]清理雪道 有上下界网络流(最小流)
2502: 清理雪道 Time Limit: 10 Sec Memory Limit: 128 MB Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...
- BZOJ 2502: 清理雪道 | 有上下界最小流
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...
- P4843 清理雪道(上下界网络流)
P4843 清理雪道 上下界最小流 我们先搞一遍上下界可行流(转) 回忆上下界最大流的写法:在可行流的残量网络$s\ -\ t$上跑最大流,答案为可行流$+$残量网络的最大流 那么上下界最小流的写法呢 ...
- BZOJ 2502: 清理雪道
BZOJ 2502: 清理雪道 标签(空格分隔): OI-BZOJ OI-最小流 OI-上下界网络流 Time Limit: 10 Sec Memory Limit: 128 MB Descripti ...
- hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )
题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...
- HDU3157 Crazy Circuits(有源汇流量有上下界网络的最小流)
题目大概给一个电路,电路上有n+2个结点,其中有两个分别是电源和负载,结点们由m个单向的部件相连,每个部件都有最少需要的电流,求使整个电路运转需要的最少电流. 容量网络的构建很容易,建好后就是一个有源 ...
- bzoj 2502 清理雪道 (有源汇上下界最小流)
2502: 清理雪道 Time Limit: 10 Sec Memory Limit: 128 MB Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...
- 【BZOJ-2502】清理雪道 有上下界的网络流(有下界的最小流)
2502: 清理雪道 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 594 Solved: 318[Submit][Status][Discuss] ...
- 【bzoj2502】清理雪道 有上下界最小流
题目描述 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞 ...
随机推荐
- spring重点一:处理对象创建时间 个数以及方式
/** * 1) 对象创建: 单例/多例(个数) * scope="singleton", 默认值, 即 默认是单例 [service/dao/工具类] * scope=&quo ...
- 序列模型(2)-----循环神经网络RNN
一.RNN的作用和粗略介绍: RNN可解决的问题: 训练样本输入是连续的序列,且序列的长短不一,比如基于时间的序列:一段段连续的语音,一段段连续的手写文字.这些序列比较长,且长度不一,比较难直接的拆分 ...
- UVA455 - Periodic Strings(紫书习题3.4)
如果一个字符串可以被某个长度为k的字符串重复多次得到,则称这个字符串的周期为k.例如,字符串"abcabcabcabc"以3为周期(当然,他也以6.12等等为周期). 现在请你编 ...
- linux 中配置假域名来测试
1.linux中配置假域名 找到hosts文件进行编辑 命令:vim /etc/hosts 配置: #centos(本机IP)192.168.1.179 www.imooc.com(假域名,自己设置) ...
- J2EE异常问题总结
http://blog.csdn.net/rchm8519/article/details/41624381 http://www.51testing.com/html/49/240349-14393 ...
- echarts在vue里面使用,以及基础配置。
基础的图表和基础的配置. 效果图如下: 1.安装图表依赖包:npm install echarts 2.在main.js里面 引入echarts import echarts from 'echart ...
- CodeForcesGym 100641E Inspectors
Inspectors Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. O ...
- ES scroll(ES游标) 解决深分页
ES scroll(ES游标) 解决深分页. Why 当Elasticsearch响应请求时,它必须确定docs的顺序,排列响应结果.如果请求的页数较少(假设每页20个docs), Elasticse ...
- POJ 3737
第一道三分题,有模板 #define eps 10e-6 double cal(){}//计算题目所需要的值 while(l+eps<r) { m1=l+(r-l)/3; m2=r-(r-l)/ ...
- [HTML5] aria-label & aria-labelledby
'aria-labelledby' overwrite 'aria-label' overwirte native element label. TOP-LEFT: aria-label overwr ...