#include<bits/stdc++.h>
using namespace std;
set<int>doge[30100];
int dist[30100],pos[30100];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;
int main(){
memset(dist,9,sizeof dist);
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int b;cin>>pos[i]>>b;
doge[pos[i]].insert(b);
}
dist[pos[0]]=0;
pq.push({0,pos[0]});
while(pq.size()){
auto[a,b]=pq.top();
pq.pop();
if(dist[b]!=a)continue;
for(auto d:doge[b]){
for(int i=b-d;i>=0;i-=d){
int D=a+(b-i)/d;
if(dist[i]>D&&doge[i].size())
pq.push({dist[i]=D,i});
if(doge[i].count(d))
break;
}
for(int i=b+d;i<n;i+=d){
int D=a+(i-b)/d;
if(dist[i]>D&&doge[i].size())
pq.push({dist[i]=D,i});
if(doge[i].count(d))
break;
}
}
}
cout<<(dist[pos[1]]!=dist[30099]?dist[pos[1]]:-1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2136 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
2 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1880 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1880 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
1880 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1984 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
2140 KB |
Output is correct |
21 |
Correct |
1 ms |
1880 KB |
Output is correct |
22 |
Correct |
1 ms |
1880 KB |
Output is correct |
23 |
Correct |
2 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
1880 KB |
Output is correct |
29 |
Correct |
1 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1880 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
2 ms |
1884 KB |
Output is correct |
34 |
Correct |
3 ms |
2092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
2000 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1880 KB |
Output is correct |
21 |
Correct |
1 ms |
1828 KB |
Output is correct |
22 |
Correct |
2 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
1988 KB |
Output is correct |
25 |
Correct |
2 ms |
1988 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
2 ms |
1880 KB |
Output is correct |
28 |
Correct |
2 ms |
1884 KB |
Output is correct |
29 |
Correct |
1 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
2 ms |
1884 KB |
Output is correct |
34 |
Correct |
2 ms |
1884 KB |
Output is correct |
35 |
Correct |
16 ms |
3516 KB |
Output is correct |
36 |
Correct |
3 ms |
2140 KB |
Output is correct |
37 |
Correct |
14 ms |
3212 KB |
Output is correct |
38 |
Correct |
21 ms |
3824 KB |
Output is correct |
39 |
Correct |
18 ms |
3716 KB |
Output is correct |
40 |
Correct |
19 ms |
3676 KB |
Output is correct |
41 |
Correct |
19 ms |
3676 KB |
Output is correct |
42 |
Correct |
8 ms |
2140 KB |
Output is correct |
43 |
Correct |
8 ms |
2312 KB |
Output is correct |
44 |
Correct |
8 ms |
2140 KB |
Output is correct |
45 |
Correct |
15 ms |
3732 KB |
Output is correct |
46 |
Correct |
16 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
2 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
2 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1976 KB |
Output is correct |
16 |
Correct |
1 ms |
2008 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1980 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1880 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
1984 KB |
Output is correct |
29 |
Correct |
1 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
2 ms |
1884 KB |
Output is correct |
34 |
Correct |
2 ms |
1884 KB |
Output is correct |
35 |
Correct |
15 ms |
3516 KB |
Output is correct |
36 |
Correct |
4 ms |
2140 KB |
Output is correct |
37 |
Correct |
14 ms |
3180 KB |
Output is correct |
38 |
Correct |
18 ms |
3676 KB |
Output is correct |
39 |
Correct |
19 ms |
3764 KB |
Output is correct |
40 |
Correct |
19 ms |
3676 KB |
Output is correct |
41 |
Correct |
21 ms |
3672 KB |
Output is correct |
42 |
Correct |
11 ms |
2140 KB |
Output is correct |
43 |
Correct |
11 ms |
2112 KB |
Output is correct |
44 |
Correct |
8 ms |
2140 KB |
Output is correct |
45 |
Correct |
15 ms |
3556 KB |
Output is correct |
46 |
Correct |
15 ms |
3724 KB |
Output is correct |
47 |
Correct |
27 ms |
3804 KB |
Output is correct |
48 |
Correct |
12 ms |
3164 KB |
Output is correct |
49 |
Correct |
10 ms |
3164 KB |
Output is correct |
50 |
Correct |
8 ms |
2652 KB |
Output is correct |
51 |
Correct |
28 ms |
4568 KB |
Output is correct |
52 |
Correct |
24 ms |
4316 KB |
Output is correct |
53 |
Correct |
18 ms |
4188 KB |
Output is correct |
54 |
Correct |
1 ms |
1880 KB |
Output is correct |
55 |
Correct |
1 ms |
1884 KB |
Output is correct |
56 |
Correct |
14 ms |
3700 KB |
Output is correct |
57 |
Correct |
6 ms |
1984 KB |
Output is correct |
58 |
Correct |
6 ms |
2140 KB |
Output is correct |
59 |
Correct |
8 ms |
2140 KB |
Output is correct |
60 |
Correct |
8 ms |
2136 KB |
Output is correct |
61 |
Correct |
9 ms |
2476 KB |
Output is correct |
62 |
Correct |
17 ms |
3676 KB |
Output is correct |
63 |
Correct |
21 ms |
3676 KB |
Output is correct |
64 |
Correct |
20 ms |
3712 KB |
Output is correct |
65 |
Correct |
25 ms |
3740 KB |
Output is correct |
66 |
Correct |
33 ms |
3672 KB |
Output is correct |
67 |
Correct |
32 ms |
3672 KB |
Output is correct |