#include <bits/stdc++.h>
using namespace std;
const int NP=3e4+10;
const int NP1=3e4+10;
const long long inf=1e18;
long long dist[NP1];
int b[NP],p[NP];
vector<int> len[NP1];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
for(int j=0;j<n;j++)
dist[j]=inf;
for(int j=0;j<m;j++)
{
cin>>b[j]>>p[j];
len[b[j]].push_back(p[j]);
}
priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> dp;
dp.push({0,b[0]});
dist[b[0]]=0;
while(dp.size())
{
auto f=(dp.top());
dp.pop();
if(f.second==b[1])
{
cout<<f.first<<'\n';
break;
}
for(auto lp:len[f.second])
{
int po=f.second+lp;
int le=1;
while(po<n)
{
if(dist[po]>f.first+le)
{
dist[po]=f.first+le;
dp.push({dist[po],po});
}
po+=lp;
le++;
}
po=f.second-lp;
le=1;
while(po>=0)
{
if(dist[po]>f.first+le)
{
dist[po]=f.first+le;
dp.push({dist[po],po});
}
po-=lp;
le++;
}
}
}
if(dist[b[1]]==inf)
cout<<-1<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1164 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1112 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1192 KB |
Output is correct |
26 |
Correct |
1 ms |
1192 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1500 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
1 ms |
1436 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1628 KB |
Output is correct |
34 |
Correct |
2 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1112 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
3 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1500 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
1 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1628 KB |
Output is correct |
34 |
Correct |
3 ms |
1504 KB |
Output is correct |
35 |
Correct |
5 ms |
1880 KB |
Output is correct |
36 |
Correct |
1 ms |
1112 KB |
Output is correct |
37 |
Correct |
4 ms |
1692 KB |
Output is correct |
38 |
Correct |
5 ms |
2000 KB |
Output is correct |
39 |
Correct |
5 ms |
1908 KB |
Output is correct |
40 |
Correct |
5 ms |
1948 KB |
Output is correct |
41 |
Correct |
5 ms |
1968 KB |
Output is correct |
42 |
Correct |
4 ms |
1628 KB |
Output is correct |
43 |
Correct |
4 ms |
1628 KB |
Output is correct |
44 |
Correct |
40 ms |
1628 KB |
Output is correct |
45 |
Correct |
7 ms |
2140 KB |
Output is correct |
46 |
Correct |
6 ms |
2040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1172 KB |
Output is correct |
13 |
Correct |
1 ms |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1112 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1140 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1268 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1500 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1368 KB |
Output is correct |
32 |
Correct |
1 ms |
1172 KB |
Output is correct |
33 |
Correct |
3 ms |
1628 KB |
Output is correct |
34 |
Correct |
2 ms |
1628 KB |
Output is correct |
35 |
Correct |
7 ms |
2140 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
4 ms |
1688 KB |
Output is correct |
38 |
Correct |
6 ms |
1884 KB |
Output is correct |
39 |
Correct |
5 ms |
1884 KB |
Output is correct |
40 |
Correct |
5 ms |
1884 KB |
Output is correct |
41 |
Correct |
5 ms |
1908 KB |
Output is correct |
42 |
Correct |
4 ms |
1628 KB |
Output is correct |
43 |
Correct |
4 ms |
1628 KB |
Output is correct |
44 |
Correct |
42 ms |
1868 KB |
Output is correct |
45 |
Correct |
6 ms |
2140 KB |
Output is correct |
46 |
Correct |
8 ms |
2140 KB |
Output is correct |
47 |
Correct |
7 ms |
3028 KB |
Output is correct |
48 |
Correct |
6 ms |
2140 KB |
Output is correct |
49 |
Correct |
5 ms |
2136 KB |
Output is correct |
50 |
Correct |
3 ms |
1884 KB |
Output is correct |
51 |
Correct |
12 ms |
4560 KB |
Output is correct |
52 |
Correct |
12 ms |
3540 KB |
Output is correct |
53 |
Correct |
6 ms |
2908 KB |
Output is correct |
54 |
Correct |
2 ms |
1760 KB |
Output is correct |
55 |
Correct |
2 ms |
1760 KB |
Output is correct |
56 |
Correct |
442 ms |
3296 KB |
Output is correct |
57 |
Correct |
3 ms |
2516 KB |
Output is correct |
58 |
Correct |
286 ms |
2140 KB |
Output is correct |
59 |
Correct |
6 ms |
2264 KB |
Output is correct |
60 |
Correct |
6 ms |
2260 KB |
Output is correct |
61 |
Correct |
6 ms |
2396 KB |
Output is correct |
62 |
Correct |
9 ms |
2652 KB |
Output is correct |
63 |
Correct |
28 ms |
4056 KB |
Output is correct |
64 |
Correct |
27 ms |
7372 KB |
Output is correct |
65 |
Correct |
35 ms |
6608 KB |
Output is correct |
66 |
Correct |
51 ms |
7116 KB |
Output is correct |
67 |
Correct |
41 ms |
6344 KB |
Output is correct |