#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define MOD 1000000007
typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
const ll INF=LLONG_MAX;
const int mxn=3e5+5;
bool DEBUG=0;
int n,m,start,target;
set<int>jumps[mxn];
void solve(){
priority_queue<ii,vector<ii>,greater<ii>>pq;
pq.push({0,start});
vi dist(n,INT_MAX);
while(!pq.empty()){
ii now = pq.top();
pq.pop();
int cost = now.fi;
int pos = now.se;
if(cost>dist[pos])continue;
dist[pos]=cost;
if(pos==target){
cout << cost << endl;
return;
}
//to the left
for(int jmp:jumps[pos]){
for(int cnt=1,cur=pos-jmp; cur>=0; cur-=jmp,cnt++){
if(dist[cur]>dist[pos]+cnt){
dist[cur]=dist[pos]+cnt;
pq.push({dist[cur],cur});
if(jumps[cur].count(jmp))break;
}
}
}
// to the right
for(int jmp:jumps[pos]){
for(int cnt=1,cur=pos+jmp; cur<n; cur+=jmp,cnt++){
if(dist[cur]>dist[pos]+cnt){
dist[cur]=dist[pos]+cnt;
pq.push({dist[cur],cur});
if(jumps[cur].count(jmp))break;
}
}
}
}
cout << -1 << endl;
return;
}
int main()
{
if(!DEBUG){ios_base::sync_with_stdio(0); cin.tie(0);}
//freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
cin >> n >> m;
for(int i=0; i<m; i++){
int pos, jmp;
cin >> pos >> jmp;
jumps[pos].insert(jmp);
if(i==0)start=pos;
if(i==1)target=pos;
}
solve();
}
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14464 KB |
Output is correct |
2 |
Correct |
8 ms |
14464 KB |
Output is correct |
3 |
Correct |
9 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14464 KB |
Output is correct |
5 |
Correct |
8 ms |
14464 KB |
Output is correct |
6 |
Correct |
8 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14464 KB |
Output is correct |
2 |
Correct |
8 ms |
14452 KB |
Output is correct |
3 |
Correct |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
9 ms |
14464 KB |
Output is correct |
5 |
Correct |
8 ms |
14464 KB |
Output is correct |
6 |
Correct |
8 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14464 KB |
Output is correct |
9 |
Correct |
8 ms |
14464 KB |
Output is correct |
10 |
Correct |
9 ms |
14436 KB |
Output is correct |
11 |
Correct |
9 ms |
14464 KB |
Output is correct |
12 |
Correct |
8 ms |
14464 KB |
Output is correct |
13 |
Correct |
8 ms |
14464 KB |
Output is correct |
14 |
Correct |
12 ms |
14592 KB |
Output is correct |
15 |
Correct |
9 ms |
14592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
8 ms |
14464 KB |
Output is correct |
3 |
Correct |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14464 KB |
Output is correct |
5 |
Correct |
8 ms |
14464 KB |
Output is correct |
6 |
Correct |
8 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14464 KB |
Output is correct |
9 |
Correct |
8 ms |
14464 KB |
Output is correct |
10 |
Correct |
8 ms |
14464 KB |
Output is correct |
11 |
Correct |
9 ms |
14464 KB |
Output is correct |
12 |
Correct |
9 ms |
14464 KB |
Output is correct |
13 |
Correct |
8 ms |
14464 KB |
Output is correct |
14 |
Correct |
9 ms |
14592 KB |
Output is correct |
15 |
Correct |
9 ms |
14592 KB |
Output is correct |
16 |
Correct |
8 ms |
14464 KB |
Output is correct |
17 |
Correct |
9 ms |
14592 KB |
Output is correct |
18 |
Correct |
8 ms |
14464 KB |
Output is correct |
19 |
Correct |
8 ms |
14464 KB |
Output is correct |
20 |
Correct |
10 ms |
14592 KB |
Output is correct |
21 |
Correct |
9 ms |
14464 KB |
Output is correct |
22 |
Correct |
9 ms |
14464 KB |
Output is correct |
23 |
Correct |
9 ms |
14464 KB |
Output is correct |
24 |
Correct |
9 ms |
14592 KB |
Output is correct |
25 |
Correct |
9 ms |
14592 KB |
Output is correct |
26 |
Correct |
9 ms |
14464 KB |
Output is correct |
27 |
Correct |
9 ms |
14464 KB |
Output is correct |
28 |
Correct |
9 ms |
14592 KB |
Output is correct |
29 |
Correct |
11 ms |
14720 KB |
Output is correct |
30 |
Correct |
9 ms |
14592 KB |
Output is correct |
31 |
Correct |
10 ms |
14592 KB |
Output is correct |
32 |
Correct |
9 ms |
14592 KB |
Output is correct |
33 |
Correct |
11 ms |
14976 KB |
Output is correct |
34 |
Correct |
11 ms |
14976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14464 KB |
Output is correct |
5 |
Correct |
8 ms |
14464 KB |
Output is correct |
6 |
Correct |
8 ms |
14464 KB |
Output is correct |
7 |
Correct |
9 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14464 KB |
Output is correct |
9 |
Correct |
9 ms |
14464 KB |
Output is correct |
10 |
Correct |
8 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
8 ms |
14464 KB |
Output is correct |
13 |
Correct |
8 ms |
14464 KB |
Output is correct |
14 |
Correct |
8 ms |
14592 KB |
Output is correct |
15 |
Correct |
9 ms |
14592 KB |
Output is correct |
16 |
Correct |
9 ms |
14464 KB |
Output is correct |
17 |
Correct |
9 ms |
14592 KB |
Output is correct |
18 |
Correct |
8 ms |
14464 KB |
Output is correct |
19 |
Correct |
8 ms |
14464 KB |
Output is correct |
20 |
Correct |
10 ms |
14592 KB |
Output is correct |
21 |
Correct |
8 ms |
14464 KB |
Output is correct |
22 |
Correct |
9 ms |
14464 KB |
Output is correct |
23 |
Correct |
9 ms |
14464 KB |
Output is correct |
24 |
Correct |
9 ms |
14592 KB |
Output is correct |
25 |
Correct |
9 ms |
14592 KB |
Output is correct |
26 |
Correct |
9 ms |
14464 KB |
Output is correct |
27 |
Correct |
9 ms |
14464 KB |
Output is correct |
28 |
Correct |
9 ms |
14592 KB |
Output is correct |
29 |
Correct |
10 ms |
14720 KB |
Output is correct |
30 |
Correct |
12 ms |
14592 KB |
Output is correct |
31 |
Correct |
9 ms |
14624 KB |
Output is correct |
32 |
Correct |
10 ms |
14592 KB |
Output is correct |
33 |
Correct |
12 ms |
14976 KB |
Output is correct |
34 |
Correct |
11 ms |
14976 KB |
Output is correct |
35 |
Correct |
20 ms |
15872 KB |
Output is correct |
36 |
Correct |
10 ms |
14720 KB |
Output is correct |
37 |
Correct |
16 ms |
15616 KB |
Output is correct |
38 |
Correct |
21 ms |
16256 KB |
Output is correct |
39 |
Correct |
20 ms |
16128 KB |
Output is correct |
40 |
Correct |
20 ms |
16128 KB |
Output is correct |
41 |
Correct |
21 ms |
16256 KB |
Output is correct |
42 |
Correct |
13 ms |
14720 KB |
Output is correct |
43 |
Correct |
13 ms |
14720 KB |
Output is correct |
44 |
Correct |
15 ms |
14720 KB |
Output is correct |
45 |
Correct |
27 ms |
16384 KB |
Output is correct |
46 |
Correct |
26 ms |
16384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
8 ms |
14464 KB |
Output is correct |
3 |
Correct |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
8 ms |
14464 KB |
Output is correct |
6 |
Correct |
8 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14464 KB |
Output is correct |
9 |
Correct |
8 ms |
14464 KB |
Output is correct |
10 |
Correct |
8 ms |
14464 KB |
Output is correct |
11 |
Correct |
8 ms |
14464 KB |
Output is correct |
12 |
Correct |
8 ms |
14464 KB |
Output is correct |
13 |
Correct |
9 ms |
14464 KB |
Output is correct |
14 |
Correct |
9 ms |
14592 KB |
Output is correct |
15 |
Correct |
9 ms |
14592 KB |
Output is correct |
16 |
Correct |
8 ms |
14464 KB |
Output is correct |
17 |
Correct |
9 ms |
14592 KB |
Output is correct |
18 |
Correct |
9 ms |
14464 KB |
Output is correct |
19 |
Correct |
8 ms |
14464 KB |
Output is correct |
20 |
Correct |
11 ms |
14592 KB |
Output is correct |
21 |
Correct |
9 ms |
14464 KB |
Output is correct |
22 |
Correct |
10 ms |
14464 KB |
Output is correct |
23 |
Correct |
9 ms |
14464 KB |
Output is correct |
24 |
Correct |
9 ms |
14592 KB |
Output is correct |
25 |
Correct |
9 ms |
14592 KB |
Output is correct |
26 |
Correct |
10 ms |
14464 KB |
Output is correct |
27 |
Correct |
9 ms |
14464 KB |
Output is correct |
28 |
Correct |
9 ms |
14592 KB |
Output is correct |
29 |
Correct |
11 ms |
14720 KB |
Output is correct |
30 |
Correct |
9 ms |
14592 KB |
Output is correct |
31 |
Correct |
12 ms |
14592 KB |
Output is correct |
32 |
Correct |
9 ms |
14592 KB |
Output is correct |
33 |
Correct |
12 ms |
14976 KB |
Output is correct |
34 |
Correct |
10 ms |
14880 KB |
Output is correct |
35 |
Correct |
19 ms |
15872 KB |
Output is correct |
36 |
Correct |
10 ms |
14720 KB |
Output is correct |
37 |
Correct |
17 ms |
15616 KB |
Output is correct |
38 |
Correct |
21 ms |
16128 KB |
Output is correct |
39 |
Correct |
20 ms |
16128 KB |
Output is correct |
40 |
Correct |
20 ms |
16128 KB |
Output is correct |
41 |
Correct |
22 ms |
16276 KB |
Output is correct |
42 |
Correct |
13 ms |
14592 KB |
Output is correct |
43 |
Correct |
15 ms |
14720 KB |
Output is correct |
44 |
Correct |
15 ms |
14720 KB |
Output is correct |
45 |
Correct |
26 ms |
16512 KB |
Output is correct |
46 |
Correct |
24 ms |
16512 KB |
Output is correct |
47 |
Correct |
21 ms |
16508 KB |
Output is correct |
48 |
Correct |
17 ms |
15744 KB |
Output is correct |
49 |
Correct |
18 ms |
15744 KB |
Output is correct |
50 |
Correct |
13 ms |
15360 KB |
Output is correct |
51 |
Correct |
30 ms |
16892 KB |
Output is correct |
52 |
Correct |
28 ms |
16892 KB |
Output is correct |
53 |
Correct |
21 ms |
16512 KB |
Output is correct |
54 |
Correct |
10 ms |
14848 KB |
Output is correct |
55 |
Correct |
10 ms |
14856 KB |
Output is correct |
56 |
Correct |
380 ms |
16128 KB |
Output is correct |
57 |
Correct |
11 ms |
15104 KB |
Output is correct |
58 |
Correct |
15 ms |
14976 KB |
Output is correct |
59 |
Correct |
17 ms |
14976 KB |
Output is correct |
60 |
Correct |
16 ms |
14976 KB |
Output is correct |
61 |
Correct |
17 ms |
15232 KB |
Output is correct |
62 |
Correct |
22 ms |
16256 KB |
Output is correct |
63 |
Correct |
50 ms |
18420 KB |
Output is correct |
64 |
Correct |
53 ms |
18420 KB |
Output is correct |
65 |
Correct |
60 ms |
18420 KB |
Output is correct |
66 |
Correct |
117 ms |
20464 KB |
Output is correct |
67 |
Correct |
71 ms |
20456 KB |
Output is correct |