#include <bits/stdc++.h>
#define fi first
#define se second
#define inf 0x3f3f3f3f
using namespace std;
using ll = long long;
const int nx=3e4+5;
const int sq=175;
int n, m, d[nx][sq], b[nx], p[nx], ans=inf;
vector<int> city[nx];
struct dak
{
int i, w, p;
bool operator <(const dak &o)
const
{
return w>o.w;
}
};
priority_queue<dak> f;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i = 0; i < m; i++)
{
cin>>b[i]>>p[i];
city[b[i]].emplace_back(i);
}
memset(d, 0x3f, sizeof(d));
d[b[0]][0]=0;
f.push({b[0], 0, 0});
while(f.size())
{
dak v=f.top();
f.pop();
int u=v.i, w=v.w, pw=v.p;
if(u==b[1])
ans=min(ans, w);
if(w!=d[u][pw])
continue;
if(!pw)
{
for(int i:city[u])
{
if(p[i]<sq)
{
if(d[u][p[i]]>w)
{
d[u][p[i]]=w;
f.push({u, w, p[i]});
}
}
else
{
for(int j = u%p[i]; j < n; j+=p[i])
{
if(d[j][0]>w+abs(u-j)/p[i])
{
d[j][0]=w+abs(u-j)/p[i];
f.push({j, d[j][0], 0});
}
}
}
}
}
else
{
if(u+pw<n && d[u+pw][pw]>w+1)
{
d[u+pw][pw]=w+1;
f.push({u+pw, w+1, pw});
}
if(u-pw>=0 && d[u-pw][pw]>w+1)
{
d[u-pw][pw]=w+1;
f.push({u-pw, w+1, pw});
}
if(d[u][0]>w)
{
d[u][0]=w;
f.push({u, w, 0});
}
}
}
if(ans==inf)
ans=-1;
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21848 KB |
Output is correct |
2 |
Correct |
4 ms |
21848 KB |
Output is correct |
3 |
Correct |
3 ms |
21848 KB |
Output is correct |
4 |
Correct |
3 ms |
21848 KB |
Output is correct |
5 |
Correct |
3 ms |
21848 KB |
Output is correct |
6 |
Correct |
3 ms |
21848 KB |
Output is correct |
7 |
Correct |
3 ms |
22048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21848 KB |
Output is correct |
2 |
Correct |
3 ms |
21848 KB |
Output is correct |
3 |
Correct |
3 ms |
21848 KB |
Output is correct |
4 |
Correct |
3 ms |
21848 KB |
Output is correct |
5 |
Correct |
3 ms |
21848 KB |
Output is correct |
6 |
Correct |
3 ms |
21848 KB |
Output is correct |
7 |
Correct |
4 ms |
21852 KB |
Output is correct |
8 |
Correct |
4 ms |
21848 KB |
Output is correct |
9 |
Correct |
3 ms |
21848 KB |
Output is correct |
10 |
Correct |
3 ms |
21848 KB |
Output is correct |
11 |
Correct |
3 ms |
21852 KB |
Output is correct |
12 |
Correct |
3 ms |
21852 KB |
Output is correct |
13 |
Correct |
4 ms |
21848 KB |
Output is correct |
14 |
Correct |
4 ms |
21848 KB |
Output is correct |
15 |
Correct |
4 ms |
21852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21848 KB |
Output is correct |
2 |
Correct |
3 ms |
21852 KB |
Output is correct |
3 |
Correct |
3 ms |
21848 KB |
Output is correct |
4 |
Correct |
3 ms |
21848 KB |
Output is correct |
5 |
Correct |
3 ms |
21848 KB |
Output is correct |
6 |
Correct |
3 ms |
21848 KB |
Output is correct |
7 |
Correct |
3 ms |
21848 KB |
Output is correct |
8 |
Correct |
3 ms |
21852 KB |
Output is correct |
9 |
Correct |
3 ms |
21848 KB |
Output is correct |
10 |
Correct |
3 ms |
21848 KB |
Output is correct |
11 |
Correct |
3 ms |
21848 KB |
Output is correct |
12 |
Correct |
3 ms |
21848 KB |
Output is correct |
13 |
Correct |
4 ms |
21848 KB |
Output is correct |
14 |
Correct |
4 ms |
21848 KB |
Output is correct |
15 |
Correct |
3 ms |
21852 KB |
Output is correct |
16 |
Correct |
3 ms |
21848 KB |
Output is correct |
17 |
Correct |
5 ms |
21848 KB |
Output is correct |
18 |
Correct |
3 ms |
21848 KB |
Output is correct |
19 |
Correct |
3 ms |
21848 KB |
Output is correct |
20 |
Correct |
4 ms |
21848 KB |
Output is correct |
21 |
Correct |
4 ms |
21848 KB |
Output is correct |
22 |
Correct |
3 ms |
21848 KB |
Output is correct |
23 |
Correct |
4 ms |
21848 KB |
Output is correct |
24 |
Correct |
4 ms |
21852 KB |
Output is correct |
25 |
Correct |
4 ms |
21848 KB |
Output is correct |
26 |
Correct |
4 ms |
21848 KB |
Output is correct |
27 |
Correct |
4 ms |
21848 KB |
Output is correct |
28 |
Correct |
5 ms |
21848 KB |
Output is correct |
29 |
Correct |
6 ms |
21848 KB |
Output is correct |
30 |
Correct |
4 ms |
21848 KB |
Output is correct |
31 |
Correct |
5 ms |
21852 KB |
Output is correct |
32 |
Correct |
5 ms |
21848 KB |
Output is correct |
33 |
Correct |
10 ms |
21848 KB |
Output is correct |
34 |
Correct |
10 ms |
22008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21848 KB |
Output is correct |
2 |
Correct |
3 ms |
21848 KB |
Output is correct |
3 |
Correct |
3 ms |
21848 KB |
Output is correct |
4 |
Correct |
3 ms |
21852 KB |
Output is correct |
5 |
Correct |
3 ms |
21852 KB |
Output is correct |
6 |
Correct |
3 ms |
21848 KB |
Output is correct |
7 |
Correct |
4 ms |
21848 KB |
Output is correct |
8 |
Correct |
3 ms |
21852 KB |
Output is correct |
9 |
Correct |
3 ms |
21848 KB |
Output is correct |
10 |
Correct |
3 ms |
21848 KB |
Output is correct |
11 |
Correct |
4 ms |
21848 KB |
Output is correct |
12 |
Correct |
3 ms |
21848 KB |
Output is correct |
13 |
Correct |
3 ms |
21848 KB |
Output is correct |
14 |
Correct |
4 ms |
21848 KB |
Output is correct |
15 |
Correct |
4 ms |
21848 KB |
Output is correct |
16 |
Correct |
4 ms |
21848 KB |
Output is correct |
17 |
Correct |
5 ms |
21848 KB |
Output is correct |
18 |
Correct |
4 ms |
21852 KB |
Output is correct |
19 |
Correct |
3 ms |
21848 KB |
Output is correct |
20 |
Correct |
3 ms |
21848 KB |
Output is correct |
21 |
Correct |
3 ms |
21848 KB |
Output is correct |
22 |
Correct |
3 ms |
21852 KB |
Output is correct |
23 |
Correct |
4 ms |
21852 KB |
Output is correct |
24 |
Correct |
5 ms |
21848 KB |
Output is correct |
25 |
Correct |
4 ms |
21848 KB |
Output is correct |
26 |
Correct |
4 ms |
21852 KB |
Output is correct |
27 |
Correct |
3 ms |
21848 KB |
Output is correct |
28 |
Correct |
4 ms |
21852 KB |
Output is correct |
29 |
Correct |
6 ms |
21848 KB |
Output is correct |
30 |
Correct |
5 ms |
21848 KB |
Output is correct |
31 |
Correct |
5 ms |
21852 KB |
Output is correct |
32 |
Correct |
5 ms |
21848 KB |
Output is correct |
33 |
Correct |
10 ms |
21848 KB |
Output is correct |
34 |
Correct |
10 ms |
21848 KB |
Output is correct |
35 |
Correct |
11 ms |
22104 KB |
Output is correct |
36 |
Correct |
5 ms |
21852 KB |
Output is correct |
37 |
Correct |
14 ms |
22104 KB |
Output is correct |
38 |
Correct |
14 ms |
22104 KB |
Output is correct |
39 |
Correct |
14 ms |
22104 KB |
Output is correct |
40 |
Correct |
15 ms |
22104 KB |
Output is correct |
41 |
Correct |
16 ms |
22104 KB |
Output is correct |
42 |
Correct |
7 ms |
22108 KB |
Output is correct |
43 |
Correct |
6 ms |
22104 KB |
Output is correct |
44 |
Correct |
6 ms |
22104 KB |
Output is correct |
45 |
Correct |
29 ms |
22552 KB |
Output is correct |
46 |
Correct |
29 ms |
22552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21848 KB |
Output is correct |
2 |
Correct |
3 ms |
21852 KB |
Output is correct |
3 |
Correct |
3 ms |
21848 KB |
Output is correct |
4 |
Correct |
3 ms |
21848 KB |
Output is correct |
5 |
Correct |
3 ms |
21848 KB |
Output is correct |
6 |
Correct |
3 ms |
21848 KB |
Output is correct |
7 |
Correct |
3 ms |
21848 KB |
Output is correct |
8 |
Correct |
3 ms |
22104 KB |
Output is correct |
9 |
Correct |
4 ms |
21848 KB |
Output is correct |
10 |
Correct |
4 ms |
22104 KB |
Output is correct |
11 |
Correct |
3 ms |
21852 KB |
Output is correct |
12 |
Correct |
4 ms |
21852 KB |
Output is correct |
13 |
Correct |
3 ms |
21848 KB |
Output is correct |
14 |
Correct |
4 ms |
21848 KB |
Output is correct |
15 |
Correct |
4 ms |
21852 KB |
Output is correct |
16 |
Correct |
3 ms |
21852 KB |
Output is correct |
17 |
Correct |
4 ms |
21848 KB |
Output is correct |
18 |
Correct |
3 ms |
21848 KB |
Output is correct |
19 |
Correct |
3 ms |
21848 KB |
Output is correct |
20 |
Correct |
4 ms |
21848 KB |
Output is correct |
21 |
Correct |
3 ms |
21848 KB |
Output is correct |
22 |
Correct |
3 ms |
21848 KB |
Output is correct |
23 |
Correct |
4 ms |
21852 KB |
Output is correct |
24 |
Correct |
4 ms |
21848 KB |
Output is correct |
25 |
Correct |
4 ms |
21848 KB |
Output is correct |
26 |
Correct |
4 ms |
21848 KB |
Output is correct |
27 |
Correct |
3 ms |
21852 KB |
Output is correct |
28 |
Correct |
4 ms |
21848 KB |
Output is correct |
29 |
Correct |
6 ms |
21848 KB |
Output is correct |
30 |
Correct |
4 ms |
21848 KB |
Output is correct |
31 |
Correct |
5 ms |
21848 KB |
Output is correct |
32 |
Correct |
5 ms |
21852 KB |
Output is correct |
33 |
Correct |
10 ms |
21848 KB |
Output is correct |
34 |
Correct |
13 ms |
22360 KB |
Output is correct |
35 |
Correct |
11 ms |
22104 KB |
Output is correct |
36 |
Correct |
5 ms |
21848 KB |
Output is correct |
37 |
Correct |
15 ms |
22104 KB |
Output is correct |
38 |
Correct |
14 ms |
22104 KB |
Output is correct |
39 |
Correct |
14 ms |
22104 KB |
Output is correct |
40 |
Correct |
14 ms |
22104 KB |
Output is correct |
41 |
Correct |
14 ms |
22108 KB |
Output is correct |
42 |
Correct |
6 ms |
22104 KB |
Output is correct |
43 |
Correct |
6 ms |
22104 KB |
Output is correct |
44 |
Correct |
7 ms |
22104 KB |
Output is correct |
45 |
Correct |
29 ms |
22552 KB |
Output is correct |
46 |
Correct |
30 ms |
22552 KB |
Output is correct |
47 |
Correct |
41 ms |
22808 KB |
Output is correct |
48 |
Correct |
8 ms |
22360 KB |
Output is correct |
49 |
Correct |
7 ms |
22364 KB |
Output is correct |
50 |
Correct |
5 ms |
22104 KB |
Output is correct |
51 |
Correct |
25 ms |
23068 KB |
Output is correct |
52 |
Correct |
28 ms |
23064 KB |
Output is correct |
53 |
Correct |
12 ms |
22364 KB |
Output is correct |
54 |
Correct |
4 ms |
21848 KB |
Output is correct |
55 |
Correct |
3 ms |
21848 KB |
Output is correct |
56 |
Correct |
11 ms |
23128 KB |
Output is correct |
57 |
Correct |
39 ms |
21852 KB |
Output is correct |
58 |
Correct |
8 ms |
22104 KB |
Output is correct |
59 |
Correct |
10 ms |
22108 KB |
Output is correct |
60 |
Correct |
11 ms |
22308 KB |
Output is correct |
61 |
Correct |
8 ms |
22108 KB |
Output is correct |
62 |
Correct |
25 ms |
22876 KB |
Output is correct |
63 |
Correct |
28 ms |
23764 KB |
Output is correct |
64 |
Correct |
31 ms |
23764 KB |
Output is correct |
65 |
Correct |
125 ms |
23768 KB |
Output is correct |
66 |
Correct |
398 ms |
23760 KB |
Output is correct |
67 |
Correct |
390 ms |
23764 KB |
Output is correct |