#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
const short MAX_N = 30000;
const int MAX_W = 7350000;
short qb[MAX_W], qp[MAX_W];
short B[MAX_N], P[MAX_N];
gp_hash_table <short, int> dist[MAX_N];
vector <short> graph[MAX_N];
int main() {
short N, M;
cin >> N >> M;
for(short i = 0; i < M; i++) {
cin >> B[i] >> P[i];
graph[B[i]].push_back(P[i]);
}
for(short i = 0; i < N; i++) {
sort(graph[i].begin(), graph[i].end());
graph[i].resize(unique(graph[i].begin(), graph[i].end()) - graph[i].begin());
}
int l = 0, r = 0;
qb[r] = B[0];
qp[r++] = P[0];
dist[B[0]][P[0]] = 0;
while(l < r) {
short b = qb[l], p = qp[l++];
if(b == B[1]) {
cout << dist[b][p];
return 0;
}
if(b - p >= 0 and dist[b - p].find(p) == dist[b - p].end()) {
dist[b - p][p] = dist[b][p] + 1;
qb[r] = b - p;
qp[r++] = p;
}
if(b + p < N and dist[b + p].find(p) == dist[b + p].end()) {
dist[b + p][p] = dist[b][p] + 1;
qb[r] = b + p;
qp[r++] = p;
}
for(short p2 : graph[b]) {
if(b - p2 >= 0 and dist[b - p2].find(p2) == dist[b - p2].end()) {
dist[b - p2][p2] = dist[b][p] + 1;
qb[r] = b - p2;
qp[r++] = p2;
}
if(b + p2 < N and dist[b + p2].find(p2) == dist[b + p2].end()) {
dist[b + p2][p2] = dist[b][p] + 1;
qb[r] = b + p2;
qp[r++] = p2;
}
}
}
cout << -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7376 KB |
Output is correct |
2 |
Correct |
6 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7380 KB |
Output is correct |
4 |
Correct |
6 ms |
7252 KB |
Output is correct |
5 |
Correct |
6 ms |
7368 KB |
Output is correct |
6 |
Correct |
6 ms |
7252 KB |
Output is correct |
7 |
Correct |
6 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7332 KB |
Output is correct |
2 |
Correct |
6 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7332 KB |
Output is correct |
4 |
Correct |
6 ms |
7380 KB |
Output is correct |
5 |
Correct |
6 ms |
7252 KB |
Output is correct |
6 |
Correct |
6 ms |
7252 KB |
Output is correct |
7 |
Correct |
6 ms |
7256 KB |
Output is correct |
8 |
Correct |
6 ms |
7252 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
6 ms |
7380 KB |
Output is correct |
11 |
Correct |
7 ms |
7336 KB |
Output is correct |
12 |
Correct |
6 ms |
7380 KB |
Output is correct |
13 |
Correct |
8 ms |
7380 KB |
Output is correct |
14 |
Correct |
8 ms |
7508 KB |
Output is correct |
15 |
Correct |
8 ms |
7496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7252 KB |
Output is correct |
2 |
Correct |
6 ms |
7340 KB |
Output is correct |
3 |
Correct |
6 ms |
7348 KB |
Output is correct |
4 |
Correct |
6 ms |
7252 KB |
Output is correct |
5 |
Correct |
6 ms |
7252 KB |
Output is correct |
6 |
Correct |
6 ms |
7252 KB |
Output is correct |
7 |
Correct |
5 ms |
7252 KB |
Output is correct |
8 |
Correct |
6 ms |
7252 KB |
Output is correct |
9 |
Correct |
6 ms |
7252 KB |
Output is correct |
10 |
Correct |
6 ms |
7380 KB |
Output is correct |
11 |
Correct |
7 ms |
7320 KB |
Output is correct |
12 |
Correct |
7 ms |
7388 KB |
Output is correct |
13 |
Correct |
7 ms |
7380 KB |
Output is correct |
14 |
Correct |
8 ms |
7508 KB |
Output is correct |
15 |
Correct |
7 ms |
7464 KB |
Output is correct |
16 |
Correct |
6 ms |
7380 KB |
Output is correct |
17 |
Correct |
8 ms |
7508 KB |
Output is correct |
18 |
Correct |
6 ms |
7380 KB |
Output is correct |
19 |
Correct |
6 ms |
7380 KB |
Output is correct |
20 |
Correct |
7 ms |
7380 KB |
Output is correct |
21 |
Correct |
6 ms |
7380 KB |
Output is correct |
22 |
Correct |
6 ms |
7380 KB |
Output is correct |
23 |
Correct |
7 ms |
7380 KB |
Output is correct |
24 |
Correct |
9 ms |
7508 KB |
Output is correct |
25 |
Correct |
7 ms |
7380 KB |
Output is correct |
26 |
Correct |
7 ms |
7380 KB |
Output is correct |
27 |
Correct |
7 ms |
7380 KB |
Output is correct |
28 |
Correct |
10 ms |
8020 KB |
Output is correct |
29 |
Correct |
12 ms |
9792 KB |
Output is correct |
30 |
Correct |
8 ms |
7892 KB |
Output is correct |
31 |
Correct |
10 ms |
8776 KB |
Output is correct |
32 |
Correct |
9 ms |
8020 KB |
Output is correct |
33 |
Correct |
15 ms |
11364 KB |
Output is correct |
34 |
Correct |
13 ms |
9556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7292 KB |
Output is correct |
2 |
Correct |
6 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7252 KB |
Output is correct |
4 |
Correct |
6 ms |
7288 KB |
Output is correct |
5 |
Correct |
7 ms |
7252 KB |
Output is correct |
6 |
Correct |
6 ms |
7348 KB |
Output is correct |
7 |
Correct |
6 ms |
7252 KB |
Output is correct |
8 |
Correct |
6 ms |
7252 KB |
Output is correct |
9 |
Correct |
7 ms |
7252 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
11 |
Correct |
6 ms |
7380 KB |
Output is correct |
12 |
Correct |
7 ms |
7380 KB |
Output is correct |
13 |
Correct |
9 ms |
7380 KB |
Output is correct |
14 |
Correct |
9 ms |
7508 KB |
Output is correct |
15 |
Correct |
8 ms |
7544 KB |
Output is correct |
16 |
Correct |
6 ms |
7380 KB |
Output is correct |
17 |
Correct |
7 ms |
7508 KB |
Output is correct |
18 |
Correct |
8 ms |
7380 KB |
Output is correct |
19 |
Correct |
6 ms |
7392 KB |
Output is correct |
20 |
Correct |
7 ms |
7380 KB |
Output is correct |
21 |
Correct |
7 ms |
7380 KB |
Output is correct |
22 |
Correct |
7 ms |
7380 KB |
Output is correct |
23 |
Correct |
7 ms |
7508 KB |
Output is correct |
24 |
Correct |
10 ms |
7508 KB |
Output is correct |
25 |
Correct |
8 ms |
7380 KB |
Output is correct |
26 |
Correct |
7 ms |
7380 KB |
Output is correct |
27 |
Correct |
6 ms |
7472 KB |
Output is correct |
28 |
Correct |
10 ms |
8064 KB |
Output is correct |
29 |
Correct |
12 ms |
9812 KB |
Output is correct |
30 |
Correct |
8 ms |
7892 KB |
Output is correct |
31 |
Correct |
10 ms |
8808 KB |
Output is correct |
32 |
Correct |
9 ms |
7984 KB |
Output is correct |
33 |
Correct |
17 ms |
11356 KB |
Output is correct |
34 |
Correct |
12 ms |
9556 KB |
Output is correct |
35 |
Correct |
22 ms |
8492 KB |
Output is correct |
36 |
Correct |
8 ms |
7380 KB |
Output is correct |
37 |
Correct |
26 ms |
8312 KB |
Output is correct |
38 |
Correct |
23 ms |
8080 KB |
Output is correct |
39 |
Correct |
21 ms |
7548 KB |
Output is correct |
40 |
Correct |
21 ms |
7544 KB |
Output is correct |
41 |
Correct |
20 ms |
7680 KB |
Output is correct |
42 |
Correct |
17 ms |
7556 KB |
Output is correct |
43 |
Correct |
17 ms |
7508 KB |
Output is correct |
44 |
Correct |
17 ms |
7508 KB |
Output is correct |
45 |
Correct |
83 ms |
21808 KB |
Output is correct |
46 |
Correct |
81 ms |
17316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7252 KB |
Output is correct |
2 |
Correct |
6 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7252 KB |
Output is correct |
4 |
Correct |
6 ms |
7380 KB |
Output is correct |
5 |
Correct |
6 ms |
7252 KB |
Output is correct |
6 |
Correct |
6 ms |
7296 KB |
Output is correct |
7 |
Correct |
7 ms |
7256 KB |
Output is correct |
8 |
Correct |
6 ms |
7252 KB |
Output is correct |
9 |
Correct |
6 ms |
7256 KB |
Output is correct |
10 |
Correct |
6 ms |
7252 KB |
Output is correct |
11 |
Correct |
6 ms |
7380 KB |
Output is correct |
12 |
Correct |
6 ms |
7380 KB |
Output is correct |
13 |
Correct |
8 ms |
7380 KB |
Output is correct |
14 |
Correct |
7 ms |
7508 KB |
Output is correct |
15 |
Correct |
8 ms |
7576 KB |
Output is correct |
16 |
Correct |
7 ms |
7380 KB |
Output is correct |
17 |
Correct |
7 ms |
7508 KB |
Output is correct |
18 |
Correct |
8 ms |
7380 KB |
Output is correct |
19 |
Correct |
6 ms |
7380 KB |
Output is correct |
20 |
Correct |
7 ms |
7372 KB |
Output is correct |
21 |
Correct |
6 ms |
7380 KB |
Output is correct |
22 |
Correct |
6 ms |
7300 KB |
Output is correct |
23 |
Correct |
7 ms |
7468 KB |
Output is correct |
24 |
Correct |
8 ms |
7508 KB |
Output is correct |
25 |
Correct |
8 ms |
7380 KB |
Output is correct |
26 |
Correct |
9 ms |
7412 KB |
Output is correct |
27 |
Correct |
8 ms |
7464 KB |
Output is correct |
28 |
Correct |
10 ms |
8008 KB |
Output is correct |
29 |
Correct |
11 ms |
9816 KB |
Output is correct |
30 |
Correct |
11 ms |
7920 KB |
Output is correct |
31 |
Correct |
10 ms |
8788 KB |
Output is correct |
32 |
Correct |
11 ms |
8040 KB |
Output is correct |
33 |
Correct |
16 ms |
11348 KB |
Output is correct |
34 |
Correct |
12 ms |
9616 KB |
Output is correct |
35 |
Correct |
24 ms |
8396 KB |
Output is correct |
36 |
Correct |
9 ms |
7420 KB |
Output is correct |
37 |
Correct |
19 ms |
8308 KB |
Output is correct |
38 |
Correct |
25 ms |
8032 KB |
Output is correct |
39 |
Correct |
19 ms |
7508 KB |
Output is correct |
40 |
Correct |
20 ms |
7632 KB |
Output is correct |
41 |
Correct |
20 ms |
7728 KB |
Output is correct |
42 |
Correct |
20 ms |
7532 KB |
Output is correct |
43 |
Correct |
19 ms |
7596 KB |
Output is correct |
44 |
Correct |
21 ms |
7620 KB |
Output is correct |
45 |
Correct |
85 ms |
21736 KB |
Output is correct |
46 |
Correct |
77 ms |
17220 KB |
Output is correct |
47 |
Correct |
19 ms |
7988 KB |
Output is correct |
48 |
Correct |
17 ms |
7892 KB |
Output is correct |
49 |
Correct |
17 ms |
7892 KB |
Output is correct |
50 |
Correct |
14 ms |
7792 KB |
Output is correct |
51 |
Correct |
31 ms |
8780 KB |
Output is correct |
52 |
Correct |
35 ms |
9104 KB |
Output is correct |
53 |
Correct |
23 ms |
8068 KB |
Output is correct |
54 |
Correct |
9 ms |
7380 KB |
Output is correct |
55 |
Correct |
8 ms |
7380 KB |
Output is correct |
56 |
Correct |
21 ms |
8404 KB |
Output is correct |
57 |
Correct |
8 ms |
7380 KB |
Output is correct |
58 |
Correct |
20 ms |
8788 KB |
Output is correct |
59 |
Correct |
22 ms |
8164 KB |
Output is correct |
60 |
Correct |
21 ms |
8224 KB |
Output is correct |
61 |
Correct |
23 ms |
8188 KB |
Output is correct |
62 |
Correct |
118 ms |
24384 KB |
Output is correct |
63 |
Correct |
543 ms |
108548 KB |
Output is correct |
64 |
Correct |
656 ms |
112076 KB |
Output is correct |
65 |
Correct |
942 ms |
195316 KB |
Output is correct |
66 |
Correct |
882 ms |
222524 KB |
Output is correct |
67 |
Correct |
538 ms |
139340 KB |
Output is correct |