#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int maxn=3e4+5;
const int B = 192;
int n,m,st,fn;
int b[maxn],p[maxn];
int dis[maxn * B];
bool vis[maxn * B],done[maxn];
vector<int>doge[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int sq = sqrt(n)+1;
for(int i = 0;i < m;i++){
cin >> b[i] >> p[i];
if(i == 0) st = b[i];
if(i == 1) fn = b[i];
doge[b[i]].push_back(p[i]);
}
for(int i = 0;i < n * sq;i++) dis[i] = 1e9;
dis[st * sq] = 0;
priority_queue<pair<int,int>>q;
q.push({0,st * sq});
while(!q.empty()){
int x = q.top().second;
q.pop();
if(vis[x]) continue;
vis[x] = 1;
int bb = x / sq;
int pw = x % sq;
// cout<<bb<<' '<<pw<<'\n';
if(!pw){
for(auto i : doge[bb]){
if(i < sq){
int to = bb * sq + i;
if(dis[to] > dis[x]){
dis[to] = dis[x];
q.push({-dis[to],to});
}
}
else {
for(int j = 1;;j++){
int to = bb - i * j;
if(to < 0) break;
if(dis[to * sq] > dis[x] + j){
dis[to * sq] = dis[x] + j;
q.push({-dis[to * sq],to * sq});
}
}
for(int j = 1;;j++){
int to = bb + i * j;
if(to >= n) break;
if(dis[to * sq] > dis[x] + j){
dis[to * sq] = dis[x] + j;
q.push({-dis[to * sq],to * sq});
}
}
}
}
}
else {
if(dis[bb * sq] > dis[x]){
dis[bb * sq] = dis[x];
q.push({-dis[bb*sq],bb*sq});
}
if(bb - pw >= 0 and dis[(bb - pw) * sq + pw] > dis[x] + 1){
dis[(bb - pw) * sq + pw] = dis[x] + 1;
q.push({-dis[(bb - pw) * sq + pw],(bb - pw) * sq + pw});
}
if(bb + pw < n and dis[(bb + pw) * sq + pw] > dis[x] + 1){
dis[(bb + pw) * sq + pw] = dis[x] + 1;
q.push({-dis[(bb + pw) * sq + pw],(bb + pw) * sq + pw});
}
}
}
if(dis[fn * sq] == 1e9) cout<<-1;
else cout<<dis[fn * sq];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
984 KB |
Output is correct |
4 |
Correct |
1 ms |
988 KB |
Output is correct |
5 |
Correct |
1 ms |
1040 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
984 KB |
Output is correct |
2 |
Correct |
1 ms |
984 KB |
Output is correct |
3 |
Correct |
1 ms |
1040 KB |
Output is correct |
4 |
Correct |
1 ms |
984 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1040 KB |
Output is correct |
13 |
Correct |
1 ms |
1044 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1036 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1044 KB |
Output is correct |
8 |
Correct |
1 ms |
1036 KB |
Output is correct |
9 |
Correct |
1 ms |
1036 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1048 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1040 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
1 ms |
1492 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1292 KB |
Output is correct |
23 |
Correct |
2 ms |
1304 KB |
Output is correct |
24 |
Correct |
2 ms |
1432 KB |
Output is correct |
25 |
Correct |
2 ms |
1564 KB |
Output is correct |
26 |
Correct |
2 ms |
1492 KB |
Output is correct |
27 |
Correct |
2 ms |
1492 KB |
Output is correct |
28 |
Correct |
2 ms |
1492 KB |
Output is correct |
29 |
Correct |
8 ms |
1492 KB |
Output is correct |
30 |
Correct |
3 ms |
1492 KB |
Output is correct |
31 |
Correct |
5 ms |
1416 KB |
Output is correct |
32 |
Correct |
3 ms |
1540 KB |
Output is correct |
33 |
Correct |
10 ms |
1608 KB |
Output is correct |
34 |
Correct |
11 ms |
1596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1032 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1040 KB |
Output is correct |
5 |
Correct |
1 ms |
1032 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1036 KB |
Output is correct |
10 |
Correct |
1 ms |
1032 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1068 KB |
Output is correct |
17 |
Correct |
3 ms |
1176 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
2 ms |
1492 KB |
Output is correct |
21 |
Correct |
1 ms |
1044 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
2 ms |
1364 KB |
Output is correct |
24 |
Correct |
2 ms |
1492 KB |
Output is correct |
25 |
Correct |
2 ms |
1492 KB |
Output is correct |
26 |
Correct |
1 ms |
1492 KB |
Output is correct |
27 |
Correct |
2 ms |
1432 KB |
Output is correct |
28 |
Correct |
2 ms |
1496 KB |
Output is correct |
29 |
Correct |
7 ms |
1496 KB |
Output is correct |
30 |
Correct |
3 ms |
1420 KB |
Output is correct |
31 |
Correct |
5 ms |
1496 KB |
Output is correct |
32 |
Correct |
2 ms |
1496 KB |
Output is correct |
33 |
Correct |
9 ms |
1496 KB |
Output is correct |
34 |
Correct |
9 ms |
1496 KB |
Output is correct |
35 |
Correct |
10 ms |
1948 KB |
Output is correct |
36 |
Correct |
3 ms |
1368 KB |
Output is correct |
37 |
Correct |
13 ms |
2052 KB |
Output is correct |
38 |
Correct |
13 ms |
2264 KB |
Output is correct |
39 |
Correct |
12 ms |
2264 KB |
Output is correct |
40 |
Correct |
13 ms |
2264 KB |
Output is correct |
41 |
Correct |
12 ms |
2292 KB |
Output is correct |
42 |
Correct |
6 ms |
2120 KB |
Output is correct |
43 |
Correct |
5 ms |
2080 KB |
Output is correct |
44 |
Correct |
5 ms |
2072 KB |
Output is correct |
45 |
Correct |
15 ms |
2208 KB |
Output is correct |
46 |
Correct |
14 ms |
2256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
992 KB |
Output is correct |
2 |
Correct |
1 ms |
992 KB |
Output is correct |
3 |
Correct |
1 ms |
992 KB |
Output is correct |
4 |
Correct |
2 ms |
1048 KB |
Output is correct |
5 |
Correct |
1 ms |
1052 KB |
Output is correct |
6 |
Correct |
1 ms |
992 KB |
Output is correct |
7 |
Correct |
1 ms |
984 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
984 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1060 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1044 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
2 ms |
1560 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
2 ms |
1304 KB |
Output is correct |
24 |
Correct |
2 ms |
1492 KB |
Output is correct |
25 |
Correct |
2 ms |
1492 KB |
Output is correct |
26 |
Correct |
2 ms |
1492 KB |
Output is correct |
27 |
Correct |
2 ms |
1492 KB |
Output is correct |
28 |
Correct |
2 ms |
1492 KB |
Output is correct |
29 |
Correct |
7 ms |
1492 KB |
Output is correct |
30 |
Correct |
3 ms |
1420 KB |
Output is correct |
31 |
Correct |
5 ms |
1424 KB |
Output is correct |
32 |
Correct |
3 ms |
1492 KB |
Output is correct |
33 |
Correct |
9 ms |
1492 KB |
Output is correct |
34 |
Correct |
10 ms |
1600 KB |
Output is correct |
35 |
Correct |
10 ms |
1976 KB |
Output is correct |
36 |
Correct |
3 ms |
1308 KB |
Output is correct |
37 |
Correct |
12 ms |
1940 KB |
Output is correct |
38 |
Correct |
17 ms |
2260 KB |
Output is correct |
39 |
Correct |
12 ms |
2260 KB |
Output is correct |
40 |
Correct |
13 ms |
2212 KB |
Output is correct |
41 |
Correct |
13 ms |
2280 KB |
Output is correct |
42 |
Correct |
5 ms |
2072 KB |
Output is correct |
43 |
Correct |
5 ms |
2116 KB |
Output is correct |
44 |
Correct |
6 ms |
2132 KB |
Output is correct |
45 |
Correct |
14 ms |
2212 KB |
Output is correct |
46 |
Correct |
15 ms |
2132 KB |
Output is correct |
47 |
Correct |
71 ms |
8912 KB |
Output is correct |
48 |
Correct |
14 ms |
15820 KB |
Output is correct |
49 |
Correct |
14 ms |
17700 KB |
Output is correct |
50 |
Correct |
15 ms |
19940 KB |
Output is correct |
51 |
Correct |
61 ms |
28012 KB |
Output is correct |
52 |
Correct |
74 ms |
28224 KB |
Output is correct |
53 |
Correct |
29 ms |
27696 KB |
Output is correct |
54 |
Correct |
15 ms |
26552 KB |
Output is correct |
55 |
Correct |
18 ms |
26512 KB |
Output is correct |
56 |
Correct |
25 ms |
27876 KB |
Output is correct |
57 |
Correct |
78 ms |
26620 KB |
Output is correct |
58 |
Correct |
22 ms |
27092 KB |
Output is correct |
59 |
Correct |
26 ms |
27220 KB |
Output is correct |
60 |
Correct |
29 ms |
27160 KB |
Output is correct |
61 |
Correct |
25 ms |
27252 KB |
Output is correct |
62 |
Correct |
58 ms |
28024 KB |
Output is correct |
63 |
Correct |
55 ms |
27852 KB |
Output is correct |
64 |
Correct |
57 ms |
27716 KB |
Output is correct |
65 |
Correct |
239 ms |
27884 KB |
Output is correct |
66 |
Correct |
755 ms |
27964 KB |
Output is correct |
67 |
Correct |
750 ms |
28164 KB |
Output is correct |