#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 30'005, B = 1;
vector<int> gud[N];
int dist[N][B + 5][2], ok[N][B + 5];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
int st = -1, en = -1;
for (int i = 0; i < m; i++) {
int b, p;
cin >> b >> p;
if (i == 0) st = b;
if (i == 1) en = b;
if (p < B) {
ok[b][p] = 1;
}
gud[b].push_back(p);
}
memset(dist, '?', sizeof dist);
priority_queue<array<int, 4>, vector<array<int, 4>>, greater<>> pq;
pq.push({0, st, B, 0});
dist[st][B][0] = 0;
while (pq.size()) {
int cost = pq.top()[0];
int node = pq.top()[1];
int pp = pq.top()[2];
int ok2 = pq.top()[3];
pq.pop();
if (cost > dist[node][pp][ok2]) continue;
if (pp < B) {
int p = pp;
if (p - 1 >= 0 && cost < dist[node][p - 1][0]) {
dist[node][p - 1][0] = cost;
pq.push({cost, node, p - 1, 0});
}
if (cost < dist[node][p + 1][0]) {
dist[node][p + 1][0] = cost;
pq.push({cost, node, p + 1, 0});
}
if (!ok[node][p] && !ok2) continue;
if (node - p >= 0 && cost + 1 < dist[node - p][p][1]) {
dist[node - p][p][1] = cost + 1;
pq.push({cost + 1, node - p, p, 1});
}
if (node + p < n && cost + 1 < dist[node + p][p][1]) {
dist[node + p][p][1] = cost + 1;
pq.push({cost + 1, node + p, p, 1});
}
} else {
if (cost < dist[node][pp - 1][0]) {
dist[node][pp - 1][0] = cost;
pq.push({cost, node, pp - 1, 0});
}
for (auto p : gud[node]) {
if (p < B) continue;
int cnt = 0;
for (int nxt = node + p; nxt < n; nxt += p) {
cnt++;
if (cost + cnt < dist[nxt][B][0]) {
dist[nxt][B][0] = cost + cnt;
pq.push({cost + cnt, nxt, B, 0});
}
}
cnt = 0;
for (int nxt = node - p; nxt >= 0; nxt -= p) {
cnt++;
if (cost + cnt < dist[nxt][B][0]) {
dist[nxt][B][0] = cost + cnt;
pq.push({cost + cnt, nxt, B, 0});
}
}
}
}
}
cout << (dist[en][B][0] >= (ll) 1e9? -1 : dist[en][B][0]) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
3160 KB |
Output is correct |
5 |
Correct |
3 ms |
2904 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
3100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2904 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
3112 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
3108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2904 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2904 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
3120 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2904 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
3160 KB |
Output is correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
4 ms |
3164 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
1 ms |
3164 KB |
Output is correct |
24 |
Correct |
2 ms |
3164 KB |
Output is correct |
25 |
Correct |
2 ms |
3164 KB |
Output is correct |
26 |
Correct |
4 ms |
3160 KB |
Output is correct |
27 |
Correct |
3 ms |
3164 KB |
Output is correct |
28 |
Correct |
2 ms |
3164 KB |
Output is correct |
29 |
Correct |
3 ms |
3360 KB |
Output is correct |
30 |
Correct |
2 ms |
3164 KB |
Output is correct |
31 |
Correct |
2 ms |
3164 KB |
Output is correct |
32 |
Correct |
2 ms |
3164 KB |
Output is correct |
33 |
Correct |
3 ms |
3420 KB |
Output is correct |
34 |
Correct |
3 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2904 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
3164 KB |
Output is correct |
18 |
Correct |
1 ms |
2920 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
4 ms |
3164 KB |
Output is correct |
21 |
Correct |
1 ms |
2904 KB |
Output is correct |
22 |
Correct |
1 ms |
3104 KB |
Output is correct |
23 |
Correct |
1 ms |
3164 KB |
Output is correct |
24 |
Correct |
2 ms |
3164 KB |
Output is correct |
25 |
Correct |
2 ms |
3264 KB |
Output is correct |
26 |
Correct |
4 ms |
2908 KB |
Output is correct |
27 |
Correct |
4 ms |
2904 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
3 ms |
3416 KB |
Output is correct |
30 |
Correct |
2 ms |
3164 KB |
Output is correct |
31 |
Correct |
2 ms |
3164 KB |
Output is correct |
32 |
Correct |
2 ms |
3164 KB |
Output is correct |
33 |
Correct |
3 ms |
3420 KB |
Output is correct |
34 |
Correct |
3 ms |
3420 KB |
Output is correct |
35 |
Correct |
5 ms |
3620 KB |
Output is correct |
36 |
Correct |
2 ms |
3160 KB |
Output is correct |
37 |
Correct |
5 ms |
3676 KB |
Output is correct |
38 |
Correct |
6 ms |
3732 KB |
Output is correct |
39 |
Correct |
6 ms |
3672 KB |
Output is correct |
40 |
Correct |
7 ms |
3676 KB |
Output is correct |
41 |
Correct |
6 ms |
3700 KB |
Output is correct |
42 |
Correct |
49 ms |
3420 KB |
Output is correct |
43 |
Correct |
40 ms |
3540 KB |
Output is correct |
44 |
Correct |
41 ms |
3532 KB |
Output is correct |
45 |
Correct |
6 ms |
3796 KB |
Output is correct |
46 |
Correct |
6 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2904 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
1 ms |
2904 KB |
Output is correct |
11 |
Correct |
1 ms |
2904 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2904 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
3164 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
5 ms |
3164 KB |
Output is correct |
21 |
Correct |
1 ms |
3108 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
3164 KB |
Output is correct |
24 |
Correct |
2 ms |
3164 KB |
Output is correct |
25 |
Correct |
2 ms |
3164 KB |
Output is correct |
26 |
Correct |
4 ms |
3112 KB |
Output is correct |
27 |
Correct |
3 ms |
2908 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
3 ms |
3420 KB |
Output is correct |
30 |
Correct |
2 ms |
3244 KB |
Output is correct |
31 |
Correct |
2 ms |
3164 KB |
Output is correct |
32 |
Correct |
2 ms |
3164 KB |
Output is correct |
33 |
Correct |
3 ms |
3420 KB |
Output is correct |
34 |
Correct |
3 ms |
3420 KB |
Output is correct |
35 |
Correct |
6 ms |
3548 KB |
Output is correct |
36 |
Correct |
2 ms |
3188 KB |
Output is correct |
37 |
Correct |
5 ms |
3676 KB |
Output is correct |
38 |
Correct |
7 ms |
3676 KB |
Output is correct |
39 |
Correct |
6 ms |
3672 KB |
Output is correct |
40 |
Correct |
7 ms |
3672 KB |
Output is correct |
41 |
Correct |
7 ms |
3676 KB |
Output is correct |
42 |
Correct |
41 ms |
3420 KB |
Output is correct |
43 |
Correct |
41 ms |
3416 KB |
Output is correct |
44 |
Correct |
49 ms |
3420 KB |
Output is correct |
45 |
Correct |
6 ms |
3796 KB |
Output is correct |
46 |
Correct |
8 ms |
3800 KB |
Output is correct |
47 |
Correct |
17 ms |
4820 KB |
Output is correct |
48 |
Correct |
5 ms |
3676 KB |
Output is correct |
49 |
Correct |
5 ms |
3572 KB |
Output is correct |
50 |
Correct |
5 ms |
3420 KB |
Output is correct |
51 |
Correct |
27 ms |
6096 KB |
Output is correct |
52 |
Correct |
25 ms |
5072 KB |
Output is correct |
53 |
Correct |
18 ms |
5072 KB |
Output is correct |
54 |
Correct |
4 ms |
3416 KB |
Output is correct |
55 |
Correct |
4 ms |
3416 KB |
Output is correct |
56 |
Correct |
696 ms |
4828 KB |
Output is correct |
57 |
Correct |
50 ms |
9164 KB |
Output is correct |
58 |
Correct |
589 ms |
3672 KB |
Output is correct |
59 |
Correct |
679 ms |
3796 KB |
Output is correct |
60 |
Correct |
676 ms |
3800 KB |
Output is correct |
61 |
Correct |
611 ms |
3800 KB |
Output is correct |
62 |
Correct |
8 ms |
4188 KB |
Output is correct |
63 |
Correct |
32 ms |
5592 KB |
Output is correct |
64 |
Correct |
34 ms |
7628 KB |
Output is correct |
65 |
Correct |
39 ms |
7892 KB |
Output is correct |
66 |
Correct |
51 ms |
8136 KB |
Output is correct |
67 |
Correct |
55 ms |
8652 KB |
Output is correct |