#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cassert>
#include <tuple>
using namespace std;
const int MAXN = (1 << 15), QUEUE_SIZE = 40000000, CUTOFF = (1 << 8);
vector<int> powers[MAXN];
int dist[MAXN][CUTOFF];
int N, M, S, T;
tuple<int, int, int> qu[QUEUE_SIZE];
int top;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
for (int i = 0; i < M; ++i) {
int b, p;
cin >> b >> p;
if (i == 0) S = b;
else if (i == 1) T = b;
powers[b].push_back(p);
if (p >= CUTOFF) powers[b].push_back(-p);
}
for (int i = 0; i < N; ++i) for (int j = 0; j < CUTOFF; ++j) dist[i][j] = QUEUE_SIZE;
qu[0] = make_tuple(S, 0, 0), top = 0;
for (int qi = 0; qi <= top; ++qi) {
int u, d, c;
tie(u, d, c) = qu[qi];
if (u == T) return cout << c << endl, 0;
powers[u].push_back(d);
for (int d0 : powers[u]) {
if (abs(d0) < CUTOFF && dist[u][d0] == QUEUE_SIZE) dist[u][d0] = c;
if (abs(d0) >= CUTOFF) {
if (0 <= u + d0 && u + d0 < N) qu[++top] = make_tuple(u + d0, d0, c + 1);
} else {
if (u - d0 >= 0 && dist[u - d0][d0] == QUEUE_SIZE) {
qu[++top] = make_tuple(u - d0, d0, dist[u - d0][d0] = c + 1);
}
if (u + d0 < N && dist[u + d0][d0] == QUEUE_SIZE) {
qu[++top] = make_tuple(u + d0, d0, dist[u + d0][d0] = c + 1);
}
}
}
powers[u].clear();
}
cout << -1 << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1128 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
0 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
1152 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1256 KB |
Output is correct |
14 |
Correct |
1 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
1128 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
2 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
2 ms |
1280 KB |
Output is correct |
14 |
Correct |
1 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1280 KB |
Output is correct |
17 |
Correct |
2 ms |
1920 KB |
Output is correct |
18 |
Correct |
2 ms |
2944 KB |
Output is correct |
19 |
Correct |
2 ms |
3200 KB |
Output is correct |
20 |
Correct |
3 ms |
3200 KB |
Output is correct |
21 |
Correct |
2 ms |
1536 KB |
Output is correct |
22 |
Correct |
3 ms |
2944 KB |
Output is correct |
23 |
Correct |
3 ms |
2816 KB |
Output is correct |
24 |
Correct |
3 ms |
3200 KB |
Output is correct |
25 |
Correct |
3 ms |
3200 KB |
Output is correct |
26 |
Correct |
3 ms |
3200 KB |
Output is correct |
27 |
Correct |
3 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3456 KB |
Output is correct |
29 |
Correct |
5 ms |
3840 KB |
Output is correct |
30 |
Correct |
3 ms |
3328 KB |
Output is correct |
31 |
Correct |
3 ms |
3584 KB |
Output is correct |
32 |
Correct |
4 ms |
3456 KB |
Output is correct |
33 |
Correct |
7 ms |
4608 KB |
Output is correct |
34 |
Correct |
4 ms |
3968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
1152 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1280 KB |
Output is correct |
17 |
Correct |
3 ms |
1920 KB |
Output is correct |
18 |
Correct |
3 ms |
2944 KB |
Output is correct |
19 |
Correct |
3 ms |
3072 KB |
Output is correct |
20 |
Correct |
3 ms |
3200 KB |
Output is correct |
21 |
Correct |
1 ms |
1536 KB |
Output is correct |
22 |
Correct |
2 ms |
2944 KB |
Output is correct |
23 |
Correct |
3 ms |
2816 KB |
Output is correct |
24 |
Correct |
3 ms |
3200 KB |
Output is correct |
25 |
Correct |
3 ms |
3200 KB |
Output is correct |
26 |
Correct |
3 ms |
3200 KB |
Output is correct |
27 |
Correct |
3 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3328 KB |
Output is correct |
29 |
Correct |
4 ms |
3840 KB |
Output is correct |
30 |
Correct |
3 ms |
3328 KB |
Output is correct |
31 |
Correct |
3 ms |
3584 KB |
Output is correct |
32 |
Correct |
3 ms |
3456 KB |
Output is correct |
33 |
Correct |
7 ms |
4608 KB |
Output is correct |
34 |
Correct |
4 ms |
3968 KB |
Output is correct |
35 |
Correct |
10 ms |
3200 KB |
Output is correct |
36 |
Correct |
3 ms |
2304 KB |
Output is correct |
37 |
Correct |
8 ms |
3584 KB |
Output is correct |
38 |
Correct |
10 ms |
3840 KB |
Output is correct |
39 |
Correct |
10 ms |
3456 KB |
Output is correct |
40 |
Correct |
10 ms |
3524 KB |
Output is correct |
41 |
Correct |
16 ms |
3584 KB |
Output is correct |
42 |
Correct |
8 ms |
3456 KB |
Output is correct |
43 |
Correct |
8 ms |
3456 KB |
Output is correct |
44 |
Correct |
10 ms |
3328 KB |
Output is correct |
45 |
Correct |
24 ms |
8576 KB |
Output is correct |
46 |
Correct |
20 ms |
6144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
1152 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1280 KB |
Output is correct |
12 |
Correct |
2 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1280 KB |
Output is correct |
16 |
Correct |
2 ms |
1280 KB |
Output is correct |
17 |
Correct |
2 ms |
1920 KB |
Output is correct |
18 |
Correct |
3 ms |
2944 KB |
Output is correct |
19 |
Correct |
2 ms |
3072 KB |
Output is correct |
20 |
Correct |
3 ms |
3200 KB |
Output is correct |
21 |
Correct |
1 ms |
1536 KB |
Output is correct |
22 |
Correct |
3 ms |
2944 KB |
Output is correct |
23 |
Correct |
3 ms |
2816 KB |
Output is correct |
24 |
Correct |
4 ms |
3072 KB |
Output is correct |
25 |
Correct |
3 ms |
3200 KB |
Output is correct |
26 |
Correct |
3 ms |
3200 KB |
Output is correct |
27 |
Correct |
3 ms |
3200 KB |
Output is correct |
28 |
Correct |
3 ms |
3456 KB |
Output is correct |
29 |
Correct |
5 ms |
3840 KB |
Output is correct |
30 |
Correct |
3 ms |
3328 KB |
Output is correct |
31 |
Correct |
4 ms |
3584 KB |
Output is correct |
32 |
Correct |
4 ms |
3456 KB |
Output is correct |
33 |
Correct |
7 ms |
4608 KB |
Output is correct |
34 |
Correct |
6 ms |
3968 KB |
Output is correct |
35 |
Correct |
9 ms |
3200 KB |
Output is correct |
36 |
Correct |
3 ms |
2304 KB |
Output is correct |
37 |
Correct |
8 ms |
3584 KB |
Output is correct |
38 |
Correct |
13 ms |
3840 KB |
Output is correct |
39 |
Correct |
10 ms |
3456 KB |
Output is correct |
40 |
Correct |
11 ms |
3584 KB |
Output is correct |
41 |
Correct |
10 ms |
3584 KB |
Output is correct |
42 |
Correct |
8 ms |
3456 KB |
Output is correct |
43 |
Correct |
9 ms |
3432 KB |
Output is correct |
44 |
Correct |
10 ms |
3328 KB |
Output is correct |
45 |
Correct |
26 ms |
8568 KB |
Output is correct |
46 |
Correct |
17 ms |
6144 KB |
Output is correct |
47 |
Correct |
17 ms |
14080 KB |
Output is correct |
48 |
Correct |
24 ms |
24832 KB |
Output is correct |
49 |
Correct |
24 ms |
27000 KB |
Output is correct |
50 |
Correct |
24 ms |
29440 KB |
Output is correct |
51 |
Correct |
36 ms |
32504 KB |
Output is correct |
52 |
Correct |
39 ms |
32604 KB |
Output is correct |
53 |
Correct |
34 ms |
31864 KB |
Output is correct |
54 |
Correct |
37 ms |
31864 KB |
Output is correct |
55 |
Correct |
21 ms |
32104 KB |
Output is correct |
56 |
Correct |
31 ms |
32512 KB |
Output is correct |
57 |
Correct |
20 ms |
31232 KB |
Output is correct |
58 |
Correct |
30 ms |
33080 KB |
Output is correct |
59 |
Correct |
30 ms |
32888 KB |
Output is correct |
60 |
Correct |
30 ms |
32904 KB |
Output is correct |
61 |
Correct |
31 ms |
32760 KB |
Output is correct |
62 |
Correct |
63 ms |
36984 KB |
Output is correct |
63 |
Correct |
128 ms |
58872 KB |
Output is correct |
64 |
Correct |
174 ms |
65016 KB |
Output is correct |
65 |
Correct |
350 ms |
79416 KB |
Output is correct |
66 |
Correct |
743 ms |
116732 KB |
Output is correct |
67 |
Correct |
327 ms |
75564 KB |
Output is correct |