#include <bits/stdc++.h>
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 3e4 + 10, M = 5e6, INF = 1e9;
map<pii, int> d;
vector<int> t[N];
bool vis[N];
deque<pii> q;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
int sp, sb, se;
for(int i=0; i<m; i++){
int p, b; cin >> p >> b;
t[p].pb(b);
if(i == 0)
sp = p, sb = b;
if(i == 1)
se = p;
}
q.push_back({sp, sb});
d[{sp, sb}] = 0;
while(!q.empty()){
int p = q.front().F, b = q.front().S;
int dis = d[{p, b}];
q.pop_front();
if(!vis[p]){
for(auto l : t[p]){
if(d.count({p, l}) == 0)
d[{p, l}] = dis, q.push_front({p, l});
}
vis[p] = true;
}
if(p + b < n){
if(d.count({p + b, b}) == 0)
d[{p + b, b}] = dis + 1, q.push_back({p + b, b});
}
if(p - b >= 0){
if(d.count({p - b, b}) == 0)
d[{p - b, b}] = dis + 1, q.push_back({p - b, b});
}
}
int mn = INF;
for(int i=0; i<=n; i++){
if(d.count({se, i}))
mn = min(mn, d[{se, i}]);
}
if(mn < INF)
cout << mn << endl;
else cout << -1 << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2916 KB |
Output is correct |
2 |
Correct |
0 ms |
2916 KB |
Output is correct |
3 |
Correct |
0 ms |
2916 KB |
Output is correct |
4 |
Correct |
0 ms |
2916 KB |
Output is correct |
5 |
Correct |
0 ms |
2916 KB |
Output is correct |
6 |
Correct |
0 ms |
2916 KB |
Output is correct |
7 |
Correct |
0 ms |
2916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2916 KB |
Output is correct |
2 |
Correct |
0 ms |
2916 KB |
Output is correct |
3 |
Correct |
0 ms |
2916 KB |
Output is correct |
4 |
Correct |
0 ms |
2916 KB |
Output is correct |
5 |
Correct |
0 ms |
2916 KB |
Output is correct |
6 |
Correct |
0 ms |
2916 KB |
Output is correct |
7 |
Correct |
0 ms |
2916 KB |
Output is correct |
8 |
Correct |
0 ms |
2916 KB |
Output is correct |
9 |
Correct |
0 ms |
2916 KB |
Output is correct |
10 |
Correct |
0 ms |
3048 KB |
Output is correct |
11 |
Correct |
0 ms |
3180 KB |
Output is correct |
12 |
Correct |
0 ms |
2916 KB |
Output is correct |
13 |
Correct |
0 ms |
2916 KB |
Output is correct |
14 |
Correct |
3 ms |
3312 KB |
Output is correct |
15 |
Correct |
3 ms |
3312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2916 KB |
Output is correct |
2 |
Correct |
0 ms |
2916 KB |
Output is correct |
3 |
Correct |
0 ms |
2916 KB |
Output is correct |
4 |
Correct |
0 ms |
2916 KB |
Output is correct |
5 |
Correct |
0 ms |
2916 KB |
Output is correct |
6 |
Correct |
0 ms |
2916 KB |
Output is correct |
7 |
Correct |
0 ms |
2916 KB |
Output is correct |
8 |
Correct |
0 ms |
2916 KB |
Output is correct |
9 |
Correct |
0 ms |
2916 KB |
Output is correct |
10 |
Correct |
0 ms |
3048 KB |
Output is correct |
11 |
Correct |
3 ms |
3180 KB |
Output is correct |
12 |
Correct |
0 ms |
2916 KB |
Output is correct |
13 |
Correct |
0 ms |
2916 KB |
Output is correct |
14 |
Correct |
3 ms |
3312 KB |
Output is correct |
15 |
Correct |
3 ms |
3312 KB |
Output is correct |
16 |
Correct |
0 ms |
2916 KB |
Output is correct |
17 |
Correct |
9 ms |
3840 KB |
Output is correct |
18 |
Correct |
0 ms |
2916 KB |
Output is correct |
19 |
Correct |
0 ms |
2916 KB |
Output is correct |
20 |
Correct |
0 ms |
3048 KB |
Output is correct |
21 |
Correct |
0 ms |
2916 KB |
Output is correct |
22 |
Correct |
0 ms |
2916 KB |
Output is correct |
23 |
Correct |
3 ms |
3312 KB |
Output is correct |
24 |
Correct |
6 ms |
3708 KB |
Output is correct |
25 |
Correct |
3 ms |
3444 KB |
Output is correct |
26 |
Correct |
3 ms |
3312 KB |
Output is correct |
27 |
Correct |
3 ms |
3312 KB |
Output is correct |
28 |
Correct |
6 ms |
4368 KB |
Output is correct |
29 |
Correct |
53 ms |
6744 KB |
Output is correct |
30 |
Correct |
9 ms |
3972 KB |
Output is correct |
31 |
Correct |
19 ms |
5028 KB |
Output is correct |
32 |
Correct |
16 ms |
4368 KB |
Output is correct |
33 |
Correct |
153 ms |
10440 KB |
Output is correct |
34 |
Correct |
146 ms |
10440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2916 KB |
Output is correct |
2 |
Correct |
0 ms |
2916 KB |
Output is correct |
3 |
Correct |
0 ms |
2916 KB |
Output is correct |
4 |
Correct |
0 ms |
2916 KB |
Output is correct |
5 |
Correct |
0 ms |
2916 KB |
Output is correct |
6 |
Correct |
0 ms |
2916 KB |
Output is correct |
7 |
Correct |
0 ms |
2916 KB |
Output is correct |
8 |
Correct |
0 ms |
2916 KB |
Output is correct |
9 |
Correct |
0 ms |
2916 KB |
Output is correct |
10 |
Correct |
0 ms |
3048 KB |
Output is correct |
11 |
Correct |
3 ms |
3180 KB |
Output is correct |
12 |
Correct |
0 ms |
2916 KB |
Output is correct |
13 |
Correct |
0 ms |
2916 KB |
Output is correct |
14 |
Correct |
3 ms |
3312 KB |
Output is correct |
15 |
Correct |
3 ms |
3312 KB |
Output is correct |
16 |
Correct |
0 ms |
2916 KB |
Output is correct |
17 |
Correct |
16 ms |
3840 KB |
Output is correct |
18 |
Correct |
0 ms |
2916 KB |
Output is correct |
19 |
Correct |
0 ms |
2916 KB |
Output is correct |
20 |
Correct |
0 ms |
3048 KB |
Output is correct |
21 |
Correct |
0 ms |
2916 KB |
Output is correct |
22 |
Correct |
0 ms |
2916 KB |
Output is correct |
23 |
Correct |
3 ms |
3312 KB |
Output is correct |
24 |
Correct |
6 ms |
3708 KB |
Output is correct |
25 |
Correct |
6 ms |
3444 KB |
Output is correct |
26 |
Correct |
6 ms |
3312 KB |
Output is correct |
27 |
Correct |
6 ms |
3312 KB |
Output is correct |
28 |
Correct |
19 ms |
4368 KB |
Output is correct |
29 |
Correct |
79 ms |
6744 KB |
Output is correct |
30 |
Correct |
6 ms |
3972 KB |
Output is correct |
31 |
Correct |
19 ms |
5028 KB |
Output is correct |
32 |
Correct |
16 ms |
4368 KB |
Output is correct |
33 |
Correct |
159 ms |
10440 KB |
Output is correct |
34 |
Correct |
179 ms |
10440 KB |
Output is correct |
35 |
Correct |
153 ms |
9516 KB |
Output is correct |
36 |
Correct |
19 ms |
4104 KB |
Output is correct |
37 |
Correct |
386 ms |
13872 KB |
Output is correct |
38 |
Correct |
316 ms |
13216 KB |
Output is correct |
39 |
Correct |
343 ms |
13212 KB |
Output is correct |
40 |
Correct |
326 ms |
13216 KB |
Output is correct |
41 |
Correct |
296 ms |
13208 KB |
Output is correct |
42 |
Correct |
6 ms |
3476 KB |
Output is correct |
43 |
Correct |
9 ms |
3476 KB |
Output is correct |
44 |
Correct |
6 ms |
3208 KB |
Output is correct |
45 |
Execution timed out |
1000 ms |
33276 KB |
Execution timed out |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2916 KB |
Output is correct |
2 |
Correct |
0 ms |
2916 KB |
Output is correct |
3 |
Correct |
0 ms |
2916 KB |
Output is correct |
4 |
Correct |
0 ms |
2916 KB |
Output is correct |
5 |
Correct |
0 ms |
2916 KB |
Output is correct |
6 |
Correct |
0 ms |
2916 KB |
Output is correct |
7 |
Correct |
0 ms |
2916 KB |
Output is correct |
8 |
Correct |
0 ms |
2916 KB |
Output is correct |
9 |
Correct |
0 ms |
2916 KB |
Output is correct |
10 |
Correct |
0 ms |
3048 KB |
Output is correct |
11 |
Correct |
3 ms |
3180 KB |
Output is correct |
12 |
Correct |
0 ms |
2916 KB |
Output is correct |
13 |
Correct |
0 ms |
2916 KB |
Output is correct |
14 |
Correct |
3 ms |
3312 KB |
Output is correct |
15 |
Correct |
3 ms |
3312 KB |
Output is correct |
16 |
Correct |
0 ms |
2916 KB |
Output is correct |
17 |
Correct |
13 ms |
3840 KB |
Output is correct |
18 |
Correct |
0 ms |
2916 KB |
Output is correct |
19 |
Correct |
0 ms |
2916 KB |
Output is correct |
20 |
Correct |
0 ms |
3048 KB |
Output is correct |
21 |
Correct |
0 ms |
2916 KB |
Output is correct |
22 |
Correct |
0 ms |
2916 KB |
Output is correct |
23 |
Correct |
3 ms |
3312 KB |
Output is correct |
24 |
Correct |
6 ms |
3708 KB |
Output is correct |
25 |
Correct |
6 ms |
3444 KB |
Output is correct |
26 |
Correct |
6 ms |
3312 KB |
Output is correct |
27 |
Correct |
3 ms |
3312 KB |
Output is correct |
28 |
Correct |
23 ms |
4368 KB |
Output is correct |
29 |
Correct |
66 ms |
6744 KB |
Output is correct |
30 |
Correct |
13 ms |
3972 KB |
Output is correct |
31 |
Correct |
26 ms |
5028 KB |
Output is correct |
32 |
Correct |
19 ms |
4368 KB |
Output is correct |
33 |
Correct |
186 ms |
10440 KB |
Output is correct |
34 |
Correct |
169 ms |
10440 KB |
Output is correct |
35 |
Correct |
149 ms |
9516 KB |
Output is correct |
36 |
Correct |
16 ms |
4104 KB |
Output is correct |
37 |
Correct |
399 ms |
13872 KB |
Output is correct |
38 |
Correct |
336 ms |
13216 KB |
Output is correct |
39 |
Correct |
346 ms |
13212 KB |
Output is correct |
40 |
Correct |
299 ms |
13216 KB |
Output is correct |
41 |
Correct |
343 ms |
13208 KB |
Output is correct |
42 |
Correct |
6 ms |
3476 KB |
Output is correct |
43 |
Correct |
9 ms |
3476 KB |
Output is correct |
44 |
Correct |
3 ms |
3208 KB |
Output is correct |
45 |
Execution timed out |
1000 ms |
33276 KB |
Execution timed out |
46 |
Halted |
0 ms |
0 KB |
- |