#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define keish ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
const int mod = 1e9 + 7;
int n, m, b, p, st, ed;
signed main(){
keish;
cin >> n >> m;
vector<vector<int>> e(n);
for(int i = 0; i < m; i++){
cin >> b >> p;
e[b].push_back(p);
if(i == 0) st = b;
if(i == 1) ed = b;
}
vector<vector<int>> dis(n, vector<int>(2005, -1));
queue<pair<int, int>> q;
for(auto x : e[st]){
dis[st][x] = 0;
q.push({st, x});
}
while(!q.empty()){
auto [u, d] = q.front(); q.pop();
if(u == ed){
cout << dis[u][d] << '\n';
return 0;
}
if(u + d < n){
if(dis[u + d][d] == -1){
dis[u + d][d] = dis[u][d] + 1;
q.push({u + d, d});
}
for(auto v : e[u + d]){
if(dis[u + d][v] == -1){
dis[u + d][v] = dis[u][d] + 1;
q.push({u + d, v});
}
}
}
if(u - d >= 0){
if(dis[u - d][d] == -1){
dis[u - d][d] = dis[u][d] + 1;
q.push({u - d, d});
}
for(auto v : e[u - d]){
if(dis[u - d][v] == -1){
dis[u - d][v] = dis[u][d] + 1;
q.push({u - d, v});
}
}
}
}
cout << -1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
1148 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
5 ms |
11988 KB |
Output is correct |
18 |
Correct |
11 ms |
27808 KB |
Output is correct |
19 |
Correct |
12 ms |
31808 KB |
Output is correct |
20 |
Correct |
13 ms |
31848 KB |
Output is correct |
21 |
Correct |
3 ms |
6484 KB |
Output is correct |
22 |
Correct |
12 ms |
28296 KB |
Output is correct |
23 |
Correct |
11 ms |
25540 KB |
Output is correct |
24 |
Correct |
13 ms |
30284 KB |
Output is correct |
25 |
Correct |
13 ms |
31828 KB |
Output is correct |
26 |
Correct |
13 ms |
31828 KB |
Output is correct |
27 |
Correct |
13 ms |
31828 KB |
Output is correct |
28 |
Correct |
15 ms |
31828 KB |
Output is correct |
29 |
Correct |
14 ms |
31784 KB |
Output is correct |
30 |
Correct |
13 ms |
31812 KB |
Output is correct |
31 |
Correct |
13 ms |
31808 KB |
Output is correct |
32 |
Correct |
15 ms |
31828 KB |
Output is correct |
33 |
Correct |
16 ms |
31828 KB |
Output is correct |
34 |
Correct |
15 ms |
31828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
1 ms |
1948 KB |
Output is correct |
15 |
Correct |
1 ms |
1860 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
6 ms |
11988 KB |
Output is correct |
18 |
Correct |
14 ms |
27860 KB |
Output is correct |
19 |
Correct |
13 ms |
31740 KB |
Output is correct |
20 |
Correct |
13 ms |
31816 KB |
Output is correct |
21 |
Correct |
3 ms |
6484 KB |
Output is correct |
22 |
Correct |
11 ms |
28360 KB |
Output is correct |
23 |
Correct |
11 ms |
25544 KB |
Output is correct |
24 |
Correct |
13 ms |
30260 KB |
Output is correct |
25 |
Correct |
13 ms |
31828 KB |
Output is correct |
26 |
Correct |
13 ms |
31844 KB |
Output is correct |
27 |
Correct |
14 ms |
31828 KB |
Output is correct |
28 |
Correct |
14 ms |
31828 KB |
Output is correct |
29 |
Correct |
15 ms |
31828 KB |
Output is correct |
30 |
Correct |
14 ms |
31736 KB |
Output is correct |
31 |
Correct |
13 ms |
31740 KB |
Output is correct |
32 |
Correct |
13 ms |
31752 KB |
Output is correct |
33 |
Correct |
15 ms |
31896 KB |
Output is correct |
34 |
Correct |
16 ms |
31776 KB |
Output is correct |
35 |
Correct |
17 ms |
23800 KB |
Output is correct |
36 |
Correct |
8 ms |
17508 KB |
Output is correct |
37 |
Correct |
18 ms |
31640 KB |
Output is correct |
38 |
Correct |
22 ms |
32852 KB |
Output is correct |
39 |
Correct |
17 ms |
32468 KB |
Output is correct |
40 |
Correct |
17 ms |
32632 KB |
Output is correct |
41 |
Correct |
19 ms |
32848 KB |
Output is correct |
42 |
Correct |
15 ms |
32208 KB |
Output is correct |
43 |
Correct |
16 ms |
32208 KB |
Output is correct |
44 |
Correct |
16 ms |
32264 KB |
Output is correct |
45 |
Correct |
37 ms |
32784 KB |
Output is correct |
46 |
Correct |
32 ms |
32852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
5 ms |
11980 KB |
Output is correct |
18 |
Correct |
11 ms |
27764 KB |
Output is correct |
19 |
Correct |
12 ms |
31820 KB |
Output is correct |
20 |
Correct |
13 ms |
31876 KB |
Output is correct |
21 |
Correct |
3 ms |
6484 KB |
Output is correct |
22 |
Correct |
12 ms |
28384 KB |
Output is correct |
23 |
Correct |
11 ms |
25544 KB |
Output is correct |
24 |
Correct |
13 ms |
30240 KB |
Output is correct |
25 |
Correct |
13 ms |
31828 KB |
Output is correct |
26 |
Correct |
12 ms |
31772 KB |
Output is correct |
27 |
Correct |
13 ms |
31860 KB |
Output is correct |
28 |
Correct |
14 ms |
31916 KB |
Output is correct |
29 |
Correct |
14 ms |
31828 KB |
Output is correct |
30 |
Correct |
13 ms |
31836 KB |
Output is correct |
31 |
Correct |
14 ms |
31768 KB |
Output is correct |
32 |
Correct |
13 ms |
31804 KB |
Output is correct |
33 |
Correct |
16 ms |
31828 KB |
Output is correct |
34 |
Correct |
14 ms |
31828 KB |
Output is correct |
35 |
Correct |
18 ms |
23812 KB |
Output is correct |
36 |
Correct |
8 ms |
17620 KB |
Output is correct |
37 |
Correct |
17 ms |
31700 KB |
Output is correct |
38 |
Correct |
21 ms |
32948 KB |
Output is correct |
39 |
Correct |
17 ms |
32468 KB |
Output is correct |
40 |
Correct |
17 ms |
32632 KB |
Output is correct |
41 |
Correct |
18 ms |
32872 KB |
Output is correct |
42 |
Correct |
16 ms |
32200 KB |
Output is correct |
43 |
Correct |
15 ms |
32208 KB |
Output is correct |
44 |
Correct |
15 ms |
32240 KB |
Output is correct |
45 |
Correct |
38 ms |
32800 KB |
Output is correct |
46 |
Correct |
31 ms |
32852 KB |
Output is correct |
47 |
Correct |
75 ms |
195632 KB |
Output is correct |
48 |
Runtime error |
95 ms |
262144 KB |
Execution killed with signal 9 |
49 |
Halted |
0 ms |
0 KB |
- |