#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi<<","<<x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define ii pair<int,int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int> >
#define mp make_pair
#define INF 100000000
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
int B[30005],P[30005];
bool vis[30005],vis2[30005][30005];
int dist[30005];
vv vec;
int main(){
boost;
int N,M;
cin>>N>>M;
vec.resize(N);
for (int i = 0; i < M; ++i){
cin>>B[i]>>P[i];
vec[B[i]].pb(P[i]);
}
vis[B[0]]=1;
queue<pair<int,ii> > q;
for (int i = 0; i < sz(vec[B[0]]); ++i){
vis2[B[0]][vec[B[0]][i]]=1;
q.push({0,{B[0],vec[B[0]][i]}});
}
while(!q.empty()){
pair<int,ii> front=q.front();
int curr=front.se.fi;
q.pop();
if(curr==B[1]){
cout<<front.fi<<"\n";
return 0;
}
int idx=curr+front.se.se;
if(idx<N){
if(!vis2[idx][front.se.se])
q.push({front.fi+1,{idx,front.se.se}}),vis2[idx][front.se.se]=1;
if(!vis[idx]){
for (int i = 0; i < sz(vec[idx]); ++i){
q.push({front.fi+1,{idx,vec[idx][i]}});
vis2[idx][vec[idx][i]]=1;
}
vis[idx]=1;
}
}
idx=curr-front.se.se;
if(idx>=0){
if(!vis2[idx][front.se.se])
q.push({front.fi+1,{idx,front.se.se}}),vis2[idx][front.se.se]=1;
if(!vis[idx]){
for (int i = 0; i < sz(vec[idx]); ++i){
q.push({front.fi+1,{idx,vec[idx][i]}});
vis2[idx][vec[idx][i]]=1;
}
vis[idx]=1;
}
}
}
cout<<-1;
cout<<"\n";
return 0;
}
//long long
//array bounds
//special cases
//binary search
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
636 KB |
Output is correct |
11 |
Correct |
3 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
3 ms |
888 KB |
Output is correct |
15 |
Correct |
3 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
3 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
764 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
3 ms |
888 KB |
Output is correct |
15 |
Correct |
3 ms |
888 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
7 ms |
3576 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
12 ms |
8568 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
10 ms |
7160 KB |
Output is correct |
24 |
Correct |
12 ms |
9208 KB |
Output is correct |
25 |
Correct |
4 ms |
2172 KB |
Output is correct |
26 |
Correct |
10 ms |
7288 KB |
Output is correct |
27 |
Correct |
11 ms |
7928 KB |
Output is correct |
28 |
Correct |
14 ms |
10744 KB |
Output is correct |
29 |
Correct |
13 ms |
8568 KB |
Output is correct |
30 |
Correct |
12 ms |
8568 KB |
Output is correct |
31 |
Correct |
12 ms |
8568 KB |
Output is correct |
32 |
Correct |
12 ms |
8696 KB |
Output is correct |
33 |
Correct |
14 ms |
8696 KB |
Output is correct |
34 |
Correct |
13 ms |
8696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
3 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
888 KB |
Output is correct |
14 |
Correct |
3 ms |
760 KB |
Output is correct |
15 |
Correct |
3 ms |
760 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
3576 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
14 ms |
8568 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
10 ms |
7288 KB |
Output is correct |
24 |
Correct |
14 ms |
9052 KB |
Output is correct |
25 |
Correct |
5 ms |
2168 KB |
Output is correct |
26 |
Correct |
12 ms |
7288 KB |
Output is correct |
27 |
Correct |
11 ms |
7928 KB |
Output is correct |
28 |
Correct |
15 ms |
10744 KB |
Output is correct |
29 |
Correct |
13 ms |
8696 KB |
Output is correct |
30 |
Correct |
12 ms |
8568 KB |
Output is correct |
31 |
Correct |
12 ms |
8568 KB |
Output is correct |
32 |
Correct |
12 ms |
8696 KB |
Output is correct |
33 |
Correct |
14 ms |
8700 KB |
Output is correct |
34 |
Correct |
13 ms |
8568 KB |
Output is correct |
35 |
Correct |
18 ms |
9464 KB |
Output is correct |
36 |
Correct |
7 ms |
3960 KB |
Output is correct |
37 |
Correct |
18 ms |
10488 KB |
Output is correct |
38 |
Correct |
26 ms |
12792 KB |
Output is correct |
39 |
Correct |
10 ms |
1656 KB |
Output is correct |
40 |
Correct |
14 ms |
4860 KB |
Output is correct |
41 |
Correct |
21 ms |
11944 KB |
Output is correct |
42 |
Correct |
15 ms |
7928 KB |
Output is correct |
43 |
Correct |
16 ms |
8568 KB |
Output is correct |
44 |
Correct |
19 ms |
9208 KB |
Output is correct |
45 |
Correct |
27 ms |
9720 KB |
Output is correct |
46 |
Correct |
22 ms |
9592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
3 ms |
788 KB |
Output is correct |
12 |
Correct |
3 ms |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
888 KB |
Output is correct |
14 |
Correct |
3 ms |
760 KB |
Output is correct |
15 |
Correct |
3 ms |
888 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
3708 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
12 ms |
8568 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
508 KB |
Output is correct |
23 |
Correct |
10 ms |
7160 KB |
Output is correct |
24 |
Correct |
13 ms |
9208 KB |
Output is correct |
25 |
Correct |
4 ms |
2168 KB |
Output is correct |
26 |
Correct |
10 ms |
7336 KB |
Output is correct |
27 |
Correct |
11 ms |
7896 KB |
Output is correct |
28 |
Correct |
14 ms |
10744 KB |
Output is correct |
29 |
Correct |
12 ms |
8568 KB |
Output is correct |
30 |
Correct |
12 ms |
8568 KB |
Output is correct |
31 |
Correct |
12 ms |
8568 KB |
Output is correct |
32 |
Correct |
12 ms |
8568 KB |
Output is correct |
33 |
Correct |
14 ms |
8696 KB |
Output is correct |
34 |
Correct |
13 ms |
8568 KB |
Output is correct |
35 |
Correct |
18 ms |
9436 KB |
Output is correct |
36 |
Correct |
7 ms |
3960 KB |
Output is correct |
37 |
Correct |
18 ms |
10488 KB |
Output is correct |
38 |
Correct |
23 ms |
12892 KB |
Output is correct |
39 |
Correct |
11 ms |
1656 KB |
Output is correct |
40 |
Correct |
14 ms |
4856 KB |
Output is correct |
41 |
Correct |
22 ms |
11816 KB |
Output is correct |
42 |
Correct |
15 ms |
7800 KB |
Output is correct |
43 |
Correct |
16 ms |
8568 KB |
Output is correct |
44 |
Correct |
17 ms |
9208 KB |
Output is correct |
45 |
Correct |
27 ms |
9848 KB |
Output is correct |
46 |
Correct |
22 ms |
9592 KB |
Output is correct |
47 |
Correct |
45 ms |
34808 KB |
Output is correct |
48 |
Correct |
10 ms |
1528 KB |
Output is correct |
49 |
Correct |
9 ms |
1528 KB |
Output is correct |
50 |
Correct |
7 ms |
1528 KB |
Output is correct |
51 |
Correct |
239 ms |
192408 KB |
Output is correct |
52 |
Correct |
262 ms |
211832 KB |
Output is correct |
53 |
Correct |
36 ms |
24184 KB |
Output is correct |
54 |
Correct |
71 ms |
62840 KB |
Output is correct |
55 |
Correct |
96 ms |
82808 KB |
Output is correct |
56 |
Correct |
149 ms |
124024 KB |
Output is correct |
57 |
Correct |
5 ms |
2868 KB |
Output is correct |
58 |
Correct |
151 ms |
123756 KB |
Output is correct |
59 |
Correct |
144 ms |
114380 KB |
Output is correct |
60 |
Correct |
143 ms |
114312 KB |
Output is correct |
61 |
Correct |
138 ms |
111380 KB |
Output is correct |
62 |
Correct |
308 ms |
225912 KB |
Output is correct |
63 |
Correct |
303 ms |
140988 KB |
Output is correct |
64 |
Correct |
296 ms |
135132 KB |
Output is correct |
65 |
Correct |
355 ms |
132984 KB |
Output is correct |
66 |
Correct |
490 ms |
130900 KB |
Output is correct |
67 |
Correct |
325 ms |
127352 KB |
Output is correct |