#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define SOLVE int t; cin >> t; while (t--) solve();
#define int long long
using namespace std;
signed main() {
#ifdef DEBUG
freopen("main/in.txt", "r", stdin);
#endif
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
int n, m;
cin >> n >> m;
vector<vector<int>> who(n);
vector<map<int, int>> dist(n);
pair<int, int> to;
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> q;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
who[a].push_back(b);
if (i == 0) {
dist[a][b] = 0;
q.push({0, {a, b}});
} else if (i == 1) {
to = {a, b};
}
}
vector<int> used1(n);
vector<map<int, int>> used2(n);
while (q.size()) {
auto [v, p] = q.top().second;
q.pop();
if (used2[v][p]) {
continue;
}
if (v == to.first) {
cout << dist[v][p] << '\n';
return 0;
}
used2[v][p] = 1;
if (!used1[v]) {
used1[v] = 1;
for (int i : who[v]) {
dist[v][i] = dist[v][p];
q.push({dist[v][i], {v, i}});
}
}
if (v + p < n) {
if (!dist[v + p].count(p) || dist[v + p][p] > dist[v][p] + 1) {
dist[v + p][p] = dist[v][p] + 1;
q.push({dist[v + p][p], {v + p, p}});
}
}
if (v - p >= 0) {
if (!dist[v - p].count(p) || dist[v - p][p] > dist[v][p] + 1) {
dist[v - p][p] = dist[v][p] + 1;
q.push({dist[v - p][p], {v - p, p}});
}
}
}
cout << -1 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
5 ms |
1100 KB |
Output is correct |
15 |
Correct |
5 ms |
828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
2 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
5 ms |
1092 KB |
Output is correct |
15 |
Correct |
3 ms |
844 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
832 KB |
Output is correct |
18 |
Correct |
1 ms |
428 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
844 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
3 ms |
1100 KB |
Output is correct |
24 |
Correct |
5 ms |
1380 KB |
Output is correct |
25 |
Correct |
2 ms |
580 KB |
Output is correct |
26 |
Correct |
2 ms |
844 KB |
Output is correct |
27 |
Correct |
2 ms |
1100 KB |
Output is correct |
28 |
Correct |
13 ms |
3124 KB |
Output is correct |
29 |
Correct |
68 ms |
8200 KB |
Output is correct |
30 |
Correct |
11 ms |
2636 KB |
Output is correct |
31 |
Correct |
27 ms |
4548 KB |
Output is correct |
32 |
Correct |
19 ms |
3488 KB |
Output is correct |
33 |
Correct |
146 ms |
15576 KB |
Output is correct |
34 |
Correct |
57 ms |
8692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
216 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
220 KB |
Output is correct |
9 |
Correct |
1 ms |
216 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
2 ms |
472 KB |
Output is correct |
14 |
Correct |
4 ms |
1096 KB |
Output is correct |
15 |
Correct |
3 ms |
856 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
4 ms |
844 KB |
Output is correct |
18 |
Correct |
1 ms |
448 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
844 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
5 ms |
1068 KB |
Output is correct |
24 |
Correct |
5 ms |
1356 KB |
Output is correct |
25 |
Correct |
3 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
1112 KB |
Output is correct |
28 |
Correct |
13 ms |
3020 KB |
Output is correct |
29 |
Correct |
49 ms |
8200 KB |
Output is correct |
30 |
Correct |
10 ms |
2612 KB |
Output is correct |
31 |
Correct |
26 ms |
4668 KB |
Output is correct |
32 |
Correct |
14 ms |
3404 KB |
Output is correct |
33 |
Correct |
108 ms |
15492 KB |
Output is correct |
34 |
Correct |
50 ms |
8752 KB |
Output is correct |
35 |
Correct |
48 ms |
5952 KB |
Output is correct |
36 |
Correct |
3 ms |
704 KB |
Output is correct |
37 |
Correct |
24 ms |
4352 KB |
Output is correct |
38 |
Correct |
14 ms |
2864 KB |
Output is correct |
39 |
Correct |
7 ms |
1168 KB |
Output is correct |
40 |
Correct |
9 ms |
1240 KB |
Output is correct |
41 |
Correct |
10 ms |
1948 KB |
Output is correct |
42 |
Correct |
6 ms |
1292 KB |
Output is correct |
43 |
Correct |
8 ms |
1552 KB |
Output is correct |
44 |
Correct |
9 ms |
2116 KB |
Output is correct |
45 |
Correct |
726 ms |
61960 KB |
Output is correct |
46 |
Correct |
314 ms |
36672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
5 ms |
1112 KB |
Output is correct |
15 |
Correct |
3 ms |
856 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
2 ms |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
584 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
472 KB |
Output is correct |
23 |
Correct |
4 ms |
1084 KB |
Output is correct |
24 |
Correct |
4 ms |
1360 KB |
Output is correct |
25 |
Correct |
2 ms |
728 KB |
Output is correct |
26 |
Correct |
2 ms |
972 KB |
Output is correct |
27 |
Correct |
2 ms |
1084 KB |
Output is correct |
28 |
Correct |
11 ms |
3056 KB |
Output is correct |
29 |
Correct |
51 ms |
8096 KB |
Output is correct |
30 |
Correct |
11 ms |
2648 KB |
Output is correct |
31 |
Correct |
26 ms |
4644 KB |
Output is correct |
32 |
Correct |
13 ms |
3416 KB |
Output is correct |
33 |
Correct |
132 ms |
15568 KB |
Output is correct |
34 |
Correct |
70 ms |
8640 KB |
Output is correct |
35 |
Correct |
37 ms |
5956 KB |
Output is correct |
36 |
Correct |
3 ms |
716 KB |
Output is correct |
37 |
Correct |
23 ms |
4340 KB |
Output is correct |
38 |
Correct |
15 ms |
2824 KB |
Output is correct |
39 |
Correct |
8 ms |
1216 KB |
Output is correct |
40 |
Correct |
12 ms |
1196 KB |
Output is correct |
41 |
Correct |
11 ms |
1912 KB |
Output is correct |
42 |
Correct |
7 ms |
1296 KB |
Output is correct |
43 |
Correct |
9 ms |
1476 KB |
Output is correct |
44 |
Correct |
11 ms |
2120 KB |
Output is correct |
45 |
Correct |
637 ms |
61856 KB |
Output is correct |
46 |
Correct |
293 ms |
36564 KB |
Output is correct |
47 |
Correct |
20 ms |
6160 KB |
Output is correct |
48 |
Correct |
10 ms |
3916 KB |
Output is correct |
49 |
Correct |
7 ms |
4172 KB |
Output is correct |
50 |
Correct |
8 ms |
4300 KB |
Output is correct |
51 |
Correct |
48 ms |
11672 KB |
Output is correct |
52 |
Correct |
68 ms |
11860 KB |
Output is correct |
53 |
Correct |
14 ms |
5964 KB |
Output is correct |
54 |
Correct |
5 ms |
5944 KB |
Output is correct |
55 |
Correct |
6 ms |
7116 KB |
Output is correct |
56 |
Correct |
15 ms |
8928 KB |
Output is correct |
57 |
Correct |
3 ms |
4160 KB |
Output is correct |
58 |
Correct |
41 ms |
13460 KB |
Output is correct |
59 |
Correct |
24 ms |
11740 KB |
Output is correct |
60 |
Correct |
26 ms |
11672 KB |
Output is correct |
61 |
Correct |
24 ms |
11092 KB |
Output is correct |
62 |
Correct |
391 ms |
61248 KB |
Output is correct |
63 |
Execution timed out |
1064 ms |
109636 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |