【题解】P3645 [APIO2015]雅加达的摩天楼(分层图最短路)

感觉分层图是个很灵活的东西

直接连边的话,边数是\(O(n^2)\)的过不去

然而我们有一个优化的办法,可以建一个新图\(G=(V,E)\)其中\(V\)和原图\(V\)一一对应且连接一个\(0\)边,此外每个点向V中的\(i+-d\)连边。

类似网络流的办法瞎建就行了。

过不了uoj

//@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 N=90;
struct DOGE{unsigned int b:16,p:16;}dg[2];
struct E{
unsigned int to:28,w:15;
E(){to=w=0;}
E(const int&t,const int&f){to=t; w=f;}
}; vector< vector<E> > e;
int n,m,cnt; inline void add(const int&fr,const int&to,const int&w){
if(fr>=e.size()) e.resize(fr+1);
e[fr].push_back(E(to,w));
} struct SUBGraph{
int begin;
SUBGraph(){begin=0;}
inline int operator[](int x){return begin+x;}
inline void gen(const int&d){
begin=cnt+1; cnt+=n;
for(int t=0;t<n;++t){
if(t+d<n) add(begin+t+d,begin+t,1);
if(t-d>=0) add(begin+t-d,begin+t,1);
add(begin+t,t,0);
}
}
}G[N]; vector<int> d;
/*
struct cmp{inline bool operator ()(const int&a,const int&b)const{return d[a]>d[b];}};
priority_queue<int,vector<int>,cmp> q;
*/
queue<int> q;
vector<bool> usd;
const int inf=1e9;
inline void bfs(){
d.resize(cnt+1);
usd.resize(cnt+1);
for(auto&t:d) t=inf;
d[dg[0].b]=0,q.push(dg[0].b);
while(q.size()){
int now=q.front();
usd[now]=0;
q.pop();
if(now==dg[1].b) continue;
for(const auto&t:e[now]){
if(d[t.to]>d[now]+t.w){
d[t.to]=d[now]+t.w;
if(!usd[t.to]) q.push(t.to),usd[t.to]=1;
}
}
}
} int main(){
n=qr(); m=qr(); cnt=n-1;
for(int t=0,p,b;t<m;++t){
b=qr();p=qr();
if(t<2)dg[t].b=b,dg[t].p=p;
if(p>=N)
for(int i=p,k=1;i<n;i+=p,++k){
if(b-i>=0) add(b,b-i,k);
if(b+i< n) add(b,b+i,k);
}
else {
if(!G[p].begin) G[p].gen(p);
add(b,G[p][b],0);
}
}
bfs();
printf("%d\n",d[dg[1].b]==inf?-1:d[dg[1].b]);
return 0;
}

【题解】P3645 [APIO2015]雅加达的摩天楼(分层图最短路)的更多相关文章

  1. 洛谷P3645 [APIO2015]雅加达的摩天楼

    题目描述 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N − 1.除了这 NN 座摩天楼外,雅加达市没有其他摩天楼. 有 M 只叫做 “doge” 的神 ...

  2. luogu P3645 [APIO2015]雅加达的摩天楼 分块 根号分治

    LINK:雅加达的摩天楼 容易想到设\(f_{i,j}\)表示第i个\(doge\)在第j层楼的最小步数. 转移显然是bfs.值得一提的是把初始某层的\(doge\)加入队列 然后转移边权全为1不需要 ...

  3. 洛咕 P3645 [APIO2015]雅加达的摩天楼

    暴力连边可以每个bi向i+kdi连边权是k的边. 考虑这样的优化: 然后发现显然是不行的,因为可能还没有走到一个dog的建筑物就走了这个dog的边. 然后就有一个很妙的方法--建一个新的图,和原图分开 ...

  4. 洛谷P3645 [APIO2015]雅加达的摩天楼(最短路+分块)

    传送门 这最短路的建图怎么和网络流一样玄学…… 一个最朴素的想法是从每一个点向它能到达的所有点连边,边权为跳的次数,然后跑最短路(然而边数是$O(n^2)$除非自创复杂度比spfa和dijkstra还 ...

  5. 洛谷$P3645\ [APIO2015]$雅加达的摩天楼 最短路

    正解:最短路 解题报告: 传送门$QwQ$ 考虑暴力连边,发现最多有$n^2$条边.于是考虑分块 对于长度$p_i$小于等于$\sqrt(n)$的边,建立子图$d=p_i$.说下关于子图$d$的定义? ...

  6. luogu P3645 [APIO2015]雅加达的摩天楼

    luogu 暴力? 暴力! 这个题有点像最短路,所以设\(f_{i,j}\)表示在\(i\)号楼,当前\(doge\)跳跃能力为\(j\)的最短步数,转移要么跳一步到\(f_{i+j,j}\)和\(f ...

  7. BZOJ4070 [Apio2015]雅加达的摩天楼 【分块 + 最短路】

    题目链接 BZOJ4070 题解 考虑暴力建图,将每个\(B_i\)向其能到的点连边,复杂度\(O(\sum \frac{n}{p_i})\),当\(p\)比较小时不适用 考虑优化建图,每个\(dog ...

  8. bzoj 4070 [Apio2015]雅加达的摩天楼 Dijkstra+建图

    [Apio2015]雅加达的摩天楼 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 644  Solved: 238[Submit][Status][D ...

  9. 【BZOJ4070】[Apio2015]雅加达的摩天楼 set+最短路

    [BZOJ4070][Apio2015]雅加达的摩天楼 Description 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1.除了这 N 座摩天楼 ...

随机推荐

  1. 下载mysql document

    wget -b -r -np -L -p https://dev.mysql.com/doc/refman/5.6/en/ 在下载时.有用到外部域名的图片或连接.如果需要同时下载就要用-H参数. wg ...

  2. 洛谷P1049 装箱问题

    //01背包 价值等于体积 求所剩最小体积 #include<bits/stdc++.h> using namespace std; ; ; int c,n,v[maxn],f[maxv] ...

  3. oracle函数 BFILENAME(dir,file)

    [功能]函数返回一个空的BFILE位置值指示符,函数用于初始化BFILE变量或者是BFILE列. [参数]dir是一个directory类型的对象,file为一文件名. insert into lob ...

  4. vue element 中自定义传值

    一直以来都不知道如何传自定义的值,一直只会默认的,今天终于找到方法了. 比如这个上传图片的控件,想带当前的index过去,就这样写.其它的类似 :http-request="(file,fi ...

  5. Best Open Source Software

    Best Open Source Software Open Source, Software, Top The promise of open source software is best qua ...

  6. jq获取单选框、复选框、下拉菜单的值

    1.<input type="radio" name="testradio" value="jquery获取radio的值" /> ...

  7. TAE words

    love handle   pang   carbohydrate   podiatry   splay out   Cinderella   liposuction   mingle   fly t ...

  8. H3C 802.11协议的发展

  9. 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 ...

  10. web.xml和@WebServlet

    web.xml <servlet> <servlet-name>DZDYServlet</servlet-name> <servlet-class>包名 ...