#include<bits/stdc++.h>
using namespace std;
using ti = tuple<int,int,int>;
const int inf=1e9;
int n,m;
int dis[30005][175];
vector<int> bg[30005],sm[30005];
int b[30005],p[30005];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
int sq=sqrt(n);
for(int i=0;i<m;i++){
cin >> b[i] >> p[i];
if(p[i]<=sq)
sm[b[i]].push_back(p[i]);
else
bg[b[i]].push_back(p[i]);
}
for(int i=0;i<n;i++)
for(int j=0;j<175;j++)
dis[i][j]=inf;
priority_queue<ti,vector<ti>,greater<ti>> pq;
dis[b[0]][0]=0;
pq.push({0,b[0],0});
while(!pq.empty()){
auto [du,u,pw]=pq.top();
pq.pop();
if(du>dis[u][pw])
continue;
if(pw){
if(u-pw>=0){
if(du+1<dis[u-pw][pw]){
dis[u-pw][pw]=du+1;
pq.push({du+1,u-pw,pw});
}
if(du+1<dis[u-pw][0]){
dis[u-pw][0]=du+1;
pq.push({du+1,u-pw,0});
}
}
if(u+pw<n){
if(du+1<dis[u+pw][pw]){
dis[u+pw][pw]=du+1;
pq.push({du+1,u+pw,pw});
}
if(du+1<dis[u+pw][0]){
dis[u+pw][0]=du+1;
pq.push({du+1,u+pw,0});
}
}
}
else{
for(auto pl:bg[u]){
for(int v=u,cnt=0;v<n;v+=pl,cnt++){
if(dis[v][0]>du+cnt){
dis[v][0]=du+cnt;
pq.push({du+cnt,v,0});
}
}
for(int v=u,cnt=0;v>=0;v-=pl,cnt++){
if(dis[v][0]>du+cnt){
dis[v][0]=du+cnt;
pq.push({du+cnt,v,0});
}
}
}
for(auto pl:sm[u]){
if(dis[u][pl]>du){
dis[u][pl]=du;
pq.push({du,u,pl});
}
}
}
}
int mn=inf;
for(int i=0;i<=sq;i++)
mn=min(mn,dis[b[1]][i]);
if(mn==inf)
mn=-1;
cout << mn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1696 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
2 ms |
1748 KB |
Output is correct |
7 |
Correct |
2 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1816 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
1 ms |
1748 KB |
Output is correct |
15 |
Correct |
1 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
1 ms |
1748 KB |
Output is correct |
15 |
Correct |
1 ms |
1748 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
2280 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
2 ms |
3028 KB |
Output is correct |
20 |
Correct |
2 ms |
3156 KB |
Output is correct |
21 |
Correct |
1 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
3028 KB |
Output is correct |
25 |
Correct |
2 ms |
3156 KB |
Output is correct |
26 |
Correct |
2 ms |
3028 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
3 ms |
3156 KB |
Output is correct |
29 |
Correct |
9 ms |
3156 KB |
Output is correct |
30 |
Correct |
3 ms |
3028 KB |
Output is correct |
31 |
Correct |
6 ms |
3080 KB |
Output is correct |
32 |
Correct |
3 ms |
3156 KB |
Output is correct |
33 |
Correct |
11 ms |
3284 KB |
Output is correct |
34 |
Correct |
11 ms |
3284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
2 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1748 KB |
Output is correct |
15 |
Correct |
1 ms |
1748 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
2260 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
2 ms |
3028 KB |
Output is correct |
20 |
Correct |
2 ms |
3156 KB |
Output is correct |
21 |
Correct |
1 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2924 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
3028 KB |
Output is correct |
25 |
Correct |
3 ms |
3156 KB |
Output is correct |
26 |
Correct |
3 ms |
3156 KB |
Output is correct |
27 |
Correct |
2 ms |
3116 KB |
Output is correct |
28 |
Correct |
3 ms |
3156 KB |
Output is correct |
29 |
Correct |
8 ms |
3168 KB |
Output is correct |
30 |
Correct |
3 ms |
3028 KB |
Output is correct |
31 |
Correct |
5 ms |
3156 KB |
Output is correct |
32 |
Correct |
3 ms |
3156 KB |
Output is correct |
33 |
Correct |
12 ms |
3288 KB |
Output is correct |
34 |
Correct |
11 ms |
3288 KB |
Output is correct |
35 |
Correct |
10 ms |
3240 KB |
Output is correct |
36 |
Correct |
3 ms |
2516 KB |
Output is correct |
37 |
Correct |
13 ms |
3412 KB |
Output is correct |
38 |
Correct |
13 ms |
3668 KB |
Output is correct |
39 |
Correct |
13 ms |
3700 KB |
Output is correct |
40 |
Correct |
14 ms |
3700 KB |
Output is correct |
41 |
Correct |
17 ms |
3700 KB |
Output is correct |
42 |
Correct |
7 ms |
3540 KB |
Output is correct |
43 |
Correct |
5 ms |
3540 KB |
Output is correct |
44 |
Correct |
6 ms |
3540 KB |
Output is correct |
45 |
Correct |
14 ms |
3644 KB |
Output is correct |
46 |
Correct |
14 ms |
3736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
2 ms |
1744 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
2 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1760 KB |
Output is correct |
12 |
Correct |
1 ms |
1760 KB |
Output is correct |
13 |
Correct |
1 ms |
1760 KB |
Output is correct |
14 |
Correct |
2 ms |
1756 KB |
Output is correct |
15 |
Correct |
1 ms |
1756 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
3 ms |
2272 KB |
Output is correct |
18 |
Correct |
2 ms |
2912 KB |
Output is correct |
19 |
Correct |
2 ms |
3040 KB |
Output is correct |
20 |
Correct |
2 ms |
3168 KB |
Output is correct |
21 |
Correct |
1 ms |
2016 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
3028 KB |
Output is correct |
25 |
Correct |
2 ms |
3156 KB |
Output is correct |
26 |
Correct |
2 ms |
3156 KB |
Output is correct |
27 |
Correct |
3 ms |
3156 KB |
Output is correct |
28 |
Correct |
3 ms |
3156 KB |
Output is correct |
29 |
Correct |
7 ms |
3156 KB |
Output is correct |
30 |
Correct |
3 ms |
3040 KB |
Output is correct |
31 |
Correct |
6 ms |
3168 KB |
Output is correct |
32 |
Correct |
3 ms |
3160 KB |
Output is correct |
33 |
Correct |
12 ms |
3284 KB |
Output is correct |
34 |
Correct |
12 ms |
3288 KB |
Output is correct |
35 |
Correct |
9 ms |
3156 KB |
Output is correct |
36 |
Correct |
3 ms |
2516 KB |
Output is correct |
37 |
Correct |
11 ms |
3524 KB |
Output is correct |
38 |
Correct |
13 ms |
3668 KB |
Output is correct |
39 |
Correct |
13 ms |
3592 KB |
Output is correct |
40 |
Correct |
14 ms |
3668 KB |
Output is correct |
41 |
Correct |
13 ms |
3616 KB |
Output is correct |
42 |
Correct |
5 ms |
3548 KB |
Output is correct |
43 |
Correct |
5 ms |
3552 KB |
Output is correct |
44 |
Correct |
5 ms |
3552 KB |
Output is correct |
45 |
Correct |
14 ms |
3680 KB |
Output is correct |
46 |
Correct |
15 ms |
3720 KB |
Output is correct |
47 |
Correct |
69 ms |
11204 KB |
Output is correct |
48 |
Correct |
15 ms |
18272 KB |
Output is correct |
49 |
Correct |
16 ms |
19668 KB |
Output is correct |
50 |
Correct |
16 ms |
21300 KB |
Output is correct |
51 |
Correct |
53 ms |
23588 KB |
Output is correct |
52 |
Correct |
53 ms |
23588 KB |
Output is correct |
53 |
Correct |
24 ms |
22988 KB |
Output is correct |
54 |
Correct |
17 ms |
22228 KB |
Output is correct |
55 |
Correct |
16 ms |
22228 KB |
Output is correct |
56 |
Correct |
20 ms |
23328 KB |
Output is correct |
57 |
Correct |
74 ms |
22356 KB |
Output is correct |
58 |
Correct |
19 ms |
22620 KB |
Output is correct |
59 |
Correct |
25 ms |
22740 KB |
Output is correct |
60 |
Correct |
24 ms |
22740 KB |
Output is correct |
61 |
Correct |
27 ms |
22660 KB |
Output is correct |
62 |
Correct |
44 ms |
23428 KB |
Output is correct |
63 |
Correct |
66 ms |
24368 KB |
Output is correct |
64 |
Correct |
59 ms |
24396 KB |
Output is correct |
65 |
Correct |
258 ms |
24396 KB |
Output is correct |
66 |
Correct |
744 ms |
25852 KB |
Output is correct |
67 |
Correct |
756 ms |
25920 KB |
Output is correct |