Description

SGU似乎死了... 题目搬到了Codeforces...

Problem - 99999438 - Codeforces

Solution

动态最大流.

考虑如果不求时间, 只求能否到达对岸, 这显然是一个拆点最大流.

如果加上时间的限制, 考虑按照时间拆点, 然后枚举时间 \(t\), 并且连上 \(t\) 时间能到达的点.

具体的, 设河的这一岸为 \(s\), 对岸为 \(t\). \(t\) 时刻的点 \(p\) 为 \(p_t\), 并拆成两个点 \(p_t'\), \(p_t''\).

对于 \(t\) 时刻, 连边 \((p_t', p_t'', c_p)\). 对于能到达 \(s\) 或 \(t\) 的节点, 分别连边 \((s, p_t',\infty)\), \((p_{t-1}'', t, \infty)\). 对于能互相到达的点 \(u, v\), 连边 \((u_{t-1}, v_t, \infty)\), \((v_{t-1}, u_t, \infty)\).

那么当总最大流 \(\ge m\) 时, 当前枚举的时间即为答案.

Code

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
#define rep(i,l,r) for(register int i=(l);i<=(r);++i)
#define repdo(i,l,r) for(register int i=(l);i>=(r);--i)
#define il inline
typedef double db;
typedef long long ll; //---------------------------------------
const int nsz=60,n2sz=3050,psz=14050,esz=1e6+50,ninf=1e9+7;
int n,m,d,w,pos[nsz][3],link[nsz][2]; int pow2(int v){return v*v;}
struct te0{int f,t;}e0[n2sz];
int pe0=0; struct te{int t,pr,fl;}edge[esz*2];
int hd[psz],pe=1,ss,tt,np;
void adde(int f,int t,int fl){edge[++pe]=(te){t,hd[f],fl};hd[f]=pe;}
void addsg(int f,int t,int fl){adde(f,t,fl),adde(t,f,0);} int cur[psz],gap[psz],dep[psz];
void init(){
rep(i,1,np)cur[i]=hd[i],gap[i]=0,dep[i]=0;
static int que[psz],qh,qt;
qh=1,qt=0;
que[++qt]=tt,gap[1]=1,dep[tt]=1;
while(qh<=qt){
int u=que[qh++];
for(int i=hd[u],v;i;i=edge[i].pr){
v=edge[i].t;
if(dep[v])continue;
dep[v]=dep[u]+1,++gap[dep[v]],que[++qt]=v;
}
}
}
int dfs(int p,int mi){
if(p==tt||mi==0)return mi;
int fl=0,tmp;
for(int &i=cur[p],v;i;i=edge[i].pr){
v=edge[i].t;
if(dep[v]+1!=dep[p])continue;
tmp=dfs(v,min(mi,edge[i].fl));
fl+=tmp,mi-=tmp,edge[i].fl-=tmp,edge[i^1].fl+=tmp;
if(mi==0)return fl;
}
if(gap[dep[p]]==1)dep[ss]=np+1;
--gap[dep[p]],++dep[p],++gap[dep[p]];
cur[p]=hd[p];
return fl;
}
int maxfl(){
init();
int res=0;
while(dep[ss]<=np)res+=dfs(ss,ninf);
return res;
} int tr(int p,int tm,int fl){//fl==0: from; fl==1: to
return (n*(tm-1)+p)*2-1+fl;
} int sum=0;
int sol(){
if(d>=w)return 1;
else if(n==0)return -1;
ss=(n+m+5)*n*2+1,tt=(n+m+5)*n*2+2,np=tt;
rep(tm,1,n+m+1){
rep(i,1,n)if(link[i][0])addsg(ss,tr(i,tm,0),ninf);
if(tm>1)rep(i,1,n)if(link[i][1])addsg(tr(i,tm-1,1),tt,ninf);
rep(i,1,n)addsg(tr(i,tm,0),tr(i,tm,1),pos[i][2]);
if(tm>1){
rep(i,1,pe0){
addsg(tr(e0[i].f,tm-1,1),tr(e0[i].t,tm,0),ninf);
addsg(tr(e0[i].t,tm-1,1),tr(e0[i].f,tm,0),ninf);
}
}
sum+=maxfl();
// printf("tm=%d sum=%d\n",tm,sum);
if(sum>=m)return tm;
}
return -1;
} int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>m>>d>>w;
rep(i,1,n)cin>>pos[i][0]>>pos[i][1]>>pos[i][2];
rep(i,1,n){
if(pos[i][1]<=d)link[i][0]=1;
if(pos[i][1]>=w-d)link[i][1]=1;
rep(j,i+1,n){
if(pow2(pos[i][0]-pos[j][0])+pow2(pos[i][1]-pos[j][1])<=pow2(d))e0[++pe0]=(te0){i,j};
}
}
int ans=sol();
if(ans==-1)cout<<"IMPOSSIBLE\n";
else cout<<ans<<'\n';
return 0;
}

