#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];
set<pair<int,int>>vis;
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]);
}
priority_queue<tuple<int,int,int>>q;
q.push({0,b[0],p[0]});
dis[{b[0],p[0]}] = 0;
while(!q.empty()){
int idx = get<1>(q.top());
int pi = get<2>(q.top());
q.pop();
if(vis.find({idx,pi}) != vis.end()) continue;
vis.insert({idx,pi});
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({-now - 1,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({-now - 1,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({-now - 1,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({-now - 1,idx + i,i});
}
}
}
}
int ans = 1e9;
for(auto i : dis){
if(i.first.first == b[1]){
ans = min(ans,i.second);
}
}
if(ans == 1e9) cout<<-1;
else cout<<ans;
}
# |
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 |
1032 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 |
1036 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 |
1036 KB |
Output is correct |
9 |
Correct |
1 ms |
1036 KB |
Output is correct |
10 |
Correct |
3 ms |
1160 KB |
Output is correct |
11 |
Correct |
10 ms |
1492 KB |
Output is correct |
12 |
Correct |
3 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
11 ms |
1748 KB |
Output is correct |
15 |
Correct |
11 ms |
1736 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 |
1036 KB |
Output is correct |
6 |
Correct |
1 ms |
1068 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 |
3 ms |
1168 KB |
Output is correct |
11 |
Correct |
8 ms |
1492 KB |
Output is correct |
12 |
Correct |
3 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
15 ms |
1636 KB |
Output is correct |
15 |
Correct |
11 ms |
1684 KB |
Output is correct |
16 |
Correct |
1 ms |
1040 KB |
Output is correct |
17 |
Correct |
28 ms |
2628 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
2 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1304 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
1040 KB |
Output is correct |
23 |
Correct |
7 ms |
1620 KB |
Output is correct |
24 |
Correct |
15 ms |
2324 KB |
Output is correct |
25 |
Correct |
8 ms |
1872 KB |
Output is correct |
26 |
Correct |
7 ms |
1876 KB |
Output is correct |
27 |
Correct |
16 ms |
1748 KB |
Output is correct |
28 |
Correct |
23 ms |
3504 KB |
Output is correct |
29 |
Correct |
77 ms |
7656 KB |
Output is correct |
30 |
Correct |
15 ms |
2900 KB |
Output is correct |
31 |
Correct |
33 ms |
4656 KB |
Output is correct |
32 |
Correct |
23 ms |
3532 KB |
Output is correct |
33 |
Correct |
173 ms |
14212 KB |
Output is correct |
34 |
Correct |
148 ms |
14236 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 |
3 ms |
1108 KB |
Output is correct |
11 |
Correct |
9 ms |
1492 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
11 ms |
1748 KB |
Output is correct |
15 |
Correct |
12 ms |
1748 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
24 ms |
2664 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1236 KB |
Output is correct |
21 |
Correct |
2 ms |
1044 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
7 ms |
1716 KB |
Output is correct |
24 |
Correct |
18 ms |
2344 KB |
Output is correct |
25 |
Correct |
8 ms |
1816 KB |
Output is correct |
26 |
Correct |
7 ms |
1900 KB |
Output is correct |
27 |
Correct |
16 ms |
1684 KB |
Output is correct |
28 |
Correct |
26 ms |
3508 KB |
Output is correct |
29 |
Correct |
65 ms |
7628 KB |
Output is correct |
30 |
Correct |
17 ms |
2828 KB |
Output is correct |
31 |
Correct |
31 ms |
4648 KB |
Output is correct |
32 |
Correct |
25 ms |
3544 KB |
Output is correct |
33 |
Correct |
164 ms |
14208 KB |
Output is correct |
34 |
Correct |
175 ms |
14256 KB |
Output is correct |
35 |
Correct |
385 ms |
11788 KB |
Output is correct |
36 |
Correct |
23 ms |
2764 KB |
Output is correct |
37 |
Correct |
836 ms |
20796 KB |
Output is correct |
38 |
Correct |
712 ms |
18928 KB |
Output is correct |
39 |
Correct |
737 ms |
18924 KB |
Output is correct |
40 |
Correct |
682 ms |
18996 KB |
Output is correct |
41 |
Correct |
728 ms |
18820 KB |
Output is correct |
42 |
Correct |
23 ms |
2380 KB |
Output is correct |
43 |
Correct |
151 ms |
2196 KB |
Output is correct |
44 |
Correct |
7 ms |
1748 KB |
Output is correct |
45 |
Execution timed out |
1089 ms |
44508 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 |
1028 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 |
3 ms |
1108 KB |
Output is correct |
11 |
Correct |
8 ms |
1424 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
11 ms |
1680 KB |
Output is correct |
15 |
Correct |
11 ms |
1684 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
21 ms |
2712 KB |
Output is correct |
18 |
Correct |
1 ms |
1040 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
2 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 |
7 ms |
1620 KB |
Output is correct |
24 |
Correct |
15 ms |
2420 KB |
Output is correct |
25 |
Correct |
8 ms |
1848 KB |
Output is correct |
26 |
Correct |
7 ms |
1876 KB |
Output is correct |
27 |
Correct |
14 ms |
1748 KB |
Output is correct |
28 |
Correct |
20 ms |
3480 KB |
Output is correct |
29 |
Correct |
60 ms |
7704 KB |
Output is correct |
30 |
Correct |
16 ms |
2900 KB |
Output is correct |
31 |
Correct |
33 ms |
4628 KB |
Output is correct |
32 |
Correct |
23 ms |
3604 KB |
Output is correct |
33 |
Correct |
147 ms |
14220 KB |
Output is correct |
34 |
Correct |
159 ms |
14260 KB |
Output is correct |
35 |
Correct |
344 ms |
11700 KB |
Output is correct |
36 |
Correct |
24 ms |
2764 KB |
Output is correct |
37 |
Correct |
794 ms |
20804 KB |
Output is correct |
38 |
Correct |
744 ms |
19016 KB |
Output is correct |
39 |
Correct |
734 ms |
18808 KB |
Output is correct |
40 |
Correct |
698 ms |
18920 KB |
Output is correct |
41 |
Correct |
722 ms |
18920 KB |
Output is correct |
42 |
Correct |
21 ms |
2416 KB |
Output is correct |
43 |
Correct |
157 ms |
2216 KB |
Output is correct |
44 |
Correct |
7 ms |
1748 KB |
Output is correct |
45 |
Execution timed out |
1092 ms |
45140 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |