#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 3e4 + 5, B = 175;
vector<int> g[MX];
int N, M;
int dist[MX][B];
bool vis[MX];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
for(int i = 0; i < MX; i++) {
for(int j = 0; j < B; j++) {
dist[i][j] = 2e9;
}
}
cin >> N >> M;
int s = 0, f = 0;
for(int i = 0; i < M; i++) {
int x, k;
cin >> x >> k;
g[x].push_back(k);
if(!i) s = x;
if(i == 1) f = x;
}
priority_queue<array<int,3>> pq;
for(auto x : g[s]) {
if(x < B) {
dist[s][x] = 0;
pq.push({dist[s][x], s, x});
} else {
for(int k = 1; s + k * x < N; k++) {
dist[s + k * x][0] = k;
pq.push({-dist[s + k * x][0], s + k * x, 0});
}
for(int k = 1; s - k * x >= 0; k++) {
dist[s - k * x][0] = k;
pq.push({-dist[s - k * x][0], s - k * x, 0});
}
}
}
vis[s] = 1;
while(!pq.empty()) {
auto [d, v, p] = pq.top(); pq.pop();
d = -d;
if(d > dist[v][p]) continue;
if(!vis[v]) {
for(auto x : g[v]) {
if(x < B) {
dist[v][x] = d;
pq.push({-dist[v][x], v, x});
} else {
for(int k = 1; v + k * x < N; k++) {
if(dist[v + k * x][0] > d + k) {
dist[v + k * x][0] = d + k;
pq.push({-dist[v + k * x][0], v + k * x, 0});
}
}
for(int k = 1; v - k * x >= 0; k++) {
if(dist[v - k * x][0] > d + k) {
dist[v - k * x][0] = d + k;
pq.push({-dist[v - k * x][0], v - k * x, 0});
}
}
}
}
vis[v] = 1;
}
if(v + p < N && dist[v + p][p] > d + 1) {
dist[v + p][p] = d + 1;
pq.push({-dist[v + p][p], v + p, p});
}
if(v - p >= 0 && dist[v - p][p] > d + 1) {
dist[v - p][p] = d + 1;
pq.push({-dist[v - p][p], v - p, p});
}
}
int ans = 2e9;
for(int k = 0; k < B; k++) ans = min(ans, dist[f][k]);
if(ans == 2e9) ans = -1;
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
10 ms |
21460 KB |
Output is correct |
4 |
Correct |
11 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
12 ms |
21460 KB |
Output is correct |
7 |
Correct |
10 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21520 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
10 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21576 KB |
Output is correct |
5 |
Correct |
10 ms |
21532 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
10 ms |
21536 KB |
Output is correct |
8 |
Correct |
11 ms |
21580 KB |
Output is correct |
9 |
Correct |
10 ms |
21460 KB |
Output is correct |
10 |
Correct |
12 ms |
21580 KB |
Output is correct |
11 |
Correct |
11 ms |
21588 KB |
Output is correct |
12 |
Correct |
13 ms |
21588 KB |
Output is correct |
13 |
Correct |
12 ms |
21652 KB |
Output is correct |
14 |
Correct |
11 ms |
21592 KB |
Output is correct |
15 |
Correct |
11 ms |
21596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21512 KB |
Output is correct |
2 |
Correct |
10 ms |
21520 KB |
Output is correct |
3 |
Correct |
10 ms |
21572 KB |
Output is correct |
4 |
Correct |
10 ms |
21576 KB |
Output is correct |
5 |
Correct |
10 ms |
21452 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
10 ms |
21460 KB |
Output is correct |
8 |
Correct |
10 ms |
21464 KB |
Output is correct |
9 |
Correct |
10 ms |
21588 KB |
Output is correct |
10 |
Correct |
11 ms |
21552 KB |
Output is correct |
11 |
Correct |
11 ms |
21580 KB |
Output is correct |
12 |
Correct |
13 ms |
21532 KB |
Output is correct |
13 |
Correct |
11 ms |
21636 KB |
Output is correct |
14 |
Correct |
12 ms |
21588 KB |
Output is correct |
15 |
Correct |
11 ms |
21588 KB |
Output is correct |
16 |
Correct |
10 ms |
21588 KB |
Output is correct |
17 |
Correct |
15 ms |
21588 KB |
Output is correct |
18 |
Correct |
10 ms |
21588 KB |
Output is correct |
19 |
Correct |
10 ms |
21588 KB |
Output is correct |
20 |
Correct |
10 ms |
21560 KB |
Output is correct |
21 |
Correct |
11 ms |
21592 KB |
Output is correct |
22 |
Correct |
10 ms |
21544 KB |
Output is correct |
23 |
Correct |
12 ms |
21592 KB |
Output is correct |
24 |
Correct |
12 ms |
21588 KB |
Output is correct |
25 |
Correct |
11 ms |
21596 KB |
Output is correct |
26 |
Correct |
14 ms |
21616 KB |
Output is correct |
27 |
Correct |
13 ms |
21588 KB |
Output is correct |
28 |
Correct |
12 ms |
21592 KB |
Output is correct |
29 |
Correct |
16 ms |
21588 KB |
Output is correct |
30 |
Correct |
12 ms |
21588 KB |
Output is correct |
31 |
Correct |
14 ms |
21584 KB |
Output is correct |
32 |
Correct |
13 ms |
21704 KB |
Output is correct |
33 |
Correct |
22 ms |
21588 KB |
Output is correct |
34 |
Correct |
22 ms |
21588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21564 KB |
Output is correct |
5 |
Correct |
11 ms |
21468 KB |
Output is correct |
6 |
Correct |
13 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
10 ms |
21568 KB |
Output is correct |
10 |
Correct |
10 ms |
21588 KB |
Output is correct |
11 |
Correct |
11 ms |
21552 KB |
Output is correct |
12 |
Correct |
10 ms |
21592 KB |
Output is correct |
13 |
Correct |
11 ms |
21588 KB |
Output is correct |
14 |
Correct |
11 ms |
21552 KB |
Output is correct |
15 |
Correct |
13 ms |
21592 KB |
Output is correct |
16 |
Correct |
10 ms |
21476 KB |
Output is correct |
17 |
Correct |
12 ms |
21596 KB |
Output is correct |
18 |
Correct |
13 ms |
21524 KB |
Output is correct |
19 |
Correct |
11 ms |
21588 KB |
Output is correct |
20 |
Correct |
11 ms |
21608 KB |
Output is correct |
21 |
Correct |
11 ms |
21500 KB |
Output is correct |
22 |
Correct |
12 ms |
21588 KB |
Output is correct |
23 |
Correct |
11 ms |
21616 KB |
Output is correct |
24 |
Correct |
12 ms |
21588 KB |
Output is correct |
25 |
Correct |
11 ms |
21580 KB |
Output is correct |
26 |
Correct |
11 ms |
21588 KB |
Output is correct |
27 |
Correct |
11 ms |
21652 KB |
Output is correct |
28 |
Correct |
12 ms |
21596 KB |
Output is correct |
29 |
Correct |
16 ms |
21624 KB |
Output is correct |
30 |
Correct |
15 ms |
21568 KB |
Output is correct |
31 |
Correct |
14 ms |
21560 KB |
Output is correct |
32 |
Correct |
14 ms |
21580 KB |
Output is correct |
33 |
Correct |
22 ms |
21588 KB |
Output is correct |
34 |
Correct |
22 ms |
21588 KB |
Output is correct |
35 |
Correct |
21 ms |
22088 KB |
Output is correct |
36 |
Correct |
13 ms |
21588 KB |
Output is correct |
37 |
Correct |
27 ms |
22112 KB |
Output is correct |
38 |
Correct |
25 ms |
22152 KB |
Output is correct |
39 |
Correct |
26 ms |
22228 KB |
Output is correct |
40 |
Correct |
26 ms |
22236 KB |
Output is correct |
41 |
Correct |
26 ms |
22228 KB |
Output is correct |
42 |
Correct |
16 ms |
22488 KB |
Output is correct |
43 |
Correct |
16 ms |
22544 KB |
Output is correct |
44 |
Correct |
21 ms |
22456 KB |
Output is correct |
45 |
Correct |
55 ms |
22420 KB |
Output is correct |
46 |
Correct |
55 ms |
22496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21504 KB |
Output is correct |
4 |
Correct |
10 ms |
21460 KB |
Output is correct |
5 |
Correct |
10 ms |
21476 KB |
Output is correct |
6 |
Correct |
10 ms |
21544 KB |
Output is correct |
7 |
Correct |
10 ms |
21548 KB |
Output is correct |
8 |
Correct |
12 ms |
21584 KB |
Output is correct |
9 |
Correct |
10 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21516 KB |
Output is correct |
11 |
Correct |
14 ms |
21588 KB |
Output is correct |
12 |
Correct |
10 ms |
21592 KB |
Output is correct |
13 |
Correct |
11 ms |
21548 KB |
Output is correct |
14 |
Correct |
11 ms |
21588 KB |
Output is correct |
15 |
Correct |
11 ms |
21592 KB |
Output is correct |
16 |
Correct |
10 ms |
21528 KB |
Output is correct |
17 |
Correct |
12 ms |
21588 KB |
Output is correct |
18 |
Correct |
10 ms |
21596 KB |
Output is correct |
19 |
Correct |
10 ms |
21492 KB |
Output is correct |
20 |
Correct |
11 ms |
21572 KB |
Output is correct |
21 |
Correct |
11 ms |
21536 KB |
Output is correct |
22 |
Correct |
11 ms |
21508 KB |
Output is correct |
23 |
Correct |
12 ms |
21588 KB |
Output is correct |
24 |
Correct |
12 ms |
21540 KB |
Output is correct |
25 |
Correct |
11 ms |
21588 KB |
Output is correct |
26 |
Correct |
12 ms |
21596 KB |
Output is correct |
27 |
Correct |
13 ms |
21588 KB |
Output is correct |
28 |
Correct |
12 ms |
21588 KB |
Output is correct |
29 |
Correct |
16 ms |
21588 KB |
Output is correct |
30 |
Correct |
12 ms |
21584 KB |
Output is correct |
31 |
Correct |
15 ms |
21508 KB |
Output is correct |
32 |
Correct |
13 ms |
21496 KB |
Output is correct |
33 |
Correct |
22 ms |
21588 KB |
Output is correct |
34 |
Correct |
22 ms |
21588 KB |
Output is correct |
35 |
Correct |
21 ms |
22100 KB |
Output is correct |
36 |
Correct |
13 ms |
21588 KB |
Output is correct |
37 |
Correct |
30 ms |
22228 KB |
Output is correct |
38 |
Correct |
30 ms |
22216 KB |
Output is correct |
39 |
Correct |
26 ms |
22220 KB |
Output is correct |
40 |
Correct |
25 ms |
22140 KB |
Output is correct |
41 |
Correct |
30 ms |
22232 KB |
Output is correct |
42 |
Correct |
16 ms |
22604 KB |
Output is correct |
43 |
Correct |
16 ms |
22460 KB |
Output is correct |
44 |
Correct |
15 ms |
22536 KB |
Output is correct |
45 |
Correct |
56 ms |
22492 KB |
Output is correct |
46 |
Correct |
56 ms |
22428 KB |
Output is correct |
47 |
Correct |
69 ms |
22676 KB |
Output is correct |
48 |
Correct |
14 ms |
22228 KB |
Output is correct |
49 |
Correct |
14 ms |
22228 KB |
Output is correct |
50 |
Correct |
13 ms |
22012 KB |
Output is correct |
51 |
Correct |
46 ms |
22952 KB |
Output is correct |
52 |
Correct |
47 ms |
22968 KB |
Output is correct |
53 |
Correct |
18 ms |
22484 KB |
Output is correct |
54 |
Correct |
10 ms |
21584 KB |
Output is correct |
55 |
Correct |
11 ms |
21576 KB |
Output is correct |
56 |
Correct |
17 ms |
22736 KB |
Output is correct |
57 |
Correct |
62 ms |
21596 KB |
Output is correct |
58 |
Correct |
22 ms |
22476 KB |
Output is correct |
59 |
Correct |
22 ms |
22552 KB |
Output is correct |
60 |
Correct |
25 ms |
22512 KB |
Output is correct |
61 |
Correct |
21 ms |
22612 KB |
Output is correct |
62 |
Correct |
29 ms |
22872 KB |
Output is correct |
63 |
Correct |
41 ms |
22872 KB |
Output is correct |
64 |
Correct |
44 ms |
22868 KB |
Output is correct |
65 |
Correct |
215 ms |
23632 KB |
Output is correct |
66 |
Correct |
653 ms |
23620 KB |
Output is correct |
67 |
Correct |
654 ms |
23656 KB |
Output is correct |