#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define piii pair<pii,int>
#define fi first
#define se second
const int N = 3e4 + 5;
bitset<N>vis[N];
int n,m,s,t,a[N],b[N];
vector<int>vec[N];
int bfs() {
queue<piii>q;
q.push({{a[1],b[1]},0});
vis[a[1]][b[1]] = true;
while (!q.empty()) {
int u = q.front().fi.fi,steps = q.front().fi.se,d = q.front().se;
q.pop();
if (u == a[2]) return d;
if (u - steps >= 0 && !vis[u - steps][steps]) {
vis[u - steps][steps] = true;
q.push({{u - steps,steps},d + 1});
}
if (u + steps < n && !vis[u + steps][steps]) {
vis[u + steps][steps] = true;
q.push({{u + steps,steps},d + 1});
}
for (auto x: vec[u]) {
if (u - x >= 0 && !vis[u - x][x]) {
vis[u - x][x] = true;
q.push({{u - x,x},d + 1});
}
if (u + x < n && !vis[u + x][x]) {
vis[u + x][x] = true;
q.push({{u + x,x},d + 1});
}
}
}
return -1;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a[i] >> b[i];
vec[a[i]].push_back(b[i]);
}
cout << bfs() << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2688 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2692 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2684 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
4 ms |
9052 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
7260 KB |
Output is correct |
24 |
Correct |
3 ms |
8284 KB |
Output is correct |
25 |
Correct |
2 ms |
5724 KB |
Output is correct |
26 |
Correct |
3 ms |
8036 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
9052 KB |
Output is correct |
29 |
Correct |
4 ms |
8792 KB |
Output is correct |
30 |
Correct |
3 ms |
8792 KB |
Output is correct |
31 |
Correct |
4 ms |
8792 KB |
Output is correct |
32 |
Correct |
4 ms |
8796 KB |
Output is correct |
33 |
Correct |
5 ms |
9052 KB |
Output is correct |
34 |
Correct |
4 ms |
8916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2664 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2700 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4696 KB |
Output is correct |
20 |
Correct |
3 ms |
9052 KB |
Output is correct |
21 |
Correct |
1 ms |
2696 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
7256 KB |
Output is correct |
24 |
Correct |
3 ms |
8332 KB |
Output is correct |
25 |
Correct |
2 ms |
5724 KB |
Output is correct |
26 |
Correct |
3 ms |
8028 KB |
Output is correct |
27 |
Correct |
3 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8992 KB |
Output is correct |
29 |
Correct |
4 ms |
8796 KB |
Output is correct |
30 |
Correct |
3 ms |
8796 KB |
Output is correct |
31 |
Correct |
5 ms |
8948 KB |
Output is correct |
32 |
Correct |
4 ms |
8796 KB |
Output is correct |
33 |
Correct |
5 ms |
9052 KB |
Output is correct |
34 |
Correct |
4 ms |
9052 KB |
Output is correct |
35 |
Correct |
7 ms |
7772 KB |
Output is correct |
36 |
Correct |
2 ms |
3932 KB |
Output is correct |
37 |
Correct |
6 ms |
9564 KB |
Output is correct |
38 |
Correct |
8 ms |
10124 KB |
Output is correct |
39 |
Correct |
6 ms |
5468 KB |
Output is correct |
40 |
Correct |
6 ms |
7564 KB |
Output is correct |
41 |
Correct |
7 ms |
11132 KB |
Output is correct |
42 |
Correct |
6 ms |
8468 KB |
Output is correct |
43 |
Correct |
6 ms |
8924 KB |
Output is correct |
44 |
Correct |
6 ms |
9476 KB |
Output is correct |
45 |
Correct |
24 ms |
10772 KB |
Output is correct |
46 |
Correct |
21 ms |
10856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2660 KB |
Output is correct |
5 |
Correct |
1 ms |
2660 KB |
Output is correct |
6 |
Correct |
1 ms |
2912 KB |
Output is correct |
7 |
Correct |
1 ms |
2660 KB |
Output is correct |
8 |
Correct |
1 ms |
2660 KB |
Output is correct |
9 |
Correct |
1 ms |
2660 KB |
Output is correct |
10 |
Correct |
1 ms |
2656 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
4 ms |
9304 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
7004 KB |
Output is correct |
24 |
Correct |
3 ms |
8284 KB |
Output is correct |
25 |
Correct |
2 ms |
5724 KB |
Output is correct |
26 |
Correct |
4 ms |
8076 KB |
Output is correct |
27 |
Correct |
4 ms |
8336 KB |
Output is correct |
28 |
Correct |
4 ms |
9052 KB |
Output is correct |
29 |
Correct |
4 ms |
9052 KB |
Output is correct |
30 |
Correct |
4 ms |
8796 KB |
Output is correct |
31 |
Correct |
4 ms |
8796 KB |
Output is correct |
32 |
Correct |
3 ms |
8796 KB |
Output is correct |
33 |
Correct |
5 ms |
9056 KB |
Output is correct |
34 |
Correct |
5 ms |
9048 KB |
Output is correct |
35 |
Correct |
7 ms |
7824 KB |
Output is correct |
36 |
Correct |
2 ms |
3932 KB |
Output is correct |
37 |
Correct |
6 ms |
9564 KB |
Output is correct |
38 |
Correct |
8 ms |
10132 KB |
Output is correct |
39 |
Correct |
5 ms |
5720 KB |
Output is correct |
40 |
Correct |
6 ms |
7516 KB |
Output is correct |
41 |
Correct |
8 ms |
11148 KB |
Output is correct |
42 |
Correct |
6 ms |
8616 KB |
Output is correct |
43 |
Correct |
6 ms |
8924 KB |
Output is correct |
44 |
Correct |
6 ms |
9476 KB |
Output is correct |
45 |
Correct |
26 ms |
10844 KB |
Output is correct |
46 |
Correct |
21 ms |
10936 KB |
Output is correct |
47 |
Correct |
17 ms |
33624 KB |
Output is correct |
48 |
Correct |
5 ms |
5468 KB |
Output is correct |
49 |
Correct |
5 ms |
5212 KB |
Output is correct |
50 |
Correct |
4 ms |
5212 KB |
Output is correct |
51 |
Correct |
46 ms |
85332 KB |
Output is correct |
52 |
Correct |
46 ms |
91732 KB |
Output is correct |
53 |
Correct |
13 ms |
18780 KB |
Output is correct |
54 |
Correct |
23 ms |
62300 KB |
Output is correct |
55 |
Correct |
30 ms |
78704 KB |
Output is correct |
56 |
Correct |
49 ms |
112724 KB |
Output is correct |
57 |
Correct |
1 ms |
3420 KB |
Output is correct |
58 |
Correct |
45 ms |
112180 KB |
Output is correct |
59 |
Correct |
50 ms |
104788 KB |
Output is correct |
60 |
Correct |
44 ms |
104404 KB |
Output is correct |
61 |
Correct |
41 ms |
99540 KB |
Output is correct |
62 |
Correct |
84 ms |
112776 KB |
Output is correct |
63 |
Correct |
107 ms |
112720 KB |
Output is correct |
64 |
Correct |
123 ms |
112924 KB |
Output is correct |
65 |
Correct |
164 ms |
112980 KB |
Output is correct |
66 |
Correct |
223 ms |
113764 KB |
Output is correct |
67 |
Correct |
157 ms |
113680 KB |
Output is correct |