4070: [Apio2015]雅加达的摩天楼
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 415 Solved: 146
[ Submit][ Status][ Discuss]
Description
印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1。除了这 N 座摩天楼外,雅加达市没有其他摩天楼。
有 M 只叫做 “doge” 的神秘生物在雅加达市居住,它们的编号依次是 0 到 M−1。编号为 i 的 doge 最初居住于编号为 Bi 的摩天楼。每只 doge 都有一种神秘的力量,使它们能够在摩天楼之间跳跃,编号为 i 的 doge 的跳跃能力为 Pi (Pi>0)。
在一次跳跃中,位于摩天楼 b 而跳跃能力为 p 的 doge 可以跳跃到编号为 b−p (如果 0≤b−p<N)或 b+p (如果 0≤b+p<N)的摩天楼。
编号为 0 的 doge 是所有 doge 的首领,它有一条紧急的消息要尽快传送给编 号为 1 的 doge。任何一个收到消息的 doge 有以下两个选择:
跳跃到其他摩天楼上;
将消息传递给它当前所在的摩天楼上的其他 doge。
请帮助 doge 们计算将消息从 0 号 doge 传递到 1 号 doge 所需要的最少总跳跃步数,或者告诉它们消息永远不可能传递到 1 号 doge。
Input
输入的第一行包含两个整数 N 和 M。
接下来 M 行,每行包含两个整数 Bi 和 Pi。
Output
输出一行,表示所需要的最少步数。如果消息永远无法传递到 1 号 doge,输出 −1。
Sample Input
5 3
0 2
1 1
4 1
0 2
1 1
4 1
Sample Output
5
explanation
下面是一种步数为 5 的解决方案:
0 号 doge 跳跃到 2 号摩天楼,再跳跃到 4 号摩天楼(2 步)。
0 号 doge 将消息传递给 2 号 doge。
2 号 doge 跳跃到 3 号摩天楼,接着跳跃到 2 号摩天楼,再跳跃到 1 号摩天楼(3 步)。
2 号 doge 将消息传递给 1 号 doge。
explanation
下面是一种步数为 5 的解决方案:
0 号 doge 跳跃到 2 号摩天楼,再跳跃到 4 号摩天楼(2 步)。
0 号 doge 将消息传递给 2 号 doge。
2 号 doge 跳跃到 3 号摩天楼,接着跳跃到 2 号摩天楼,再跳跃到 1 号摩天楼(3 步)。
2 号 doge 将消息传递给 1 号 doge。
HINT
子任务
所有数据都保证 0≤Bi<N。
子任务 1 (10 分)
1≤N≤10
1≤Pi≤10
2≤M≤3
子任务 2 (12 分)
1≤N≤100
1≤Pi≤100
2≤M≤2000
子任务 3 (14 分)
1≤N≤2000
1≤Pi≤2000
2≤M≤2000
子任务 4 (21 分)
1≤N≤2000
1≤Pi≤2000
2≤M≤30000
子任务 5 (43 分)
1≤N≤30000
1≤Pi≤30000
2≤M≤30000
Source
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
/*struct data{
int x,y;
data(){}
data(int x,int y): x(x),y(y){}
bool operator < (const data &B) const {return y > B.y;}
};*/
struct E{
int to,w;
E(){}
E(int to,int w): to(to),w(w){}
};
const int maxn = 3E4 + 30;
const int maxm = maxn*450;
const int INF = ~0U>>1;
//typedef __gnu_pbds::priority_queue<data,less<data>,__gnu_pbds::pairing_heap_tag> Heap;
int n,m,cnt,s,t,Sqrt,tot,dis[maxn*110],pos[110][maxn]
,tail[maxm],to[maxm],va[maxm],from[maxm];
bool inq[maxm];
//Heap Q;
//Heap::point_iterator id[maxm];
queue <int> Q2;
int getint()
{
char ch = getchar();
int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
void Add(int x,int y,int w)
{
from[++tot] = tail[x];
tail[x] = tot;
to[tot] = y;
va[tot] = w;
}
void Build()
{
for (int i = 1; i <= Sqrt; i++)
for (int j = 0; j < i; j++)
for (int k = j; k < n; k += i) {
pos[i][k] = ++cnt;
if (k - i >= 0) Add(cnt,cnt - 1,1);
if (k + i < n) Add(cnt,cnt + 1,1);
Add(cnt,k,0);
}
for (int i = 0; i < m; i++) {
int B = getint(),P = getint();
if (!i) s = B;
if (i == 1) t = B;
if (P > Sqrt) {
int Nex = B + P,w = 1;
while (Nex < n) {
Add(B,Nex,w);
Nex += P; ++w;
}
Nex = B - P; w = 1;
while (Nex >= 0) {
Add(B,Nex,w);
Nex -= P; ++w;
}
}
else Add(B,pos[P][B],0);
}
}
int main()
{
//freopen("4070.in","r",stdin);
//freopen("4070.out","w",stdout);
cin >> n >> m;
Sqrt = min((int)sqrt(n),100);
cnt = n - 1;
Build();
for (int i = 0; i <= cnt; i++) dis[i] = INF;
/*id[s] = Q.push(data(s,0));
inq[s] = 1; dis[s] = 0;
while (!Q.empty()) {
data k = Q.top(); Q.pop();
for (int i = 0; i < v[k.x].size(); i++) {
int to = v[k.x][i].to;
int w = v[k.x][i].w;
if (dis[to] > dis[k.x] + w) {
dis[to] = dis[k.x] + w;
if (!inq[to]) {
inq[to] = 1;
id[to] = Q.push(data(to,dis[to]));
}
else Q.modify(id[to],data(to,dis[to]));
}
}
}*/
Q2.push(s);
inq[s] = 1;
dis[s] = 0;
while (!Q2.empty()) {
int k = Q2.front();
inq[k] = 0; Q2.pop();
for (int i = tail[k]; i; i = from[i]) {
int nex = to[i];
int w = va[i];
if (dis[nex] > dis[k] + w) {
dis[nex] = dis[k] + w;
if (!inq[nex]) Q2.push(nex),inq[nex] = 1;
}
}
}
if (dis[t] < INF) cout << dis[t];
else cout << -1;
return 0;
}