#include <bits/stdc++.h>
using namespace std;
const int M = 3e4 + 7;
bitset < 30007 > vis[M];
void solve(){
int n, m;
cin >> n >> m;
pair < int , int > doge[m];
vector < int > root[M];
for(int i = 0;i<m;i++){
cin >> doge[i].first >> doge[i].second;
root[doge[i].first].push_back(doge[i].second);
}
queue < pair < int , pair < int , int > > > pq;//dist , konum , zıplayış
pq.push({0,{doge[0].first,0}});
while(pq.size()){
pair < int , pair < int , int > > tmp = pq.front();
pq.pop();
if(vis[tmp.second.first][tmp.second.second] == 1){
continue;
}
if(tmp.second.first == doge[1].first){
cout << tmp.first << '\n';
return;
}
vis[tmp.second.first][tmp.second.second] = 1;
if(tmp.second.first + tmp.second.second < n)pq.push({tmp.first + 1 , {tmp.second.first + tmp.second.second , tmp.second.second}});
if(tmp.second.first - tmp.second.second >= 0)pq.push({tmp.first + 1 , {tmp.second.first - tmp.second.second , tmp.second.second}});
for(auto itr : root[tmp.second.first]){
if(itr == tmp.second.second)continue;
if(tmp.second.first + itr < n)pq.push({tmp.first + 1 , {tmp.second.first + itr , itr}});
if(tmp.second.first - itr >= 0)pq.push({tmp.first + 1 , {tmp.second.first - itr , itr}});
}
}
cout << -1 << '\n';
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1176 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
3 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
3 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
1624 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
944 KB |
Output is correct |
22 |
Correct |
1 ms |
1112 KB |
Output is correct |
23 |
Correct |
4 ms |
6488 KB |
Output is correct |
24 |
Correct |
4 ms |
7516 KB |
Output is correct |
25 |
Correct |
2 ms |
2392 KB |
Output is correct |
26 |
Correct |
3 ms |
7256 KB |
Output is correct |
27 |
Correct |
3 ms |
7772 KB |
Output is correct |
28 |
Correct |
4 ms |
8540 KB |
Output is correct |
29 |
Correct |
5 ms |
8540 KB |
Output is correct |
30 |
Correct |
4 ms |
8284 KB |
Output is correct |
31 |
Correct |
4 ms |
8508 KB |
Output is correct |
32 |
Correct |
4 ms |
8284 KB |
Output is correct |
33 |
Correct |
8 ms |
9492 KB |
Output is correct |
34 |
Correct |
7 ms |
9304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
3 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1208 KB |
Output is correct |
20 |
Correct |
5 ms |
8536 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
3 ms |
6492 KB |
Output is correct |
24 |
Correct |
5 ms |
7516 KB |
Output is correct |
25 |
Correct |
2 ms |
2392 KB |
Output is correct |
26 |
Correct |
3 ms |
7260 KB |
Output is correct |
27 |
Correct |
4 ms |
7708 KB |
Output is correct |
28 |
Correct |
4 ms |
8540 KB |
Output is correct |
29 |
Correct |
5 ms |
8540 KB |
Output is correct |
30 |
Correct |
4 ms |
8284 KB |
Output is correct |
31 |
Correct |
4 ms |
8284 KB |
Output is correct |
32 |
Correct |
4 ms |
8284 KB |
Output is correct |
33 |
Correct |
8 ms |
9560 KB |
Output is correct |
34 |
Correct |
7 ms |
9308 KB |
Output is correct |
35 |
Correct |
8 ms |
8284 KB |
Output is correct |
36 |
Correct |
3 ms |
2396 KB |
Output is correct |
37 |
Correct |
5 ms |
5980 KB |
Output is correct |
38 |
Correct |
8 ms |
8024 KB |
Output is correct |
39 |
Correct |
5 ms |
1628 KB |
Output is correct |
40 |
Correct |
5 ms |
1628 KB |
Output is correct |
41 |
Correct |
8 ms |
2972 KB |
Output is correct |
42 |
Correct |
6 ms |
7768 KB |
Output is correct |
43 |
Correct |
6 ms |
8284 KB |
Output is correct |
44 |
Correct |
8 ms |
8796 KB |
Output is correct |
45 |
Correct |
133 ms |
72844 KB |
Output is correct |
46 |
Correct |
122 ms |
72616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
3 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
3 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
6492 KB |
Output is correct |
24 |
Correct |
4 ms |
7604 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
3 ms |
7332 KB |
Output is correct |
27 |
Correct |
3 ms |
7772 KB |
Output is correct |
28 |
Correct |
4 ms |
8540 KB |
Output is correct |
29 |
Correct |
5 ms |
8540 KB |
Output is correct |
30 |
Correct |
4 ms |
8284 KB |
Output is correct |
31 |
Correct |
5 ms |
8536 KB |
Output is correct |
32 |
Correct |
4 ms |
8284 KB |
Output is correct |
33 |
Correct |
9 ms |
9540 KB |
Output is correct |
34 |
Correct |
7 ms |
9308 KB |
Output is correct |
35 |
Correct |
10 ms |
8284 KB |
Output is correct |
36 |
Correct |
3 ms |
2396 KB |
Output is correct |
37 |
Correct |
5 ms |
5980 KB |
Output is correct |
38 |
Correct |
8 ms |
8024 KB |
Output is correct |
39 |
Correct |
5 ms |
1628 KB |
Output is correct |
40 |
Correct |
5 ms |
1628 KB |
Output is correct |
41 |
Correct |
5 ms |
2908 KB |
Output is correct |
42 |
Correct |
6 ms |
7772 KB |
Output is correct |
43 |
Correct |
9 ms |
8156 KB |
Output is correct |
44 |
Correct |
6 ms |
8796 KB |
Output is correct |
45 |
Correct |
131 ms |
72816 KB |
Output is correct |
46 |
Correct |
127 ms |
73368 KB |
Output is correct |
47 |
Correct |
9 ms |
9816 KB |
Output is correct |
48 |
Correct |
5 ms |
1628 KB |
Output is correct |
49 |
Correct |
6 ms |
1772 KB |
Output is correct |
50 |
Correct |
4 ms |
1628 KB |
Output is correct |
51 |
Correct |
39 ms |
76912 KB |
Output is correct |
52 |
Correct |
47 ms |
83412 KB |
Output is correct |
53 |
Correct |
12 ms |
17500 KB |
Output is correct |
54 |
Correct |
22 ms |
61276 KB |
Output is correct |
55 |
Correct |
34 ms |
77912 KB |
Output is correct |
56 |
Correct |
44 ms |
112472 KB |
Output is correct |
57 |
Correct |
2 ms |
2652 KB |
Output is correct |
58 |
Correct |
56 ms |
116908 KB |
Output is correct |
59 |
Correct |
44 ms |
103784 KB |
Output is correct |
60 |
Correct |
44 ms |
103760 KB |
Output is correct |
61 |
Correct |
44 ms |
98696 KB |
Output is correct |
62 |
Correct |
84 ms |
112412 KB |
Output is correct |
63 |
Correct |
171 ms |
115380 KB |
Output is correct |
64 |
Correct |
214 ms |
117580 KB |
Output is correct |
65 |
Correct |
294 ms |
127052 KB |
Output is correct |
66 |
Correct |
497 ms |
175884 KB |
Output is correct |
67 |
Correct |
327 ms |
175684 KB |
Output is correct |