#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 30'005, B = 175;
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
43864 KB |
Output is correct |
2 |
Correct |
9 ms |
43864 KB |
Output is correct |
3 |
Correct |
7 ms |
43828 KB |
Output is correct |
4 |
Correct |
7 ms |
43680 KB |
Output is correct |
5 |
Correct |
6 ms |
43864 KB |
Output is correct |
6 |
Correct |
6 ms |
43868 KB |
Output is correct |
7 |
Correct |
6 ms |
43868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
43864 KB |
Output is correct |
2 |
Correct |
6 ms |
43868 KB |
Output is correct |
3 |
Correct |
6 ms |
43868 KB |
Output is correct |
4 |
Correct |
6 ms |
43868 KB |
Output is correct |
5 |
Correct |
6 ms |
43868 KB |
Output is correct |
6 |
Correct |
6 ms |
43868 KB |
Output is correct |
7 |
Correct |
6 ms |
43864 KB |
Output is correct |
8 |
Correct |
6 ms |
43868 KB |
Output is correct |
9 |
Correct |
7 ms |
43868 KB |
Output is correct |
10 |
Correct |
7 ms |
44220 KB |
Output is correct |
11 |
Correct |
8 ms |
43868 KB |
Output is correct |
12 |
Correct |
6 ms |
43868 KB |
Output is correct |
13 |
Correct |
7 ms |
43868 KB |
Output is correct |
14 |
Correct |
9 ms |
43868 KB |
Output is correct |
15 |
Correct |
8 ms |
43952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
44120 KB |
Output is correct |
2 |
Correct |
6 ms |
43868 KB |
Output is correct |
3 |
Correct |
6 ms |
43868 KB |
Output is correct |
4 |
Correct |
6 ms |
43856 KB |
Output is correct |
5 |
Correct |
7 ms |
43868 KB |
Output is correct |
6 |
Correct |
6 ms |
43868 KB |
Output is correct |
7 |
Correct |
7 ms |
44120 KB |
Output is correct |
8 |
Correct |
6 ms |
43872 KB |
Output is correct |
9 |
Correct |
7 ms |
43868 KB |
Output is correct |
10 |
Correct |
7 ms |
43868 KB |
Output is correct |
11 |
Correct |
8 ms |
43868 KB |
Output is correct |
12 |
Correct |
8 ms |
43864 KB |
Output is correct |
13 |
Correct |
7 ms |
43868 KB |
Output is correct |
14 |
Correct |
8 ms |
43976 KB |
Output is correct |
15 |
Correct |
8 ms |
43868 KB |
Output is correct |
16 |
Correct |
6 ms |
43864 KB |
Output is correct |
17 |
Correct |
17 ms |
46172 KB |
Output is correct |
18 |
Correct |
8 ms |
45916 KB |
Output is correct |
19 |
Correct |
7 ms |
45916 KB |
Output is correct |
20 |
Correct |
17 ms |
46172 KB |
Output is correct |
21 |
Correct |
6 ms |
45916 KB |
Output is correct |
22 |
Correct |
6 ms |
45908 KB |
Output is correct |
23 |
Correct |
18 ms |
46000 KB |
Output is correct |
24 |
Correct |
23 ms |
45916 KB |
Output is correct |
25 |
Correct |
18 ms |
45916 KB |
Output is correct |
26 |
Correct |
17 ms |
45916 KB |
Output is correct |
27 |
Correct |
18 ms |
45916 KB |
Output is correct |
28 |
Correct |
21 ms |
46116 KB |
Output is correct |
29 |
Correct |
30 ms |
45916 KB |
Output is correct |
30 |
Correct |
23 ms |
45916 KB |
Output is correct |
31 |
Correct |
29 ms |
46172 KB |
Output is correct |
32 |
Correct |
26 ms |
45912 KB |
Output is correct |
33 |
Correct |
39 ms |
45912 KB |
Output is correct |
34 |
Correct |
40 ms |
46164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
43868 KB |
Output is correct |
2 |
Correct |
6 ms |
43864 KB |
Output is correct |
3 |
Correct |
7 ms |
43868 KB |
Output is correct |
4 |
Correct |
6 ms |
43868 KB |
Output is correct |
5 |
Correct |
7 ms |
43868 KB |
Output is correct |
6 |
Correct |
6 ms |
43868 KB |
Output is correct |
7 |
Correct |
6 ms |
43844 KB |
Output is correct |
8 |
Correct |
7 ms |
43864 KB |
Output is correct |
9 |
Correct |
7 ms |
43868 KB |
Output is correct |
10 |
Correct |
7 ms |
43868 KB |
Output is correct |
11 |
Correct |
8 ms |
43996 KB |
Output is correct |
12 |
Correct |
7 ms |
43864 KB |
Output is correct |
13 |
Correct |
7 ms |
44120 KB |
Output is correct |
14 |
Correct |
9 ms |
43868 KB |
Output is correct |
15 |
Correct |
9 ms |
43868 KB |
Output is correct |
16 |
Correct |
6 ms |
44024 KB |
Output is correct |
17 |
Correct |
15 ms |
46172 KB |
Output is correct |
18 |
Correct |
6 ms |
45916 KB |
Output is correct |
19 |
Correct |
6 ms |
45916 KB |
Output is correct |
20 |
Correct |
16 ms |
46132 KB |
Output is correct |
21 |
Correct |
7 ms |
45916 KB |
Output is correct |
22 |
Correct |
6 ms |
45916 KB |
Output is correct |
23 |
Correct |
18 ms |
45916 KB |
Output is correct |
24 |
Correct |
24 ms |
45916 KB |
Output is correct |
25 |
Correct |
18 ms |
45912 KB |
Output is correct |
26 |
Correct |
18 ms |
45916 KB |
Output is correct |
27 |
Correct |
17 ms |
45940 KB |
Output is correct |
28 |
Correct |
19 ms |
45916 KB |
Output is correct |
29 |
Correct |
30 ms |
45916 KB |
Output is correct |
30 |
Correct |
22 ms |
45916 KB |
Output is correct |
31 |
Correct |
27 ms |
45916 KB |
Output is correct |
32 |
Correct |
24 ms |
45916 KB |
Output is correct |
33 |
Correct |
37 ms |
45916 KB |
Output is correct |
34 |
Correct |
38 ms |
46112 KB |
Output is correct |
35 |
Correct |
34 ms |
46428 KB |
Output is correct |
36 |
Correct |
20 ms |
46172 KB |
Output is correct |
37 |
Correct |
45 ms |
46676 KB |
Output is correct |
38 |
Correct |
45 ms |
46424 KB |
Output is correct |
39 |
Correct |
45 ms |
46428 KB |
Output is correct |
40 |
Correct |
45 ms |
46428 KB |
Output is correct |
41 |
Correct |
47 ms |
46516 KB |
Output is correct |
42 |
Correct |
23 ms |
46172 KB |
Output is correct |
43 |
Correct |
19 ms |
46168 KB |
Output is correct |
44 |
Correct |
19 ms |
46168 KB |
Output is correct |
45 |
Correct |
76 ms |
47316 KB |
Output is correct |
46 |
Correct |
77 ms |
47316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
43868 KB |
Output is correct |
2 |
Correct |
6 ms |
43868 KB |
Output is correct |
3 |
Correct |
7 ms |
43868 KB |
Output is correct |
4 |
Correct |
6 ms |
43864 KB |
Output is correct |
5 |
Correct |
6 ms |
43864 KB |
Output is correct |
6 |
Correct |
7 ms |
43868 KB |
Output is correct |
7 |
Correct |
6 ms |
43868 KB |
Output is correct |
8 |
Correct |
7 ms |
43868 KB |
Output is correct |
9 |
Correct |
6 ms |
43880 KB |
Output is correct |
10 |
Correct |
7 ms |
43952 KB |
Output is correct |
11 |
Correct |
8 ms |
43868 KB |
Output is correct |
12 |
Correct |
7 ms |
43868 KB |
Output is correct |
13 |
Correct |
7 ms |
43868 KB |
Output is correct |
14 |
Correct |
9 ms |
43864 KB |
Output is correct |
15 |
Correct |
8 ms |
43868 KB |
Output is correct |
16 |
Correct |
6 ms |
43868 KB |
Output is correct |
17 |
Correct |
15 ms |
46168 KB |
Output is correct |
18 |
Correct |
8 ms |
45916 KB |
Output is correct |
19 |
Correct |
6 ms |
45968 KB |
Output is correct |
20 |
Correct |
17 ms |
46172 KB |
Output is correct |
21 |
Correct |
6 ms |
46032 KB |
Output is correct |
22 |
Correct |
6 ms |
45960 KB |
Output is correct |
23 |
Correct |
18 ms |
45916 KB |
Output is correct |
24 |
Correct |
24 ms |
46024 KB |
Output is correct |
25 |
Correct |
18 ms |
45916 KB |
Output is correct |
26 |
Correct |
18 ms |
46032 KB |
Output is correct |
27 |
Correct |
16 ms |
45916 KB |
Output is correct |
28 |
Correct |
18 ms |
46120 KB |
Output is correct |
29 |
Correct |
30 ms |
45912 KB |
Output is correct |
30 |
Correct |
22 ms |
45916 KB |
Output is correct |
31 |
Correct |
26 ms |
45916 KB |
Output is correct |
32 |
Correct |
25 ms |
45916 KB |
Output is correct |
33 |
Correct |
39 ms |
45912 KB |
Output is correct |
34 |
Correct |
38 ms |
45916 KB |
Output is correct |
35 |
Correct |
32 ms |
46424 KB |
Output is correct |
36 |
Correct |
18 ms |
46172 KB |
Output is correct |
37 |
Correct |
45 ms |
46676 KB |
Output is correct |
38 |
Correct |
44 ms |
46424 KB |
Output is correct |
39 |
Correct |
46 ms |
46428 KB |
Output is correct |
40 |
Correct |
44 ms |
46428 KB |
Output is correct |
41 |
Correct |
48 ms |
46456 KB |
Output is correct |
42 |
Correct |
20 ms |
46168 KB |
Output is correct |
43 |
Correct |
22 ms |
46428 KB |
Output is correct |
44 |
Correct |
19 ms |
46172 KB |
Output is correct |
45 |
Correct |
76 ms |
47308 KB |
Output is correct |
46 |
Correct |
75 ms |
47312 KB |
Output is correct |
47 |
Correct |
240 ms |
54992 KB |
Output is correct |
48 |
Correct |
13 ms |
60504 KB |
Output is correct |
49 |
Correct |
14 ms |
62556 KB |
Output is correct |
50 |
Correct |
11 ms |
64604 KB |
Output is correct |
51 |
Correct |
412 ms |
65596 KB |
Output is correct |
52 |
Correct |
426 ms |
66044 KB |
Output is correct |
53 |
Correct |
172 ms |
44480 KB |
Output is correct |
54 |
Correct |
79 ms |
43868 KB |
Output is correct |
55 |
Correct |
108 ms |
43864 KB |
Output is correct |
56 |
Correct |
162 ms |
65368 KB |
Output is correct |
57 |
Correct |
334 ms |
64344 KB |
Output is correct |
58 |
Correct |
157 ms |
44124 KB |
Output is correct |
59 |
Correct |
165 ms |
48268 KB |
Output is correct |
60 |
Correct |
181 ms |
56412 KB |
Output is correct |
61 |
Correct |
171 ms |
50268 KB |
Output is correct |
62 |
Correct |
192 ms |
44944 KB |
Output is correct |
63 |
Correct |
447 ms |
48340 KB |
Output is correct |
64 |
Correct |
443 ms |
48336 KB |
Output is correct |
65 |
Correct |
650 ms |
48344 KB |
Output is correct |
66 |
Execution timed out |
1016 ms |
52396 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |