#include <bits/stdc++.h>
#define FOR(i,j,k) for(int i=j; i<=k; ++i)
#define ROF(i,j,k) for(int i=j; i>=k; --i)
inline int read (void) {
int x = 0, f = 1, ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -f; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
using tiii = std::tuple <int, int, int>;
const int maxn = 30005;
std::bitset <maxn> b[maxn];
std::vector <int> v[maxn];
std::queue <tiii> q;
int f[maxn];
int main (void) {
int n = read(), m = read(), pos0, pos1;
pos0 = read();
v[pos0].push_back (read());
pos1 = read();
v[pos1].push_back (read());
FOR(i,3,m) {
int x = read(), y = read();
v[x].push_back (y);
}
f[pos0] = 1;
for(auto&it:v[pos0]) q.emplace (pos0, it, 1);
while(!f[pos1] && !q.empty()) {
int x, y, z; std::tie (x, y, z) = q.front(); q.pop();
if(x + y < n && !b[x + y].test (y)) {
b[x + y].set (y); q.emplace (x + y, y, z + 1);
if(!f[x + y]) {
f[x + y] = z + 1;
for(auto&it:v[x + y]) q.emplace (x + y, it, z + 1);
}
}
if(x - y >= 0 && !b[x - y].test (y)) {
b[x - y].set (y); q.emplace (x - y, y, z + 1);
if(!f[x - y]) {
f[x - y] = z + 1;
for(auto&it:v[x - y]) q.emplace (x - y, it, z + 1);
}
}
}
printf("%d\n", f[pos1] - 1);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
3284 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
5 ms |
8404 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
4 ms |
6356 KB |
Output is correct |
24 |
Correct |
5 ms |
7508 KB |
Output is correct |
25 |
Correct |
2 ms |
2388 KB |
Output is correct |
26 |
Correct |
4 ms |
7252 KB |
Output is correct |
27 |
Correct |
5 ms |
7636 KB |
Output is correct |
28 |
Correct |
5 ms |
8404 KB |
Output is correct |
29 |
Correct |
6 ms |
8276 KB |
Output is correct |
30 |
Correct |
6 ms |
8312 KB |
Output is correct |
31 |
Correct |
5 ms |
8276 KB |
Output is correct |
32 |
Correct |
7 ms |
8320 KB |
Output is correct |
33 |
Correct |
7 ms |
8404 KB |
Output is correct |
34 |
Correct |
6 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
2 ms |
1364 KB |
Output is correct |
15 |
Correct |
2 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
3284 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
6 ms |
8404 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
5 ms |
6356 KB |
Output is correct |
24 |
Correct |
5 ms |
7508 KB |
Output is correct |
25 |
Correct |
2 ms |
2388 KB |
Output is correct |
26 |
Correct |
5 ms |
7224 KB |
Output is correct |
27 |
Correct |
5 ms |
7636 KB |
Output is correct |
28 |
Correct |
7 ms |
8404 KB |
Output is correct |
29 |
Correct |
6 ms |
8276 KB |
Output is correct |
30 |
Correct |
6 ms |
8276 KB |
Output is correct |
31 |
Correct |
6 ms |
8276 KB |
Output is correct |
32 |
Correct |
6 ms |
8276 KB |
Output is correct |
33 |
Correct |
8 ms |
8404 KB |
Output is correct |
34 |
Correct |
7 ms |
8300 KB |
Output is correct |
35 |
Correct |
7 ms |
6740 KB |
Output is correct |
36 |
Correct |
2 ms |
2260 KB |
Output is correct |
37 |
Correct |
6 ms |
5460 KB |
Output is correct |
38 |
Correct |
7 ms |
7508 KB |
Output is correct |
39 |
Correct |
3 ms |
1208 KB |
Output is correct |
40 |
Correct |
3 ms |
1492 KB |
Output is correct |
41 |
Correct |
4 ms |
2516 KB |
Output is correct |
42 |
Correct |
5 ms |
7508 KB |
Output is correct |
43 |
Correct |
6 ms |
8148 KB |
Output is correct |
44 |
Correct |
7 ms |
8788 KB |
Output is correct |
45 |
Correct |
15 ms |
9208 KB |
Output is correct |
46 |
Correct |
10 ms |
9172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
2 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
3284 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
5 ms |
8404 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
4 ms |
6356 KB |
Output is correct |
24 |
Correct |
5 ms |
7508 KB |
Output is correct |
25 |
Correct |
2 ms |
2388 KB |
Output is correct |
26 |
Correct |
4 ms |
7252 KB |
Output is correct |
27 |
Correct |
5 ms |
7680 KB |
Output is correct |
28 |
Correct |
6 ms |
8404 KB |
Output is correct |
29 |
Correct |
6 ms |
8276 KB |
Output is correct |
30 |
Correct |
6 ms |
8276 KB |
Output is correct |
31 |
Correct |
7 ms |
8276 KB |
Output is correct |
32 |
Correct |
6 ms |
8312 KB |
Output is correct |
33 |
Correct |
8 ms |
8368 KB |
Output is correct |
34 |
Correct |
6 ms |
8272 KB |
Output is correct |
35 |
Correct |
6 ms |
6740 KB |
Output is correct |
36 |
Correct |
2 ms |
2260 KB |
Output is correct |
37 |
Correct |
4 ms |
5460 KB |
Output is correct |
38 |
Correct |
6 ms |
7508 KB |
Output is correct |
39 |
Correct |
3 ms |
1236 KB |
Output is correct |
40 |
Correct |
3 ms |
1492 KB |
Output is correct |
41 |
Correct |
4 ms |
2516 KB |
Output is correct |
42 |
Correct |
6 ms |
7508 KB |
Output is correct |
43 |
Correct |
6 ms |
8116 KB |
Output is correct |
44 |
Correct |
6 ms |
8788 KB |
Output is correct |
45 |
Correct |
14 ms |
9232 KB |
Output is correct |
46 |
Correct |
11 ms |
9172 KB |
Output is correct |
47 |
Correct |
8 ms |
9684 KB |
Output is correct |
48 |
Correct |
4 ms |
1492 KB |
Output is correct |
49 |
Correct |
3 ms |
1492 KB |
Output is correct |
50 |
Correct |
2 ms |
1364 KB |
Output is correct |
51 |
Correct |
52 ms |
76728 KB |
Output is correct |
52 |
Correct |
53 ms |
83124 KB |
Output is correct |
53 |
Correct |
14 ms |
17364 KB |
Output is correct |
54 |
Correct |
34 ms |
61264 KB |
Output is correct |
55 |
Correct |
42 ms |
77900 KB |
Output is correct |
56 |
Correct |
63 ms |
112100 KB |
Output is correct |
57 |
Correct |
2 ms |
2516 KB |
Output is correct |
58 |
Correct |
62 ms |
111692 KB |
Output is correct |
59 |
Correct |
58 ms |
103684 KB |
Output is correct |
60 |
Correct |
57 ms |
103724 KB |
Output is correct |
61 |
Correct |
56 ms |
98720 KB |
Output is correct |
62 |
Correct |
81 ms |
112080 KB |
Output is correct |
63 |
Correct |
132 ms |
111684 KB |
Output is correct |
64 |
Correct |
149 ms |
111680 KB |
Output is correct |
65 |
Correct |
175 ms |
111780 KB |
Output is correct |
66 |
Correct |
313 ms |
112204 KB |
Output is correct |
67 |
Correct |
192 ms |
111948 KB |
Output is correct |