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

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

//@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){
}
}
}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){