#include <bits/stdc++.h>
using namespace std;
#define dbg(x) x
#define prt(x) dbg(cerr << x)
#define pv(x) dbg(cerr << #x << " = " << x << '\n')
#define pv2(x) dbg(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define parr(x) dbg(prt(#x << " = { "); for (auto y : x) prt(y << ' '); prt("}\n");)
#define parr2(x) dbg(prt(#x << " = { "); for (auto [y, z] : x) prt(y << ',' << z << " "); prt("}\n");)
#define parr2d(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr(arr);} prt('\n'));
#define parr2d2(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr2(arr);} prt('\n'));
/*
can jump only multiples of 1 number
get from p0 to p1
can only jump if 0 or it was passed to you
everyone will only have it once?
can add o(n^2) edges and dijkstra
idea:
nsqrtn
from each node
for each p[i] > sqrt(n), dijkstra to all the nodes that work
also for each node
keep sqrtn distances that you can use to travel to nearby nodes
for each node, keep the overall best dist, but also allow jump
values < sqrt(n) that the node itself doesn't have to visit the node
*/
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
int s = (int) sqrt(n);
vector<int> a(m), b(m);
vector<vector<array<int, 2>>> edges(n);
vector<vector<bool>> jump(n, vector<bool>(s, false));
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i];
if (b[i] >= s) {
for (int j = a[i] + b[i]; j < n; j += b[i]) {
edges[a[i]].push_back({j, (j - a[i]) / b[i]});
}
for (int j = a[i] - b[i]; j >= 0; j -= b[i]) {
edges[a[i]].push_back({j, (a[i] - j) / b[i]});
}
} else {
jump[a[i]][b[i]] = true;
}
}
vector<vector<int>> dist(n, vector<int>(s, 1e9));
dist[a[0]][0] = 0;
vector<bool> vis(n, false);
priority_queue<array<int, 3>> q;
q.push({0, a[0], 0});
while (q.size()) {
int d = -q.top()[0], node = q.top()[1], jsz = q.top()[2];
q.pop();
if (d != dist[node][jsz]) continue;
if (!vis[node]) {
for (auto [next, wt] : edges[node]) {
if (d + wt < dist[next][0]) {
dist[next][0] = d + wt;
q.push({-dist[next][0], next, 0});
}
}
for (int nj = 1; nj < s; nj++) {
if (jump[node][nj]) {
if (node + nj < n && d + 1 < dist[node + nj][nj]) {
dist[node + nj][nj] = d + 1;
q.push({-dist[node + nj][nj], node + nj, nj});
}
if (node - nj >= 0 && d + 1 < dist[node - nj][nj]) {
dist[node - nj][nj] = d + 1;
q.push({-dist[node - nj][nj], node - nj, nj});
}
}
}
vis[node] = true;
}
if (jsz != 0) {
if (node + jsz < n && d + 1 < dist[node + jsz][jsz]) {
dist[node + jsz][jsz] = d + 1;
q.push({-dist[node + jsz][jsz], node + jsz, jsz});
}
if (node - jsz >= 0 && d + 1 < dist[node - jsz][jsz]) {
dist[node - jsz][jsz] = d + 1;
q.push({-dist[node - jsz][jsz], node - jsz, jsz});
}
}
}
int mn = 1e9;
for (int i = 0; i < s; i++) {
mn = min(mn, dist[a[1]][i]);
}
cout << (mn == 1e9 ? -1 : mn) << '\n';
}
/*
any observations help
check every line
IF YOUR LINES AREN'T WRONG
CHECK IF YOUR LINES ARE IN THE RIGHT ORDER
NEVER GIVE UP
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
2 ms |
1064 KB |
Output is correct |
29 |
Correct |
6 ms |
1116 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
4 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1112 KB |
Output is correct |
33 |
Correct |
9 ms |
1628 KB |
Output is correct |
34 |
Correct |
9 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
6 ms |
1116 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
5 ms |
1176 KB |
Output is correct |
32 |
Correct |
3 ms |
1116 KB |
Output is correct |
33 |
Correct |
9 ms |
1628 KB |
Output is correct |
34 |
Correct |
9 ms |
1628 KB |
Output is correct |
35 |
Correct |
7 ms |
1844 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
13 ms |
2752 KB |
Output is correct |
38 |
Correct |
14 ms |
2652 KB |
Output is correct |
39 |
Correct |
15 ms |
2648 KB |
Output is correct |
40 |
Correct |
11 ms |
2652 KB |
Output is correct |
41 |
Correct |
11 ms |
2652 KB |
Output is correct |
42 |
Correct |
4 ms |
1372 KB |
Output is correct |
43 |
Correct |
3 ms |
1372 KB |
Output is correct |
44 |
Correct |
3 ms |
1372 KB |
Output is correct |
45 |
Correct |
15 ms |
5240 KB |
Output is correct |
46 |
Correct |
17 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
964 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
6 ms |
1116 KB |
Output is correct |
30 |
Correct |
2 ms |
972 KB |
Output is correct |
31 |
Correct |
3 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1144 KB |
Output is correct |
33 |
Correct |
8 ms |
1692 KB |
Output is correct |
34 |
Correct |
9 ms |
1628 KB |
Output is correct |
35 |
Correct |
8 ms |
1744 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
11 ms |
2600 KB |
Output is correct |
38 |
Correct |
11 ms |
2652 KB |
Output is correct |
39 |
Correct |
11 ms |
2652 KB |
Output is correct |
40 |
Correct |
12 ms |
2904 KB |
Output is correct |
41 |
Correct |
11 ms |
2652 KB |
Output is correct |
42 |
Correct |
4 ms |
1372 KB |
Output is correct |
43 |
Correct |
3 ms |
1372 KB |
Output is correct |
44 |
Correct |
3 ms |
1428 KB |
Output is correct |
45 |
Correct |
15 ms |
5376 KB |
Output is correct |
46 |
Correct |
15 ms |
5228 KB |
Output is correct |
47 |
Correct |
57 ms |
12576 KB |
Output is correct |
48 |
Correct |
14 ms |
18776 KB |
Output is correct |
49 |
Correct |
15 ms |
21084 KB |
Output is correct |
50 |
Correct |
16 ms |
23384 KB |
Output is correct |
51 |
Correct |
64 ms |
27308 KB |
Output is correct |
52 |
Correct |
53 ms |
27216 KB |
Output is correct |
53 |
Correct |
25 ms |
25168 KB |
Output is correct |
54 |
Correct |
16 ms |
24412 KB |
Output is correct |
55 |
Correct |
17 ms |
24412 KB |
Output is correct |
56 |
Correct |
21 ms |
24872 KB |
Output is correct |
57 |
Correct |
67 ms |
24700 KB |
Output is correct |
58 |
Correct |
24 ms |
24792 KB |
Output is correct |
59 |
Correct |
26 ms |
24924 KB |
Output is correct |
60 |
Correct |
31 ms |
25232 KB |
Output is correct |
61 |
Correct |
27 ms |
25180 KB |
Output is correct |
62 |
Correct |
35 ms |
27480 KB |
Output is correct |
63 |
Correct |
68 ms |
47280 KB |
Output is correct |
64 |
Correct |
76 ms |
52796 KB |
Output is correct |
65 |
Correct |
205 ms |
53616 KB |
Output is correct |
66 |
Correct |
546 ms |
48076 KB |
Output is correct |
67 |
Correct |
552 ms |
48076 KB |
Output is correct |