【题解】CTSC1999家园(网络流)
CTSC1999家园
建模方法类似我NOI2019网络同步赛我的T1写法[【题解】NOI2019Route](70分)
问题的焦点是:空间时间载具。
考虑如何击破时间限制,可以对每个点关于每个时刻建立一个点,这样就实现一个点在两个时间互不干扰。由于时间是流淌的,所以从过去到现在连一条免费的\(inf\)边
此时空间问题也就解决了,比较空间就是具体的节点。
考虑载具,载具就相当于一个时刻在新建边的东西,我们直接枚举时间让他慢慢加点就好了。注意到载具总要从上个时间连接到这个时间,相当于虫洞?
下面这个图会非常清楚:
写的话有点麻烦,模块化编程就稍微好写点。数据范围不超过100,不用怕MLE,TLE。
发现一种很好用的调试技巧,就是在add函数中输出fr to w 等等内容,按照这个提示建图就可以很快发现问题。
//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; typedef long long ll;
inline int qr(){
register int ret=0,f=0;
register char c=getchar();
while(c<48||c>57)f|=c==45,c=getchar();
while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}
const int maxn=5e4+5;
struct E{
int to,nx,w;
E(){to=nx=w=0;}
E(const int&a,const int&b,const int&c){to=a;nx=b;w=c;}
}e[maxn];
int head[maxn];
int last[maxn];
int lastlast[maxn];
int cnt=1;
int n,m,k;
int nodecnt;
int S,T;
const int inf=0x3f3f3f3f;
inline void add(const int&fr,const int&to,const int&w,const int&f=1){
e[++cnt]=E(to,head[fr],w);
head[fr]=cnt;
if(f) add(to,fr,0,0);
}
queue < int > q;
int d[maxn],cur[maxn];
inline bool bfs(){
for(register int t=1;t<=nodecnt;++t) d[t]=0,cur[t]=head[t];
d[S]=1;q.push(S);
while(q.size()){
register int now=q.front();
q.pop();
//cout<<"now="<<now<<' '<<T<<endl;
for(register int t=head[now];t;t=e[t].nx){
if(e[t].w>0&&d[e[t].to]==0){
d[e[t].to]=d[now]+1;
q.push(e[t].to);
}
}
}
return d[T];
}
int dfs(const int&now,int fl){
if(now==T||fl==0)return fl;
register int ret=0;
for(register int&t=cur[now];t;t=e[t].nx){
if(e[t].w>0&&d[e[t].to]==d[now]+1){
int d=dfs(e[t].to,min(e[t].w,fl));
e[t].w-=d;e[t^1].w+=d;ret+=d;fl-=d;
}
}
return ret;
}
inline int dinic(){
register int ret=0;
while(bfs()) ret+=dfs(S,inf);
return ret;
}
int sh[205][205];
int siz[205];
int stay[205];
int cap[205];
inline void init(){
S=++nodecnt;
T=++nodecnt;
for(register int t=1;t<=n+2;++t) last[t]=++nodecnt;
add(S,last[n+1],k);
}
inline void update(const int&now){
for(register int t=1;t<=n+2;++t)
add(lastlast[t]=last[t],++nodecnt,inf),last[t]=nodecnt;
for(register int t=1;t<=m;++t){
int la=stay[t];
int to=(stay[t]+1)%siz[t];
add(lastlast[sh[t][la]],last[sh[t][to]],cap[t]);
stay[t]=to;
}
add(last[n+2],T,inf);
}
namespace binc{
int r[201];
int n;
inline void init(int a){n=a;for(int t=1;t<=n;++t) r[t]=t;}
int q(int a){return a==r[a]?a:r[a]=q(r[a]);}
void j(int a,int b){r[q(a)]=q(b);}
}
int main(){
n=qr();m=qr();k=qr();
binc::init(n+2);
for(register int t=1;t<=m;++t){
cap[t]=qr();
siz[t]=qr();
for(register int i=0;i<siz[t];++i){
sh[t][i]=qr();
if(sh[t][i]==0)sh[t][i]=n+1;
if(sh[t][i]==-1)sh[t][i]=n+2;
if(i)binc::j(sh[t][i],sh[t][i-1]);
}
}
if(binc::q(n+1)!=binc::q(n+2)) return puts("0"),0;
int timenow=0,sum=0,t1,cnt=0;
init();
while(sum<k){
update(++timenow);
t1=dinic();
sum+=t1;
if(t1==0&&sum)++cnt;
}
printf("%d\n",timenow);
return 0;
}
【题解】CTSC1999家园(网络流)的更多相关文章
- 洛谷2754 [CTSC1999]家园
题目链接:[CTSC1999]家园 这个题目我们不是很好在做网络流的时候判断是否有解,因此我们考虑分开来做 对于是否有解的判断,我们唯一需要解决的是飞船的周期停泊问题,对于这个问题,我们可以用并查集解 ...
- 【题解】【网络流24题】航空路线问题 [P2770] [Loj6122]
[题解][网络流24题]航空路线问题 [P2770] [Loj6122] 传送门:航空路线问题 \([P2770]\) \([Loj6122]\) [题目描述] 给出一张有向图,每个点(除了起点 \( ...
- 【题解】【网络流24题】汽车加油行驶问题 [P4009] [Loj6223]
[题解][网络流24题]汽车加油行驶问题 [P4009] [Loj6223] 传送门:汽车加油行驶问题 \([P4009]\) \([Loj6223]\) [题目描述] 给出一个 \(N \times ...
- [网络流24题][CTSC1999] 家园
题目链接:戳我 对于这种一个点(表面意义上的一个点,比如说一个位置)对应多种情况的(比如说随着时间的推移有着不同的状态,而且这种状态>2),我们考虑在类似于分层图上面跑网络流. 比如说这道题,周 ...
- 网络流24题 P2754 [CTSC1999]家园
思路 如图,建立分层图跑dinic 每次在残余网络里加边继续跑 跑到ans>=k时候的i就是答案 诶呀啊,忘记弄箭头了,最后一列是向上的箭头,不过聪明的你们应该没啥影响 代码 #include ...
- [CTSC1999]家园 分层图网络流_并查集
Code: #include<cstdio> #include<vector> #include<algorithm> #include<queue> ...
- 洛谷P2754 [CTSC1999]家园(最大流)
传送门 这题思路太强了……大佬们怎么想到的……我这菜鸡根本想不出来…… 先判断是否能到达,对每一艘飞船能到的地方用并查集合并一下,最后判断一下是否连通 然后考虑几天怎么判断,我们可以枚举. 每一个点表 ...
- P2754 [CTSC1999]家园
传送门 人在各个太空站流动,所以显然的网络流模型 因为不同时间能走的边不同,所以显然按时间拆点 但是因为不知道要多少时间,所以要枚举时间,动态拆点 每一点向下一个时间的同一点连流量为 $INF$ 的边 ...
- [CTSC1999]【网络流24题】星际转移
Description 由于人类对自然资源的消耗,人们意识到大约在2300 年之后,地球就不能再居住了.于是在月球上建立了新的绿地,以便在需要时移民.令人意想不到的是,2177 年冬由于未知的原因,地 ...
随机推荐
- 计算php程序运行时间
<?php //程序运行时间 $starttime = explode(' ',microtime()); echo microtime(); /*········以下是代码区······· ...
- 怎么清除火狐浏览器的cookie?
火狐浏览器清除Cookie方法/步骤 1.打开火狐浏览器.并在火狐浏览器工具栏找到并单击“工具”下的“选项”. 2.在打开的“火狐浏览器选项”程序窗口中,找到工具栏中的“隐私”并单击,在隐私选项下找到 ...
- Ubuntu 14.04 使用ntfs-config解决开机自动挂载NTFS分区的方法
先安装: sudo apt-get install ntfs-3g ntfs-config 再配置一下: sudo ntfs-config 然后就会弹出来一个对话框,选择你需要挂载的分区,点应用,再选 ...
- tp3.2.3 解决http://lx.com/后必须加index.php才能访问的问题,配置文件中忘了加index index.php index.html 等默认文件
server { listen 80; server_name lx.com; root "D:\phpstudy\PHPTutorial\WWW\liuxue"; locatio ...
- vue+file-saver+xlsx导出table表格为excel
https://blog.csdn.net/wjswangjinsheng/article/details/91393396
- HDU 2191多重背包问题、
#include<cstdio> #include<cmath> #include<iostream> #include<cstring> +; int ...
- NGINX比Apache的性能高是因为NGINX由C语言开发,而Apache由C++开发
事实上,NGINX比Apache的性能高是因为NGINX由C语言开发,而Apache由C++开发.因此,NGINX效率大概是Apache的10倍左右
- Python--day42--mysql操作数据库及数据表和基本增删改查
sql语法规则: 一.操作文件夹 1.创建数据库db2:create database db2; 2.创建数据库db2并标明数据库的编码格式为utf8:create database db2 defa ...
- Vue 中的过滤器的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- H3C 静态黑洞路由应用