#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define ll long long
const int N = 30'005, B = 30;
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 |
11100 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Correct |
2 ms |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11100 KB |
Output is correct |
7 |
Correct |
2 ms |
11100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11100 KB |
Output is correct |
2 |
Correct |
2 ms |
11252 KB |
Output is correct |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Correct |
2 ms |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11100 KB |
Output is correct |
7 |
Correct |
2 ms |
11100 KB |
Output is correct |
8 |
Correct |
2 ms |
11096 KB |
Output is correct |
9 |
Correct |
2 ms |
11100 KB |
Output is correct |
10 |
Correct |
2 ms |
11100 KB |
Output is correct |
11 |
Correct |
3 ms |
11096 KB |
Output is correct |
12 |
Correct |
2 ms |
11100 KB |
Output is correct |
13 |
Correct |
2 ms |
11100 KB |
Output is correct |
14 |
Correct |
2 ms |
11100 KB |
Output is correct |
15 |
Correct |
2 ms |
11096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
2 ms |
11096 KB |
Output is correct |
4 |
Correct |
2 ms |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11100 KB |
Output is correct |
7 |
Correct |
2 ms |
11100 KB |
Output is correct |
8 |
Correct |
2 ms |
11100 KB |
Output is correct |
9 |
Correct |
3 ms |
11100 KB |
Output is correct |
10 |
Correct |
2 ms |
11100 KB |
Output is correct |
11 |
Correct |
3 ms |
11352 KB |
Output is correct |
12 |
Correct |
2 ms |
11096 KB |
Output is correct |
13 |
Correct |
2 ms |
11100 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
3 ms |
11100 KB |
Output is correct |
16 |
Correct |
2 ms |
11096 KB |
Output is correct |
17 |
Correct |
4 ms |
11356 KB |
Output is correct |
18 |
Correct |
2 ms |
13148 KB |
Output is correct |
19 |
Correct |
2 ms |
13148 KB |
Output is correct |
20 |
Correct |
5 ms |
13560 KB |
Output is correct |
21 |
Correct |
2 ms |
11100 KB |
Output is correct |
22 |
Correct |
3 ms |
13144 KB |
Output is correct |
23 |
Correct |
7 ms |
13400 KB |
Output is correct |
24 |
Correct |
7 ms |
13400 KB |
Output is correct |
25 |
Correct |
5 ms |
13144 KB |
Output is correct |
26 |
Correct |
5 ms |
13148 KB |
Output is correct |
27 |
Correct |
4 ms |
13148 KB |
Output is correct |
28 |
Correct |
6 ms |
13148 KB |
Output is correct |
29 |
Correct |
11 ms |
13552 KB |
Output is correct |
30 |
Correct |
7 ms |
13144 KB |
Output is correct |
31 |
Correct |
7 ms |
13320 KB |
Output is correct |
32 |
Correct |
6 ms |
13404 KB |
Output is correct |
33 |
Correct |
12 ms |
13404 KB |
Output is correct |
34 |
Correct |
12 ms |
13400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11096 KB |
Output is correct |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Correct |
2 ms |
11096 KB |
Output is correct |
5 |
Correct |
2 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11100 KB |
Output is correct |
7 |
Correct |
2 ms |
11100 KB |
Output is correct |
8 |
Correct |
2 ms |
11096 KB |
Output is correct |
9 |
Correct |
3 ms |
11100 KB |
Output is correct |
10 |
Correct |
3 ms |
11100 KB |
Output is correct |
11 |
Correct |
3 ms |
11100 KB |
Output is correct |
12 |
Correct |
2 ms |
11100 KB |
Output is correct |
13 |
Correct |
2 ms |
11180 KB |
Output is correct |
14 |
Correct |
2 ms |
11100 KB |
Output is correct |
15 |
Correct |
3 ms |
11100 KB |
Output is correct |
16 |
Correct |
2 ms |
11100 KB |
Output is correct |
17 |
Correct |
4 ms |
11352 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
19 |
Correct |
3 ms |
13148 KB |
Output is correct |
20 |
Correct |
5 ms |
13404 KB |
Output is correct |
21 |
Correct |
2 ms |
11100 KB |
Output is correct |
22 |
Correct |
2 ms |
13148 KB |
Output is correct |
23 |
Correct |
6 ms |
13404 KB |
Output is correct |
24 |
Correct |
8 ms |
13404 KB |
Output is correct |
25 |
Correct |
5 ms |
13148 KB |
Output is correct |
26 |
Correct |
4 ms |
13348 KB |
Output is correct |
27 |
Correct |
5 ms |
13148 KB |
Output is correct |
28 |
Correct |
5 ms |
13148 KB |
Output is correct |
29 |
Correct |
10 ms |
13404 KB |
Output is correct |
30 |
Correct |
8 ms |
13144 KB |
Output is correct |
31 |
Correct |
7 ms |
13404 KB |
Output is correct |
32 |
Correct |
6 ms |
13400 KB |
Output is correct |
33 |
Correct |
12 ms |
13404 KB |
Output is correct |
34 |
Correct |
12 ms |
13404 KB |
Output is correct |
35 |
Correct |
11 ms |
13660 KB |
Output is correct |
36 |
Correct |
5 ms |
11352 KB |
Output is correct |
37 |
Correct |
14 ms |
13820 KB |
Output is correct |
38 |
Correct |
15 ms |
13848 KB |
Output is correct |
39 |
Correct |
15 ms |
13660 KB |
Output is correct |
40 |
Correct |
15 ms |
13660 KB |
Output is correct |
41 |
Correct |
15 ms |
13848 KB |
Output is correct |
42 |
Correct |
8 ms |
13404 KB |
Output is correct |
43 |
Correct |
7 ms |
13404 KB |
Output is correct |
44 |
Correct |
7 ms |
13636 KB |
Output is correct |
45 |
Correct |
15 ms |
13660 KB |
Output is correct |
46 |
Correct |
15 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11100 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Correct |
2 ms |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11100 KB |
Output is correct |
7 |
Correct |
2 ms |
11100 KB |
Output is correct |
8 |
Correct |
2 ms |
11100 KB |
Output is correct |
9 |
Correct |
2 ms |
11100 KB |
Output is correct |
10 |
Correct |
2 ms |
11100 KB |
Output is correct |
11 |
Correct |
4 ms |
11100 KB |
Output is correct |
12 |
Correct |
2 ms |
11100 KB |
Output is correct |
13 |
Correct |
2 ms |
11100 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
4 ms |
11096 KB |
Output is correct |
16 |
Correct |
2 ms |
11100 KB |
Output is correct |
17 |
Correct |
4 ms |
11356 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
19 |
Correct |
3 ms |
13148 KB |
Output is correct |
20 |
Correct |
5 ms |
13404 KB |
Output is correct |
21 |
Correct |
2 ms |
11100 KB |
Output is correct |
22 |
Correct |
2 ms |
13148 KB |
Output is correct |
23 |
Correct |
6 ms |
13404 KB |
Output is correct |
24 |
Correct |
6 ms |
13404 KB |
Output is correct |
25 |
Correct |
5 ms |
13148 KB |
Output is correct |
26 |
Correct |
5 ms |
13148 KB |
Output is correct |
27 |
Correct |
4 ms |
13144 KB |
Output is correct |
28 |
Correct |
5 ms |
13144 KB |
Output is correct |
29 |
Correct |
11 ms |
13404 KB |
Output is correct |
30 |
Correct |
7 ms |
13148 KB |
Output is correct |
31 |
Correct |
7 ms |
13400 KB |
Output is correct |
32 |
Correct |
6 ms |
13404 KB |
Output is correct |
33 |
Correct |
11 ms |
13400 KB |
Output is correct |
34 |
Correct |
11 ms |
13400 KB |
Output is correct |
35 |
Correct |
12 ms |
13660 KB |
Output is correct |
36 |
Correct |
5 ms |
11356 KB |
Output is correct |
37 |
Correct |
14 ms |
13676 KB |
Output is correct |
38 |
Correct |
17 ms |
13660 KB |
Output is correct |
39 |
Correct |
15 ms |
13660 KB |
Output is correct |
40 |
Correct |
15 ms |
13656 KB |
Output is correct |
41 |
Correct |
16 ms |
13656 KB |
Output is correct |
42 |
Correct |
7 ms |
13400 KB |
Output is correct |
43 |
Correct |
7 ms |
13416 KB |
Output is correct |
44 |
Correct |
7 ms |
13912 KB |
Output is correct |
45 |
Correct |
15 ms |
13672 KB |
Output is correct |
46 |
Correct |
20 ms |
13660 KB |
Output is correct |
47 |
Correct |
66 ms |
14292 KB |
Output is correct |
48 |
Correct |
6 ms |
13660 KB |
Output is correct |
49 |
Correct |
7 ms |
13656 KB |
Output is correct |
50 |
Correct |
5 ms |
13660 KB |
Output is correct |
51 |
Correct |
95 ms |
14552 KB |
Output is correct |
52 |
Correct |
106 ms |
14552 KB |
Output is correct |
53 |
Correct |
38 ms |
11612 KB |
Output is correct |
54 |
Correct |
16 ms |
11100 KB |
Output is correct |
55 |
Correct |
23 ms |
11260 KB |
Output is correct |
56 |
Correct |
40 ms |
14404 KB |
Output is correct |
57 |
Correct |
152 ms |
14744 KB |
Output is correct |
58 |
Correct |
34 ms |
11356 KB |
Output is correct |
59 |
Correct |
39 ms |
11392 KB |
Output is correct |
60 |
Correct |
40 ms |
13404 KB |
Output is correct |
61 |
Correct |
40 ms |
11356 KB |
Output is correct |
62 |
Correct |
53 ms |
12196 KB |
Output is correct |
63 |
Correct |
104 ms |
13524 KB |
Output is correct |
64 |
Correct |
98 ms |
13516 KB |
Output is correct |
65 |
Correct |
115 ms |
17364 KB |
Output is correct |
66 |
Correct |
213 ms |
16532 KB |
Output is correct |
67 |
Correct |
193 ms |
16072 KB |
Output is correct |