sgu438-The_Glorious_Karlutka_River
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的更多相关文章
- SGU438 The Glorious Karlutka River =)(最大流)
题目大概说有m个人要过一条宽W的河,人最远跳远距离是d,河上有n个垃圾堆,每个垃圾堆都有坐标和同一时间能容纳的人数,问所有人最少要跳几次才能跳到对岸. 又是一题根据时间拆点的最大流. 二分时间建容量网 ...
- SGU438 The Glorious Karlutka River =)
传送门 sgu原来搬到cf了呀点了好几个链接才找到233 传说中的动态流(?) 反正很暴力就对了QwQ 有容量限制->拆点 对于每个点拆成入点和出点 时间限制->分层 对于每个时刻的每个石 ...
- The Glorious Karlutka River =)
sgu438:http://acm.sgu.ru/problem.php?contest=0&problem=438 题意:有一条东西向流淌的河,宽为 W,河中有 N 块石头,每块石头的坐标( ...
随机推荐
- 免费开源ERP-成功案例分析(2)
Odoo用户案例 Odoo用户概要 关于Odoo全球的用户,我们来看一些数据: Odoo目前全球有300万使用者 Odoo系统上每天新创建的数据库超过1000个 Odoo和Word.Excel.Pow ...
- SuperMap GIS资料-----云与Web端技术资料集锦
转自:http://blog.csdn.net/supermapsupport/article/details/70254484 产品白皮书 iServer产品 教学视频 许可说明 安装部署 ...
- PHP如何实现在数据库随机获取几条记录
本文实例讲述了PHP实现在数据库百万条数据中随机获取20条记录的方法.PHP实例分享给大家供大家参考,具体如下: 为什么要写这个? 在去某个公司面试时,让写个算法出来,当时就蒙了,我开发过程中用到算法 ...
- 从零学习Fluter(七):Flutter打包apk详解
写一个win上 flutter 打包apk的教程 这篇文档介绍一下flutter打包发布正式版apk 整体来看,和命令行打包rn的方法相差不大 打包前先做检查工作&查看构建配置 Android ...
- SQL Server非域(跨域)环境下镜像(Mirror)的搭建步骤及注意事项
在实际的生产环境下,我们经常需要跨域进行数据备份,而创建Mirror是其中一个方案.但跨域创建Mirror要相对复杂的多,需要借助证书进行搭建. 下面我们将具体的步骤总结如下: 第一部分 创建证书 S ...
- Nagle 算法
1. Nagel算法 TCP/IP协议中,无论发送多少数据,总是要在数据前面加上协议头,同时,对方接收到数据,也需要发送ACK表示确认.为了尽可能的利用网络带宽,TCP总是希望尽可能的发 ...
- AES 加密与解密
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Securit ...
- sqlbulkcopy 批量插入数据
批量插入 Datetable数据 通过sqlbulkcopy 插入1百万条数据 用时 10秒钟 (有兴趣的小伙伴可以去测试) /// <summary> /// /// </sum ...
- 建立第一个SpringBoot小列子(碰到的错误)
当加入@SpringBootApplication注解时,无法得到解析 错误提示:SpringBootApplication cannot be resolved to a type 错误原因是因为s ...
- Python面试笔记四
数据库 1.将name字段添加索引 create index index_emp_name on student(name); 2.查询女生中数学成绩最高的分数 select max(score) f ...