#include <bits/stdc++.h>
#define ST first
#define ND second
#define PB push_back
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 30 * 1000 + 10, B = 150, INF = 1e9 + 10;
int n, m;
int jump[nax], pos[nax];
vector<pi>V[nax];
bool exists[nax][B + 10];
int dist[nax][B + 10];
priority_queue<pair<int, pi>>PQ;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; ++i) {
for(int j = 0; j <= B; ++j) {
dist[i][j] = INF;
}
}
for(int i = 1; i <= m; ++i) {
cin >> pos[i] >> jump[i];
if(jump[i] > B) {
for(int j = 1; j * jump[i] + pos[i] < n; ++j) {
V[pos[i]].emplace_back(j * jump[i] + pos[i], j);
}
for(int j = -1; j * jump[i] + pos[i] >= 0; --j) {
V[pos[i]].emplace_back(j * jump[i] + pos[i], -j);
}
} else {
exists[pos[i]][jump[i]] = true;
}
}
dist[pos[1]][jump[1]] = 0;
PQ.push({0, {pos[1], jump[1]}});
while(!PQ.empty()) {
auto [x, y] = PQ.top();
PQ.pop();
if(-x > dist[y.ST][y.ND]) continue;
if(y.ST == pos[2]) {
cout << -x;
return 0;
}
if(y.ND == 0) {
for(auto nbh : V[y.ST]) {
if(dist[nbh.ST][0] > -x + nbh.ND) {
dist[nbh.ST][0] = -x + nbh.ND;
PQ.push({-dist[nbh.ST][0], {nbh.ST, 0}});
}
}
for(int i = 1; i <= B; ++i) {
if(exists[y.ST][i] && dist[y.ST][i] > -x) {
dist[y.ST][i] = -x;
PQ.push({-dist[y.ST][i], {y.ST, i}});
}
}
} else {
if(dist[y.ST][0] > -x) {
dist[y.ST][0] = -x;
PQ.push({x, {y.ST, 0}});
}
for(int d : {-y.ND, y.ND}) {
if(y.ST + d < n && y.ST + d >= 0) {
if(dist[y.ST + d][y.ND] > -x + 1) {
dist[y.ST + d][y.ND] = -x + 1;
PQ.push({-(-x + 1), {y.ST + d, y.ND}});
}
}
}
}
}
cout << "-1";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
2 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
2 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
2 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
2 ms |
972 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
2252 KB |
Output is correct |
23 |
Correct |
3 ms |
2252 KB |
Output is correct |
24 |
Correct |
4 ms |
2508 KB |
Output is correct |
25 |
Correct |
3 ms |
2252 KB |
Output is correct |
26 |
Correct |
3 ms |
2380 KB |
Output is correct |
27 |
Correct |
3 ms |
2380 KB |
Output is correct |
28 |
Correct |
4 ms |
2380 KB |
Output is correct |
29 |
Correct |
10 ms |
2332 KB |
Output is correct |
30 |
Correct |
4 ms |
2252 KB |
Output is correct |
31 |
Correct |
6 ms |
2336 KB |
Output is correct |
32 |
Correct |
5 ms |
2252 KB |
Output is correct |
33 |
Correct |
18 ms |
2252 KB |
Output is correct |
34 |
Correct |
12 ms |
2364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
2 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
2 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
2 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
3 ms |
1612 KB |
Output is correct |
18 |
Correct |
3 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1356 KB |
Output is correct |
22 |
Correct |
3 ms |
2252 KB |
Output is correct |
23 |
Correct |
3 ms |
2252 KB |
Output is correct |
24 |
Correct |
3 ms |
2508 KB |
Output is correct |
25 |
Correct |
3 ms |
2252 KB |
Output is correct |
26 |
Correct |
3 ms |
2380 KB |
Output is correct |
27 |
Correct |
3 ms |
2380 KB |
Output is correct |
28 |
Correct |
4 ms |
2380 KB |
Output is correct |
29 |
Correct |
10 ms |
2332 KB |
Output is correct |
30 |
Correct |
4 ms |
2252 KB |
Output is correct |
31 |
Correct |
6 ms |
2252 KB |
Output is correct |
32 |
Correct |
5 ms |
2252 KB |
Output is correct |
33 |
Correct |
18 ms |
2368 KB |
Output is correct |
34 |
Correct |
14 ms |
2252 KB |
Output is correct |
35 |
Correct |
13 ms |
2808 KB |
Output is correct |
36 |
Correct |
3 ms |
1868 KB |
Output is correct |
37 |
Correct |
9 ms |
3532 KB |
Output is correct |
38 |
Correct |
11 ms |
3660 KB |
Output is correct |
39 |
Correct |
10 ms |
3432 KB |
Output is correct |
40 |
Correct |
10 ms |
3552 KB |
Output is correct |
41 |
Correct |
11 ms |
3660 KB |
Output is correct |
42 |
Correct |
7 ms |
2636 KB |
Output is correct |
43 |
Correct |
8 ms |
2588 KB |
Output is correct |
44 |
Correct |
7 ms |
2764 KB |
Output is correct |
45 |
Correct |
56 ms |
4612 KB |
Output is correct |
46 |
Correct |
27 ms |
4684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
3 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2592 KB |
Output is correct |
21 |
Correct |
2 ms |
1228 KB |
Output is correct |
22 |
Correct |
2 ms |
2252 KB |
Output is correct |
23 |
Correct |
3 ms |
2252 KB |
Output is correct |
24 |
Correct |
4 ms |
2508 KB |
Output is correct |
25 |
Correct |
3 ms |
2252 KB |
Output is correct |
26 |
Correct |
3 ms |
2456 KB |
Output is correct |
27 |
Correct |
3 ms |
2380 KB |
Output is correct |
28 |
Correct |
4 ms |
2380 KB |
Output is correct |
29 |
Correct |
13 ms |
2336 KB |
Output is correct |
30 |
Correct |
4 ms |
2252 KB |
Output is correct |
31 |
Correct |
7 ms |
2320 KB |
Output is correct |
32 |
Correct |
5 ms |
2252 KB |
Output is correct |
33 |
Correct |
18 ms |
2364 KB |
Output is correct |
34 |
Correct |
13 ms |
2372 KB |
Output is correct |
35 |
Correct |
9 ms |
2776 KB |
Output is correct |
36 |
Correct |
3 ms |
1868 KB |
Output is correct |
37 |
Correct |
9 ms |
3604 KB |
Output is correct |
38 |
Correct |
12 ms |
3660 KB |
Output is correct |
39 |
Correct |
10 ms |
3520 KB |
Output is correct |
40 |
Correct |
10 ms |
3532 KB |
Output is correct |
41 |
Correct |
12 ms |
3652 KB |
Output is correct |
42 |
Correct |
8 ms |
2636 KB |
Output is correct |
43 |
Correct |
7 ms |
2624 KB |
Output is correct |
44 |
Correct |
7 ms |
2740 KB |
Output is correct |
45 |
Correct |
56 ms |
4632 KB |
Output is correct |
46 |
Correct |
26 ms |
4608 KB |
Output is correct |
47 |
Correct |
23 ms |
15072 KB |
Output is correct |
48 |
Correct |
20 ms |
17312 KB |
Output is correct |
49 |
Correct |
18 ms |
18332 KB |
Output is correct |
50 |
Correct |
19 ms |
19652 KB |
Output is correct |
51 |
Correct |
37 ms |
22652 KB |
Output is correct |
52 |
Correct |
37 ms |
22916 KB |
Output is correct |
53 |
Correct |
19 ms |
20092 KB |
Output is correct |
54 |
Correct |
15 ms |
19776 KB |
Output is correct |
55 |
Correct |
17 ms |
19788 KB |
Output is correct |
56 |
Correct |
26 ms |
24672 KB |
Output is correct |
57 |
Correct |
15 ms |
23884 KB |
Output is correct |
58 |
Correct |
28 ms |
20056 KB |
Output is correct |
59 |
Correct |
26 ms |
20044 KB |
Output is correct |
60 |
Correct |
32 ms |
20600 KB |
Output is correct |
61 |
Correct |
27 ms |
20388 KB |
Output is correct |
62 |
Correct |
54 ms |
22512 KB |
Output is correct |
63 |
Correct |
87 ms |
44416 KB |
Output is correct |
64 |
Correct |
105 ms |
52264 KB |
Output is correct |
65 |
Correct |
271 ms |
52936 KB |
Output is correct |
66 |
Correct |
862 ms |
47648 KB |
Output is correct |
67 |
Correct |
390 ms |
47996 KB |
Output is correct |