// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int maxn=3e4+5;
int n,m;
int b[maxn],p[maxn];
vector<int>v[maxn];
map<pair<int,int>,int>dis;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 0;i < m;i++){
cin >> b[i] >> p[i];
v[b[i]].push_back(p[i]);
}
queue<pair<int,int>>q;
q.push({b[0],p[0]});
dis[{b[0],p[0]}] = 0;
while(!q.empty()){
int idx = q.front().first;
int pi = q.front().second;
q.pop();
if(idx == b[1]) return cout<<dis[{idx,pi}],0;
int now = dis[{idx,pi}];
if(idx - pi >= 0){
auto t = dis.find({idx - pi,pi});
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[{idx - pi,pi}] = now + 1;
q.push({idx - pi,pi});
}
}
if(idx + pi < n){
auto t = dis.find({idx + pi,pi});
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[{idx + pi,pi}] = now + 1;
q.push({idx + pi,pi});
}
}
for(auto i : v[idx]){
if(idx - i >= 0){
auto t = dis.find({idx - i,i});
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[{idx - i,i}] = now + 1;
q.push({idx - i,i});
}
}
if(idx + i < n){
auto t = dis.find({idx + i,i});
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[{idx + i,i}] = now + 1;
q.push({idx + i,i});
}
}
}
}
cout<<-1;
}
# |
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 |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
1048 KB |
Output is correct |
14 |
Correct |
8 ms |
1372 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 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 |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
9 ms |
1316 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
3 ms |
1236 KB |
Output is correct |
24 |
Correct |
5 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1236 KB |
Output is correct |
27 |
Correct |
3 ms |
1236 KB |
Output is correct |
28 |
Correct |
13 ms |
2172 KB |
Output is correct |
29 |
Correct |
42 ms |
4812 KB |
Output is correct |
30 |
Correct |
9 ms |
2060 KB |
Output is correct |
31 |
Correct |
20 ms |
3060 KB |
Output is correct |
32 |
Correct |
12 ms |
2516 KB |
Output is correct |
33 |
Correct |
117 ms |
8480 KB |
Output is correct |
34 |
Correct |
54 ms |
5080 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 |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
9 ms |
1380 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
1236 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 |
1268 KB |
Output is correct |
24 |
Correct |
5 ms |
1456 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
11 ms |
2260 KB |
Output is correct |
29 |
Correct |
43 ms |
4784 KB |
Output is correct |
30 |
Correct |
10 ms |
2008 KB |
Output is correct |
31 |
Correct |
19 ms |
2968 KB |
Output is correct |
32 |
Correct |
13 ms |
2512 KB |
Output is correct |
33 |
Correct |
115 ms |
8636 KB |
Output is correct |
34 |
Correct |
50 ms |
5024 KB |
Output is correct |
35 |
Correct |
36 ms |
3268 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
14 ms |
2692 KB |
Output is correct |
38 |
Correct |
25 ms |
3380 KB |
Output is correct |
39 |
Correct |
6 ms |
1496 KB |
Output is correct |
40 |
Correct |
6 ms |
1492 KB |
Output is correct |
41 |
Correct |
7 ms |
1748 KB |
Output is correct |
42 |
Correct |
5 ms |
1492 KB |
Output is correct |
43 |
Correct |
6 ms |
1620 KB |
Output is correct |
44 |
Correct |
6 ms |
1492 KB |
Output is correct |
45 |
Execution timed out |
1097 ms |
24708 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
9 ms |
1364 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
992 KB |
Output is correct |
17 |
Correct |
4 ms |
1248 KB |
Output is correct |
18 |
Correct |
1 ms |
992 KB |
Output is correct |
19 |
Correct |
1 ms |
984 KB |
Output is correct |
20 |
Correct |
2 ms |
1240 KB |
Output is correct |
21 |
Correct |
1 ms |
984 KB |
Output is correct |
22 |
Correct |
1 ms |
984 KB |
Output is correct |
23 |
Correct |
3 ms |
1240 KB |
Output is correct |
24 |
Correct |
5 ms |
1368 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1236 KB |
Output is correct |
27 |
Correct |
3 ms |
1236 KB |
Output is correct |
28 |
Correct |
12 ms |
2172 KB |
Output is correct |
29 |
Correct |
40 ms |
4844 KB |
Output is correct |
30 |
Correct |
11 ms |
2004 KB |
Output is correct |
31 |
Correct |
19 ms |
3080 KB |
Output is correct |
32 |
Correct |
12 ms |
2412 KB |
Output is correct |
33 |
Correct |
115 ms |
8480 KB |
Output is correct |
34 |
Correct |
51 ms |
5172 KB |
Output is correct |
35 |
Correct |
37 ms |
3324 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
14 ms |
2680 KB |
Output is correct |
38 |
Correct |
24 ms |
3388 KB |
Output is correct |
39 |
Correct |
6 ms |
1492 KB |
Output is correct |
40 |
Correct |
6 ms |
1492 KB |
Output is correct |
41 |
Correct |
9 ms |
1748 KB |
Output is correct |
42 |
Correct |
6 ms |
1492 KB |
Output is correct |
43 |
Correct |
7 ms |
1620 KB |
Output is correct |
44 |
Correct |
7 ms |
1492 KB |
Output is correct |
45 |
Execution timed out |
1099 ms |
24748 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |