#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
vector <int> b(m),p(m);
set <int> s[n];
for(int i=0;i<m;i++){
cin>>b[i]>>p[i];
s[b[i]].insert(p[i]);
}
vector <pair <int,int> > g[n];
for(int i=0;i<n;i++){
for(auto x : s[i]){
int y=i+x,cnt=1;
if(y<n){
g[i].pb({y,1});
}
while(y+x<n){
cnt++;
if(s[y].find(x)==s[y].end()){
g[i].pb({y+x,cnt});
y+=x;
}else break;
}
y=i-x;cnt=1;
if(y>=0)g[i].pb({y,1});
while(y-x>=0){
cnt++;
if(s[y].find(x)==s[y].end()){
g[i].pb({y-x,cnt});
y-=x;
}else break;
}
}
}
set <pair <int,int> > st;
vector <int> d(n,1e12);
st.insert({0,b[0]});
d[b[0]]=0;
while(!st.empty()){
int v=st.begin()->second;
//cout<<v<<" "<<d[v]<<"\n";
st.erase(st.begin());
for(auto to : g[v]){
if(d[to.ff]>d[v]+to.ss){
st.erase({d[to.ff],to.ff});
d[to.ff]=d[v]+to.ss;
st.insert({d[to.ff],to.ff});
}
}
}
if(d[b[1]]==1e12)d[b[1]]=-1;
cout<<d[b[1]]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
548 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
628 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
3 ms |
1116 KB |
Output is correct |
29 |
Correct |
5 ms |
1628 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
1368 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
7 ms |
2780 KB |
Output is correct |
34 |
Correct |
7 ms |
2964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
3 ms |
1016 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
856 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
4 ms |
1628 KB |
Output is correct |
30 |
Correct |
2 ms |
1104 KB |
Output is correct |
31 |
Correct |
3 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1112 KB |
Output is correct |
33 |
Correct |
7 ms |
2780 KB |
Output is correct |
34 |
Correct |
7 ms |
2784 KB |
Output is correct |
35 |
Correct |
17 ms |
3936 KB |
Output is correct |
36 |
Correct |
3 ms |
1116 KB |
Output is correct |
37 |
Correct |
17 ms |
5700 KB |
Output is correct |
38 |
Correct |
21 ms |
5660 KB |
Output is correct |
39 |
Correct |
21 ms |
5720 KB |
Output is correct |
40 |
Correct |
21 ms |
5724 KB |
Output is correct |
41 |
Correct |
21 ms |
5792 KB |
Output is correct |
42 |
Correct |
9 ms |
1372 KB |
Output is correct |
43 |
Correct |
9 ms |
1368 KB |
Output is correct |
44 |
Correct |
9 ms |
1372 KB |
Output is correct |
45 |
Correct |
29 ms |
10328 KB |
Output is correct |
46 |
Correct |
26 ms |
10384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
4 ms |
856 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
872 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
6 ms |
1628 KB |
Output is correct |
30 |
Correct |
2 ms |
1104 KB |
Output is correct |
31 |
Correct |
3 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
9 ms |
2784 KB |
Output is correct |
34 |
Correct |
7 ms |
2784 KB |
Output is correct |
35 |
Correct |
16 ms |
3932 KB |
Output is correct |
36 |
Correct |
3 ms |
1116 KB |
Output is correct |
37 |
Correct |
17 ms |
5724 KB |
Output is correct |
38 |
Correct |
22 ms |
5612 KB |
Output is correct |
39 |
Correct |
22 ms |
5724 KB |
Output is correct |
40 |
Correct |
21 ms |
5672 KB |
Output is correct |
41 |
Correct |
22 ms |
5700 KB |
Output is correct |
42 |
Correct |
9 ms |
1480 KB |
Output is correct |
43 |
Correct |
9 ms |
1388 KB |
Output is correct |
44 |
Correct |
9 ms |
1372 KB |
Output is correct |
45 |
Correct |
38 ms |
10320 KB |
Output is correct |
46 |
Correct |
34 ms |
10460 KB |
Output is correct |
47 |
Correct |
59 ms |
20052 KB |
Output is correct |
48 |
Correct |
26 ms |
8152 KB |
Output is correct |
49 |
Correct |
17 ms |
6528 KB |
Output is correct |
50 |
Correct |
12 ms |
6360 KB |
Output is correct |
51 |
Correct |
63 ms |
12664 KB |
Output is correct |
52 |
Correct |
60 ms |
13300 KB |
Output is correct |
53 |
Correct |
28 ms |
8096 KB |
Output is correct |
54 |
Correct |
7 ms |
4188 KB |
Output is correct |
55 |
Correct |
7 ms |
4952 KB |
Output is correct |
56 |
Correct |
19 ms |
6048 KB |
Output is correct |
57 |
Correct |
63 ms |
19628 KB |
Output is correct |
58 |
Correct |
20 ms |
5716 KB |
Output is correct |
59 |
Correct |
20 ms |
6392 KB |
Output is correct |
60 |
Correct |
21 ms |
7636 KB |
Output is correct |
61 |
Correct |
20 ms |
6352 KB |
Output is correct |
62 |
Correct |
38 ms |
14552 KB |
Output is correct |
63 |
Correct |
118 ms |
44708 KB |
Output is correct |
64 |
Correct |
119 ms |
52048 KB |
Output is correct |
65 |
Correct |
159 ms |
70940 KB |
Output is correct |
66 |
Correct |
281 ms |
121072 KB |
Output is correct |
67 |
Correct |
285 ms |
121524 KB |
Output is correct |