#include <bits/stdc++.h>
using namespace std;
const int N = 3e4+5;
const int inf = 1e9;
struct node {
int dist, pos, jump;
bool operator < (const node &o) const {
return dist > o.dist;
}
};
int n, m, b[N], p[N], d[N][200];
vector <int> pos[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
int sqrtn = sqrt(n);
for(int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
pos[b[i]].emplace_back(p[i]);
}
int ans = inf;
priority_queue <node> pq;
memset(d, 0x3f, sizeof(d));
d[b[0]][0] = 0;
pq.push({0, b[0], 0});
while(pq.size()) {
node x = pq.top(); pq.pop();
if(x.pos == b[1]) ans = min(ans, x.dist);
if(x.dist > d[x.pos][x.jump]) continue;
if(x.jump == 0) {
for(int i:pos[x.pos]) {
if(i <= sqrtn) {
if(x.dist < d[x.pos][i]) {
d[x.pos][i] = x.dist;
pq.push({x.dist, x.pos, i});
}
}
else {
for(int j = 0; x.pos + j*i < n; ++j) {
if(x.pos + j*i >= n) break;
if(x.dist + j < d[x.pos + i*j][0]) {
d[x.pos + i*j][0] = x.dist + j;
pq.push({x.dist+j, x.pos+i*j, 0});
}
}
for(int j = 0; x.pos - i*j >= 0; ++j) {
if(x.pos - i*j < 0) break;
if(x.dist + j < d[x.pos - i*j][0]) {
d[x.pos-i*j][0] = x.dist + j;
pq.push({x.dist+j, x.pos-i*j, 0});
}
}
}
}
}
else {
if(x.jump + x.pos < n and d[x.jump+x.pos][x.jump] > x.dist+1) {
d[x.jump+x.pos][x.jump] = x.dist+1;
pq.push({x.dist+1, x.jump+x.pos, x.jump});
}
if(x.pos-x.jump >= 0 and d[x.pos-x.jump][x.jump] > x.dist+1) {
d[x.pos-x.jump][x.jump] = x.dist+1;
pq.push({x.dist+1, x.pos-x.jump, x.jump});
}
if(x.dist < d[x.pos][0]) {
d[x.pos][0] = x.dist;
pq.push({x.dist, x.pos, 0});
}
}
}
if(ans == inf) cout << -1;
else cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
24656 KB |
Output is correct |
2 |
Correct |
5 ms |
24656 KB |
Output is correct |
3 |
Correct |
5 ms |
24656 KB |
Output is correct |
4 |
Correct |
6 ms |
24656 KB |
Output is correct |
5 |
Correct |
5 ms |
24668 KB |
Output is correct |
6 |
Correct |
5 ms |
24656 KB |
Output is correct |
7 |
Correct |
5 ms |
24456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
24656 KB |
Output is correct |
2 |
Correct |
6 ms |
24656 KB |
Output is correct |
3 |
Correct |
5 ms |
24656 KB |
Output is correct |
4 |
Correct |
5 ms |
24656 KB |
Output is correct |
5 |
Correct |
4 ms |
24460 KB |
Output is correct |
6 |
Correct |
6 ms |
24672 KB |
Output is correct |
7 |
Correct |
7 ms |
24824 KB |
Output is correct |
8 |
Correct |
5 ms |
24656 KB |
Output is correct |
9 |
Correct |
5 ms |
24656 KB |
Output is correct |
10 |
Correct |
4 ms |
24656 KB |
Output is correct |
11 |
Correct |
5 ms |
24656 KB |
Output is correct |
12 |
Correct |
5 ms |
24656 KB |
Output is correct |
13 |
Correct |
4 ms |
24824 KB |
Output is correct |
14 |
Correct |
5 ms |
24656 KB |
Output is correct |
15 |
Correct |
5 ms |
24656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
24656 KB |
Output is correct |
2 |
Correct |
4 ms |
24656 KB |
Output is correct |
3 |
Correct |
4 ms |
24656 KB |
Output is correct |
4 |
Correct |
7 ms |
24656 KB |
Output is correct |
5 |
Correct |
7 ms |
24656 KB |
Output is correct |
6 |
Correct |
7 ms |
24456 KB |
Output is correct |
7 |
Correct |
7 ms |
24656 KB |
Output is correct |
8 |
Correct |
6 ms |
24656 KB |
Output is correct |
9 |
Correct |
6 ms |
24656 KB |
Output is correct |
10 |
Correct |
6 ms |
24656 KB |
Output is correct |
11 |
Correct |
5 ms |
24520 KB |
Output is correct |
12 |
Correct |
7 ms |
24824 KB |
Output is correct |
13 |
Correct |
4 ms |
24656 KB |
Output is correct |
14 |
Correct |
4 ms |
24656 KB |
Output is correct |
15 |
Correct |
9 ms |
24460 KB |
Output is correct |
16 |
Correct |
5 ms |
24656 KB |
Output is correct |
17 |
Correct |
4 ms |
24740 KB |
Output is correct |
18 |
Correct |
4 ms |
24532 KB |
Output is correct |
19 |
Correct |
5 ms |
24824 KB |
Output is correct |
20 |
Correct |
5 ms |
24656 KB |
Output is correct |
21 |
Correct |
5 ms |
24504 KB |
Output is correct |
22 |
Correct |
6 ms |
24536 KB |
Output is correct |
23 |
Correct |
9 ms |
24668 KB |
Output is correct |
24 |
Correct |
5 ms |
24656 KB |
Output is correct |
25 |
Correct |
6 ms |
24588 KB |
Output is correct |
26 |
Correct |
4 ms |
24656 KB |
Output is correct |
27 |
Correct |
4 ms |
24544 KB |
Output is correct |
28 |
Correct |
8 ms |
24656 KB |
Output is correct |
29 |
Correct |
8 ms |
24704 KB |
Output is correct |
30 |
Correct |
5 ms |
24656 KB |
Output is correct |
31 |
Correct |
6 ms |
24656 KB |
Output is correct |
32 |
Correct |
5 ms |
24656 KB |
Output is correct |
33 |
Correct |
9 ms |
24656 KB |
Output is correct |
34 |
Correct |
10 ms |
24656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
24656 KB |
Output is correct |
2 |
Correct |
5 ms |
24656 KB |
Output is correct |
3 |
Correct |
5 ms |
24656 KB |
Output is correct |
4 |
Correct |
5 ms |
24456 KB |
Output is correct |
5 |
Correct |
5 ms |
24400 KB |
Output is correct |
6 |
Correct |
5 ms |
24604 KB |
Output is correct |
7 |
Correct |
6 ms |
24656 KB |
Output is correct |
8 |
Correct |
7 ms |
24656 KB |
Output is correct |
9 |
Correct |
6 ms |
24656 KB |
Output is correct |
10 |
Correct |
5 ms |
24656 KB |
Output is correct |
11 |
Correct |
6 ms |
24540 KB |
Output is correct |
12 |
Correct |
5 ms |
24656 KB |
Output is correct |
13 |
Correct |
7 ms |
24656 KB |
Output is correct |
14 |
Correct |
8 ms |
24604 KB |
Output is correct |
15 |
Correct |
9 ms |
24668 KB |
Output is correct |
16 |
Correct |
11 ms |
24656 KB |
Output is correct |
17 |
Correct |
8 ms |
24656 KB |
Output is correct |
18 |
Correct |
5 ms |
24656 KB |
Output is correct |
19 |
Correct |
7 ms |
24656 KB |
Output is correct |
20 |
Correct |
5 ms |
24656 KB |
Output is correct |
21 |
Correct |
6 ms |
24656 KB |
Output is correct |
22 |
Correct |
5 ms |
24672 KB |
Output is correct |
23 |
Correct |
4 ms |
24656 KB |
Output is correct |
24 |
Correct |
6 ms |
24656 KB |
Output is correct |
25 |
Correct |
5 ms |
24656 KB |
Output is correct |
26 |
Correct |
5 ms |
24656 KB |
Output is correct |
27 |
Correct |
5 ms |
24656 KB |
Output is correct |
28 |
Correct |
8 ms |
24632 KB |
Output is correct |
29 |
Correct |
9 ms |
24656 KB |
Output is correct |
30 |
Correct |
8 ms |
24656 KB |
Output is correct |
31 |
Correct |
10 ms |
24828 KB |
Output is correct |
32 |
Correct |
8 ms |
24656 KB |
Output is correct |
33 |
Correct |
12 ms |
24784 KB |
Output is correct |
34 |
Correct |
12 ms |
24668 KB |
Output is correct |
35 |
Correct |
13 ms |
25336 KB |
Output is correct |
36 |
Correct |
10 ms |
24656 KB |
Output is correct |
37 |
Correct |
13 ms |
25168 KB |
Output is correct |
38 |
Correct |
14 ms |
25284 KB |
Output is correct |
39 |
Correct |
15 ms |
25424 KB |
Output is correct |
40 |
Correct |
16 ms |
25424 KB |
Output is correct |
41 |
Correct |
15 ms |
25480 KB |
Output is correct |
42 |
Correct |
10 ms |
25168 KB |
Output is correct |
43 |
Correct |
11 ms |
25168 KB |
Output is correct |
44 |
Correct |
15 ms |
25336 KB |
Output is correct |
45 |
Correct |
15 ms |
25424 KB |
Output is correct |
46 |
Correct |
13 ms |
25424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
24668 KB |
Output is correct |
2 |
Correct |
6 ms |
24656 KB |
Output is correct |
3 |
Correct |
5 ms |
24656 KB |
Output is correct |
4 |
Correct |
5 ms |
24656 KB |
Output is correct |
5 |
Correct |
5 ms |
24656 KB |
Output is correct |
6 |
Correct |
5 ms |
24656 KB |
Output is correct |
7 |
Correct |
5 ms |
24884 KB |
Output is correct |
8 |
Correct |
5 ms |
24668 KB |
Output is correct |
9 |
Correct |
6 ms |
24484 KB |
Output is correct |
10 |
Correct |
7 ms |
24656 KB |
Output is correct |
11 |
Correct |
8 ms |
24604 KB |
Output is correct |
12 |
Correct |
6 ms |
24656 KB |
Output is correct |
13 |
Correct |
5 ms |
24656 KB |
Output is correct |
14 |
Correct |
6 ms |
24656 KB |
Output is correct |
15 |
Correct |
5 ms |
24488 KB |
Output is correct |
16 |
Correct |
4 ms |
24656 KB |
Output is correct |
17 |
Correct |
10 ms |
24644 KB |
Output is correct |
18 |
Correct |
14 ms |
24512 KB |
Output is correct |
19 |
Correct |
7 ms |
24656 KB |
Output is correct |
20 |
Correct |
5 ms |
24656 KB |
Output is correct |
21 |
Correct |
4 ms |
24656 KB |
Output is correct |
22 |
Correct |
5 ms |
24656 KB |
Output is correct |
23 |
Correct |
5 ms |
24664 KB |
Output is correct |
24 |
Correct |
5 ms |
24656 KB |
Output is correct |
25 |
Correct |
9 ms |
24656 KB |
Output is correct |
26 |
Correct |
9 ms |
24672 KB |
Output is correct |
27 |
Correct |
7 ms |
24656 KB |
Output is correct |
28 |
Correct |
6 ms |
24736 KB |
Output is correct |
29 |
Correct |
8 ms |
24660 KB |
Output is correct |
30 |
Correct |
9 ms |
24656 KB |
Output is correct |
31 |
Correct |
11 ms |
24668 KB |
Output is correct |
32 |
Correct |
9 ms |
24656 KB |
Output is correct |
33 |
Correct |
11 ms |
24656 KB |
Output is correct |
34 |
Correct |
9 ms |
24656 KB |
Output is correct |
35 |
Correct |
10 ms |
25168 KB |
Output is correct |
36 |
Correct |
6 ms |
24656 KB |
Output is correct |
37 |
Correct |
12 ms |
25168 KB |
Output is correct |
38 |
Correct |
12 ms |
25424 KB |
Output is correct |
39 |
Correct |
12 ms |
25424 KB |
Output is correct |
40 |
Correct |
13 ms |
25476 KB |
Output is correct |
41 |
Correct |
11 ms |
25424 KB |
Output is correct |
42 |
Correct |
8 ms |
25168 KB |
Output is correct |
43 |
Correct |
8 ms |
25180 KB |
Output is correct |
44 |
Correct |
10 ms |
25168 KB |
Output is correct |
45 |
Correct |
12 ms |
25424 KB |
Output is correct |
46 |
Correct |
14 ms |
25424 KB |
Output is correct |
47 |
Correct |
29 ms |
25940 KB |
Output is correct |
48 |
Correct |
9 ms |
25436 KB |
Output is correct |
49 |
Correct |
10 ms |
25272 KB |
Output is correct |
50 |
Correct |
11 ms |
25340 KB |
Output is correct |
51 |
Correct |
25 ms |
26128 KB |
Output is correct |
52 |
Correct |
25 ms |
26160 KB |
Output is correct |
53 |
Correct |
15 ms |
25680 KB |
Output is correct |
54 |
Correct |
6 ms |
24672 KB |
Output is correct |
55 |
Correct |
7 ms |
24640 KB |
Output is correct |
56 |
Correct |
13 ms |
25936 KB |
Output is correct |
57 |
Correct |
30 ms |
24792 KB |
Output is correct |
58 |
Correct |
9 ms |
25156 KB |
Output is correct |
59 |
Correct |
11 ms |
25168 KB |
Output is correct |
60 |
Correct |
10 ms |
25192 KB |
Output is correct |
61 |
Correct |
10 ms |
25168 KB |
Output is correct |
62 |
Correct |
17 ms |
25936 KB |
Output is correct |
63 |
Correct |
22 ms |
26828 KB |
Output is correct |
64 |
Correct |
24 ms |
26828 KB |
Output is correct |
65 |
Correct |
92 ms |
26828 KB |
Output is correct |
66 |
Correct |
325 ms |
26824 KB |
Output is correct |
67 |
Correct |
348 ms |
26828 KB |
Output is correct |