4070: [Apio2015]雅加达的摩天楼
Description
印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1。除了这 N 座摩天楼外,雅加达市没有其他摩天楼。
Input
输入的第一行包含两个整数 N 和 M。
Output
输出一行,表示所需要的最少步数。如果消息永远无法传递到 1 号 doge,输出 −1。
Sample Input
0 2
1 1
4 1
Sample Output
explanation
下面是一种步数为 5 的解决方案:
0 号 doge 跳跃到 2 号摩天楼,再跳跃到 4 号摩天楼(2 步)。
0 号 doge 将消息传递给 2 号 doge。
2 号 doge 跳跃到 3 号摩天楼,接着跳跃到 2 号摩天楼,再跳跃到 1 号摩天楼(3 步)。
2 号 doge 将消息传递给 1 号 doge。
HINT
子任务
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
const int maxn=*;
const int maxm=maxn*;
const int inf=;
int n,m,a,b,lim;
struct Graph{
int tot,now[maxn],son[maxm],pre[maxm],val[maxm];
int head,tail,que[maxn],dis[maxn];
bool bo[maxn];
void put(int a,int b,int c){pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=c;}
int spfa(){
memset(dis,,sizeof(dis));
head=,tail=,que[]=,bo[]=,dis[]=;
while (head!=tail){
if (++head==maxn) head=;
int u=que[head];
for (int p=now[u],v=son[p];p;p=pre[p],v=son[p])
if (dis[v]>dis[u]+val[p]){
dis[v]=dis[u]+val[p];
if (!bo[v]){
if (++tail==maxn) tail=;
que[tail]=v,bo[v]=;
}
}
bo[u]=;
}
if (dis[]==inf) dis[]=-;
return dis[];
}
}G;
int f(int x,int y){return m+x*n+y;}
int main(){
read(n),read(m),lim=sqrt(m/);
for (int t=;t<=lim;t++) for (int i=;i<n;i++) G.put(f(t,i),f(,i),);
for (int t=;t<=lim;t++) for (int i=t;i<n;i++) G.put(f(t,i-t),f(t,i),),G.put(f(t,i),f(t,i-t),);
for (int i=;i<m;i++){
read(a),read(b);
G.put(i,f(,a),),G.put(f(,a),i,);
if (b<=lim) G.put(i,f(b,a),);
else{
for (int j=;a+b*j<n;j++) G.put(i,f(,a+b*j),j);
for (int j=;a-b*j>=;j++) G.put(i,f(,a-b*j),j);
}
}
printf("%d\n",G.spfa());
return ;
}
4070: [Apio2015]雅加达的摩天楼的更多相关文章
- BZOJ 4070:[APIO2015]雅加达的摩天楼 最短路
4070: [Apio2015]雅加达的摩天楼 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 464 Solved: 164[Submit][Sta ...
- bzoj 4070 [Apio2015]雅加达的摩天楼 Dijkstra+建图
[Apio2015]雅加达的摩天楼 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 644 Solved: 238[Submit][Status][D ...
- BZOJ 4070 [Apio2015]雅加达的摩天楼 ——分块 SPFA
挺有趣的分块的题目. 直接暴力建边SPFA貌似是$O(nm)$的. 然后考虑分块,$\sqrt n$一下用虚拟节点辅助连边, 以上的直接暴力连边即可. 然后卡卡时间,卡卡空间. 终于在UOJ上T掉辣. ...
- bzoj 4070: [Apio2015]雅加达的摩天楼【spfa】
明明是个最短路却有网络流一样的神建图= A = 首先要是暴力建图的话最坏有O(nm)条边.所以优化建图. 考虑分块思想,设bs=sqrt(n),对于p大于bs的,直接连边即可,最多有sqrt(n)条, ...
- BZOJ 4070: [Apio2015]雅加达的摩天楼 根号分治+spfa
此题卡Dijkstra... Code: #include <bits/stdc++.h> #define N 30005 #define M 4000000 #define ll lon ...
- 【BZOJ4070】[Apio2015]雅加达的摩天楼 set+最短路
[BZOJ4070][Apio2015]雅加达的摩天楼 Description 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1.除了这 N 座摩天楼 ...
- 【题解】P3645 [APIO2015]雅加达的摩天楼(分层图最短路)
[题解]P3645 [APIO2015]雅加达的摩天楼(分层图最短路) 感觉分层图是个很灵活的东西 直接连边的话,边数是\(O(n^2)\)的过不去 然而我们有一个优化的办法,可以建一个新图\(G=( ...
- luogu P3645 [APIO2015]雅加达的摩天楼 分块 根号分治
LINK:雅加达的摩天楼 容易想到设\(f_{i,j}\)表示第i个\(doge\)在第j层楼的最小步数. 转移显然是bfs.值得一提的是把初始某层的\(doge\)加入队列 然后转移边权全为1不需要 ...
- [APIO2015]雅加达的摩天楼
Description 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1.除了这 N 座摩天楼外,雅加达市没有其他摩天楼. 有 M 只叫做 " ...
随机推荐
- [CSS] Make element not selectable
.noselect { -webkit-touch-callout: none; /* iOS Safari */ -webkit-user-select: none; /* Chrome/Safar ...
- OPTIMIZE TABLE
INNODB 不支持 mysql> OPTIMIZE TABLE t; +--------+----------+----------+----------------------------- ...
- 系统数据文件和信息之附加组ID
4.2BSD引入了附加组ID(supplementary group ID)的概念.我们不仅可以属于口令文件记录项中组ID所对应的组,也可属于多达16个另外的组.文件访问权限检查相应被修改为:不仅将进 ...
- Enable SPI 1.0 and 1.1 with device tre overlays on BeagleBone
For most people the above image means absolutely nothing, but for that one guy that has been searchi ...
- 将table内容输出为csv文件
1.创建 directory create or replace directory tt as'c:\'; SELECT * FROM DBA_OBJECTS DO WHERE DO.OBJECT_ ...
- [Form Builder]POST 与 commit_form 的区别
commit_form:将form上的数据写入数据库,并且会在database提交,即 直接查询表是能够查到结果,在form左下角会得到“FRM-40400:Transaction complete: ...
- oracle Form Builer:ID_NULL Built-in
Description Returns a BOOLEAN va ...
- ArcGIS Server JavaScript API 各命名空间的含义【转】
1.esri 命名空间 所有的对象都是在 esri 命名空间下的,esri 有自己的属性和方法. 如 esri.version 返回当前 JavaScript API 的版本号.e ...
- android之错误汇总
A.错误:生成android源码索引期间使用mmm命令报错 B.解决: 或者 . build/envsetup.sh 依据自己的环境脚本而定 未完待续.....
- C#里面比较时间大小三种方法
1.比较时间大小的实验 string st1="12:13";string st2="14:14";DateTime dt1=Convert.ToDateTim ...