sgu438-The_Glorious_Karlutka_River的更多相关文章

  1. SGU438 The Glorious Karlutka River =)(最大流)

    题目大概说有m个人要过一条宽W的河,人最远跳远距离是d,河上有n个垃圾堆,每个垃圾堆都有坐标和同一时间能容纳的人数,问所有人最少要跳几次才能跳到对岸. 又是一题根据时间拆点的最大流. 二分时间建容量网 ...

  2. SGU438 The Glorious Karlutka River =)

    传送门 sgu原来搬到cf了呀点了好几个链接才找到233 传说中的动态流(?) 反正很暴力就对了QwQ 有容量限制->拆点 对于每个点拆成入点和出点 时间限制->分层 对于每个时刻的每个石 ...

  3. The Glorious Karlutka River =)

    sgu438:http://acm.sgu.ru/problem.php?contest=0&problem=438 题意:有一条东西向流淌的河,宽为 W,河中有 N 块石头,每块石头的坐标( ...

随机推荐

  1. 2019前端面试题汇总(主要为Vue)

    摘要: 经典面试题. 原文:2019前端面试题汇总(主要为Vue) 作者:前端小酱 Fundebug经授权转载,版权归原作者所有. 毕业之后就在一直合肥小公司工作,没有老司机.没有技术氛围,在技术的道 ...

  2. npm run dev 启动错误:Module build failed: Error: No PostCSS Config found in:xxxxxxxxxxxxxx

    解决办法:在根目录新建postcss.config.js module.exports = { plugins: { 'autoprefixer': {browsers: 'last 5 versio ...

  3. AndroisStudio列选择模式

    今天敲代码的时候可能由于误开了“列选择模式”,在移动光标时,发现若光标所在列超过当前行的行尾列,不像一般情况应该跳到行尾,而变成了保持列的位置,停在了超过行尾的空白处, 如图:非一般情况 一般情况: ...

  4. MySQL外键设置中的的 Cascade、NO ACTION、Restrict、SET NULL

    例如: ALTER TABLE stuinfo ADD CONSTRAINT fk_stuinfo FOREIGN KEY(gradeid) REFERENCES grade(id) ON DELET ...

  5. C#基础第七天

    1.ref参数ref参数侧重于将一个变量以参数的形式带到一个方法中进行改变,改变完成后,再讲改变后的值带出来.在使用ref参数的时候需要注意:ref参数在方法外必须为其赋值. 2.方法的重载方法的重载 ...

  6. SQLServer之创建隐式事务

    隐式事务创建注意事项 IMPLICIT_TRANSACTIONS为 ON 时,系统处于“隐式”事务模式. 这意味着如果 @@TRANCOUNT = 0,下列任一 Transact-SQL 语句都会开始 ...

  7. pymongo 一篇文章搞定

    一 安装 pip install pymongo 二 python连接mongodb数据库的前提 确保pymongo安装完毕 mongodb数据库的服务器端(mongod)必须处于启动状态 三 连接m ...

  8. Taro项目遇到的问题

    1. https://taro-ui.aotu.io/#/docs/questions 请在Taro项目根目录找到 config/index.js 文件中的h5项,添加如下: h5: { ... es ...

  9. web框架开发-分页器(Paginator)

    Django有自带的分页器,可以将数据分在不同的页面中,并提供一些属性和方法实现对分页数据的操作.分页功能的类位于django/core/paginator.py中. 常用方法 # 分页器 # pag ...

  10. MAC oh-my-zsh

    效果图 step1 : 安装zsh    brew install zsh step2: sudo vim  /etc/shells 添加 /usr/local/bin/zsh  step3:安装oh ...