#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#endif
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define gc getchar()
#define pc(x) putchar(x)
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=char(n%10+48);n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
using pii = tuple<int, int, int>;
constexpr int MM = 3e4+5, NN = (MM<<15);
#define val(b, a) ((b)|((a)<<15))
deque<pii> q;
int n, m, a[MM], b[MM];
vector<int> in[MM];
bitset<NN> vis;
int main(){
scan(n, m);
for(int i = 0; i < m; i++){
scan(b[i], a[i]);
in[b[i]].emplace_back(a[i]);
}
vis[val(b[0], a[0])] = 1;
q.emplace_back(0, b[0], a[0]);
while(size(q)){
auto [dis, i, d] = q.front(); q.pop_front();
if(i == b[1]){
print(dis);
return 0;
}
int u = i-d;
if(u >= 0){
if(!vis[val(u, d)]){
vis[val(u, d)] = 1;
q.emplace_back(dis+1, u, d);
}
}
u = i+d;
if(u < n){
if(!vis[val(u, d)]){
vis[val(u, d)] = 1;
q.emplace_back(dis+1, u, d);
}
}
for(int dd: in[i]){
if(!vis[val(i, dd)]){
vis[val(i, dd)] = 1;
q.emplace_front(dis, i, dd);
}
}
in[i].clear();
}
print(-1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1408 KB |
Output is correct |
12 |
Correct |
1 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
1024 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1408 KB |
Output is correct |
12 |
Correct |
1 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
1152 KB |
Output is correct |
14 |
Correct |
2 ms |
1408 KB |
Output is correct |
15 |
Correct |
1 ms |
1408 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1152 KB |
Output is correct |
20 |
Correct |
2 ms |
1152 KB |
Output is correct |
21 |
Correct |
1 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
3 ms |
3840 KB |
Output is correct |
24 |
Correct |
6 ms |
5760 KB |
Output is correct |
25 |
Correct |
3 ms |
2304 KB |
Output is correct |
26 |
Correct |
1 ms |
1152 KB |
Output is correct |
27 |
Correct |
2 ms |
1664 KB |
Output is correct |
28 |
Correct |
3 ms |
2176 KB |
Output is correct |
29 |
Correct |
2 ms |
1152 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
31 |
Correct |
2 ms |
1152 KB |
Output is correct |
32 |
Correct |
1 ms |
1152 KB |
Output is correct |
33 |
Correct |
4 ms |
1408 KB |
Output is correct |
34 |
Correct |
3 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1056 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1408 KB |
Output is correct |
12 |
Correct |
1 ms |
1152 KB |
Output is correct |
13 |
Correct |
2 ms |
1152 KB |
Output is correct |
14 |
Correct |
1 ms |
1408 KB |
Output is correct |
15 |
Correct |
1 ms |
1408 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1152 KB |
Output is correct |
20 |
Correct |
1 ms |
1152 KB |
Output is correct |
21 |
Correct |
1 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
3 ms |
3840 KB |
Output is correct |
24 |
Correct |
6 ms |
5760 KB |
Output is correct |
25 |
Correct |
2 ms |
2304 KB |
Output is correct |
26 |
Correct |
1 ms |
1152 KB |
Output is correct |
27 |
Correct |
2 ms |
1792 KB |
Output is correct |
28 |
Correct |
3 ms |
2176 KB |
Output is correct |
29 |
Correct |
3 ms |
1152 KB |
Output is correct |
30 |
Correct |
2 ms |
1024 KB |
Output is correct |
31 |
Correct |
2 ms |
1184 KB |
Output is correct |
32 |
Correct |
2 ms |
1152 KB |
Output is correct |
33 |
Correct |
4 ms |
1408 KB |
Output is correct |
34 |
Correct |
3 ms |
1408 KB |
Output is correct |
35 |
Correct |
13 ms |
9856 KB |
Output is correct |
36 |
Correct |
4 ms |
3712 KB |
Output is correct |
37 |
Correct |
8 ms |
5504 KB |
Output is correct |
38 |
Correct |
13 ms |
9984 KB |
Output is correct |
39 |
Correct |
6 ms |
2560 KB |
Output is correct |
40 |
Correct |
11 ms |
8192 KB |
Output is correct |
41 |
Correct |
12 ms |
9344 KB |
Output is correct |
42 |
Correct |
4 ms |
1792 KB |
Output is correct |
43 |
Correct |
5 ms |
2304 KB |
Output is correct |
44 |
Correct |
4 ms |
1760 KB |
Output is correct |
45 |
Correct |
17 ms |
2944 KB |
Output is correct |
46 |
Correct |
14 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1312 KB |
Output is correct |
11 |
Correct |
2 ms |
1408 KB |
Output is correct |
12 |
Correct |
1 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
1024 KB |
Output is correct |
14 |
Correct |
1 ms |
1408 KB |
Output is correct |
15 |
Correct |
1 ms |
1408 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1152 KB |
Output is correct |
20 |
Correct |
1 ms |
1152 KB |
Output is correct |
21 |
Correct |
1 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
3 ms |
3840 KB |
Output is correct |
24 |
Correct |
5 ms |
5760 KB |
Output is correct |
25 |
Correct |
2 ms |
2304 KB |
Output is correct |
26 |
Correct |
1 ms |
1152 KB |
Output is correct |
27 |
Correct |
2 ms |
1664 KB |
Output is correct |
28 |
Correct |
3 ms |
2176 KB |
Output is correct |
29 |
Correct |
3 ms |
1152 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
31 |
Correct |
2 ms |
1152 KB |
Output is correct |
32 |
Correct |
2 ms |
1152 KB |
Output is correct |
33 |
Correct |
4 ms |
1408 KB |
Output is correct |
34 |
Correct |
3 ms |
1408 KB |
Output is correct |
35 |
Correct |
14 ms |
9856 KB |
Output is correct |
36 |
Correct |
4 ms |
3712 KB |
Output is correct |
37 |
Correct |
8 ms |
5504 KB |
Output is correct |
38 |
Correct |
13 ms |
9856 KB |
Output is correct |
39 |
Correct |
7 ms |
2560 KB |
Output is correct |
40 |
Correct |
11 ms |
8192 KB |
Output is correct |
41 |
Correct |
12 ms |
9344 KB |
Output is correct |
42 |
Correct |
4 ms |
1792 KB |
Output is correct |
43 |
Correct |
5 ms |
2304 KB |
Output is correct |
44 |
Correct |
4 ms |
1664 KB |
Output is correct |
45 |
Correct |
17 ms |
2944 KB |
Output is correct |
46 |
Correct |
12 ms |
2944 KB |
Output is correct |
47 |
Correct |
14 ms |
11008 KB |
Output is correct |
48 |
Correct |
6 ms |
1920 KB |
Output is correct |
49 |
Correct |
6 ms |
1920 KB |
Output is correct |
50 |
Correct |
5 ms |
1664 KB |
Output is correct |
51 |
Correct |
68 ms |
71544 KB |
Output is correct |
52 |
Correct |
75 ms |
78968 KB |
Output is correct |
53 |
Correct |
20 ms |
15488 KB |
Output is correct |
54 |
Correct |
1 ms |
1024 KB |
Output is correct |
55 |
Correct |
2 ms |
1024 KB |
Output is correct |
56 |
Correct |
8 ms |
2432 KB |
Output is correct |
57 |
Correct |
2 ms |
1184 KB |
Output is correct |
58 |
Correct |
5 ms |
1736 KB |
Output is correct |
59 |
Correct |
6 ms |
1800 KB |
Output is correct |
60 |
Correct |
6 ms |
1792 KB |
Output is correct |
61 |
Correct |
7 ms |
3072 KB |
Output is correct |
62 |
Correct |
32 ms |
14584 KB |
Output is correct |
63 |
Correct |
81 ms |
3488 KB |
Output is correct |
64 |
Correct |
80 ms |
2936 KB |
Output is correct |
65 |
Correct |
103 ms |
3192 KB |
Output is correct |
66 |
Correct |
169 ms |
3068 KB |
Output is correct |
67 |
Correct |
91 ms |
3064 KB |
Output is correct |