/*
5 3
0 2
1 1
4 1
*/
#include<bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define MAXN 30000
#define THRESHOLD 180
int n, m, startPosition, endPosition, dist[MAXN + 3][THRESHOLD + 3];
vector<int> dogesAtPosition[MAXN + 3];
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int b, p;
cin >> b >> p;
if (i == 0) {
startPosition = b;
}
if (i == 1) {
endPosition = b;
}
dogesAtPosition[b].push_back(p);
}
for (int i = 0; i < n; i++) {
sort(dogesAtPosition[i].begin(), dogesAtPosition[i].end());
dogesAtPosition[i].erase(unique(dogesAtPosition[i].begin(), dogesAtPosition[i].end()), dogesAtPosition[i].end());
}
for (int i = 0; i < n; i++) {
for (int p = 0; p <= THRESHOLD; p++) {
dist[i][p] = INF;
}
}
dist[startPosition][0] = 0;
priority_queue<pair<int, pair<int, int> > > pq;
pq.push({-dist[startPosition][0], {startPosition, 0}});
while (!pq.empty()) {
auto [d, state] = pq.top(); pq.pop();
auto [position, power] = state;
d *= -1; // Because we push -dist
if (d > dist[position][power]) {
continue;
}
if (power == 0) {
for (auto newPower: dogesAtPosition[position]) {
if (newPower <= THRESHOLD) {
if (dist[position][newPower] > dist[position][power]) {
dist[position][newPower] = dist[position][power];
pq.push({-dist[position][newPower], {position, newPower}});
}
} else {
for (int direction = -1; direction <= 1; direction += 2) {
for (int numJumps = 1; ; numJumps++) {
int newPosition = position + direction * newPower * numJumps;
if (newPosition < 0 || newPosition >= n) {
break;
}
if (dist[newPosition][0] > dist[position][power] + numJumps) {
dist[newPosition][0] = dist[position][power] + numJumps;
pq.push({-dist[newPosition][0], {newPosition, 0}});
}
}
}
}
}
} else {
for (int direction = -1; direction <= 1; direction += 2) {
int newPosition = position + direction * power;
if (newPosition >= 0 && newPosition < n) {
if (dist[newPosition][0] > dist[position][power] + 1) {
dist[newPosition][0] = dist[position][power] + 1;
pq.push({-dist[newPosition][0], {newPosition, 0}});
}
if (dist[newPosition][power] > dist[position][power] + 1) {
dist[newPosition][power] = dist[position][power] + 1;
pq.push({-dist[newPosition][power], {newPosition, power}});
}
}
}
}
}
int ans = INF;
for (int p = 0; p <= THRESHOLD; p++) {
ans = min(ans, dist[endPosition][p]);
}
if (ans < INF) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1016 KB |
Output is correct |
3 |
Correct |
1 ms |
980 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 |
1012 KB |
Output is correct |
7 |
Correct |
1 ms |
980 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 |
1020 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 |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1020 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
3 ms |
1132 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1108 KB |
Output is correct |
14 |
Correct |
3 ms |
1152 KB |
Output is correct |
15 |
Correct |
2 ms |
1152 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 |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1016 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1016 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1016 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1152 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
4 ms |
1540 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
3 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
3 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2388 KB |
Output is correct |
25 |
Correct |
3 ms |
2388 KB |
Output is correct |
26 |
Correct |
3 ms |
2388 KB |
Output is correct |
27 |
Correct |
2 ms |
2388 KB |
Output is correct |
28 |
Correct |
4 ms |
2436 KB |
Output is correct |
29 |
Correct |
9 ms |
2388 KB |
Output is correct |
30 |
Correct |
5 ms |
2388 KB |
Output is correct |
31 |
Correct |
5 ms |
2388 KB |
Output is correct |
32 |
Correct |
5 ms |
2424 KB |
Output is correct |
33 |
Correct |
17 ms |
2540 KB |
Output is correct |
34 |
Correct |
16 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1016 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1016 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
1012 KB |
Output is correct |
8 |
Correct |
1 ms |
1012 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1104 KB |
Output is correct |
14 |
Correct |
3 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 KB |
Output is correct |
17 |
Correct |
4 ms |
1620 KB |
Output is correct |
18 |
Correct |
2 ms |
2264 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
3 ms |
2516 KB |
Output is correct |
21 |
Correct |
1 ms |
1240 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
3 ms |
2132 KB |
Output is correct |
24 |
Correct |
4 ms |
2388 KB |
Output is correct |
25 |
Correct |
4 ms |
2436 KB |
Output is correct |
26 |
Correct |
3 ms |
2392 KB |
Output is correct |
27 |
Correct |
4 ms |
2392 KB |
Output is correct |
28 |
Correct |
4 ms |
2436 KB |
Output is correct |
29 |
Correct |
9 ms |
2388 KB |
Output is correct |
30 |
Correct |
4 ms |
2388 KB |
Output is correct |
31 |
Correct |
6 ms |
2388 KB |
Output is correct |
32 |
Correct |
4 ms |
2388 KB |
Output is correct |
33 |
Correct |
18 ms |
2548 KB |
Output is correct |
34 |
Correct |
19 ms |
2516 KB |
Output is correct |
35 |
Correct |
23 ms |
2572 KB |
Output is correct |
36 |
Correct |
5 ms |
1876 KB |
Output is correct |
37 |
Correct |
27 ms |
2816 KB |
Output is correct |
38 |
Correct |
27 ms |
3028 KB |
Output is correct |
39 |
Correct |
30 ms |
3000 KB |
Output is correct |
40 |
Correct |
28 ms |
3092 KB |
Output is correct |
41 |
Correct |
29 ms |
3036 KB |
Output is correct |
42 |
Correct |
19 ms |
2820 KB |
Output is correct |
43 |
Correct |
17 ms |
2904 KB |
Output is correct |
44 |
Correct |
15 ms |
2868 KB |
Output is correct |
45 |
Correct |
62 ms |
3304 KB |
Output is correct |
46 |
Correct |
61 ms |
3388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
992 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
992 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
992 KB |
Output is correct |
6 |
Correct |
1 ms |
988 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 |
1108 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1024 KB |
Output is correct |
13 |
Correct |
2 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
1540 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2428 KB |
Output is correct |
20 |
Correct |
3 ms |
2436 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
3 ms |
2260 KB |
Output is correct |
23 |
Correct |
3 ms |
2132 KB |
Output is correct |
24 |
Correct |
5 ms |
2360 KB |
Output is correct |
25 |
Correct |
4 ms |
2488 KB |
Output is correct |
26 |
Correct |
3 ms |
2400 KB |
Output is correct |
27 |
Correct |
3 ms |
2400 KB |
Output is correct |
28 |
Correct |
4 ms |
2528 KB |
Output is correct |
29 |
Correct |
8 ms |
2444 KB |
Output is correct |
30 |
Correct |
3 ms |
2400 KB |
Output is correct |
31 |
Correct |
6 ms |
2436 KB |
Output is correct |
32 |
Correct |
4 ms |
2400 KB |
Output is correct |
33 |
Correct |
16 ms |
2552 KB |
Output is correct |
34 |
Correct |
16 ms |
2528 KB |
Output is correct |
35 |
Correct |
23 ms |
2572 KB |
Output is correct |
36 |
Correct |
6 ms |
1888 KB |
Output is correct |
37 |
Correct |
37 ms |
2912 KB |
Output is correct |
38 |
Correct |
29 ms |
3040 KB |
Output is correct |
39 |
Correct |
34 ms |
3016 KB |
Output is correct |
40 |
Correct |
28 ms |
3020 KB |
Output is correct |
41 |
Correct |
30 ms |
3048 KB |
Output is correct |
42 |
Correct |
13 ms |
2904 KB |
Output is correct |
43 |
Correct |
17 ms |
2820 KB |
Output is correct |
44 |
Correct |
16 ms |
2820 KB |
Output is correct |
45 |
Correct |
64 ms |
3340 KB |
Output is correct |
46 |
Correct |
64 ms |
3348 KB |
Output is correct |
47 |
Correct |
90 ms |
10848 KB |
Output is correct |
48 |
Correct |
24 ms |
18200 KB |
Output is correct |
49 |
Correct |
31 ms |
19872 KB |
Output is correct |
50 |
Correct |
22 ms |
21500 KB |
Output is correct |
51 |
Correct |
75 ms |
23860 KB |
Output is correct |
52 |
Correct |
71 ms |
23920 KB |
Output is correct |
53 |
Correct |
44 ms |
23288 KB |
Output is correct |
54 |
Correct |
14 ms |
22484 KB |
Output is correct |
55 |
Correct |
16 ms |
22488 KB |
Output is correct |
56 |
Correct |
31 ms |
23592 KB |
Output is correct |
57 |
Correct |
82 ms |
22580 KB |
Output is correct |
58 |
Correct |
27 ms |
22760 KB |
Output is correct |
59 |
Correct |
34 ms |
22956 KB |
Output is correct |
60 |
Correct |
38 ms |
22856 KB |
Output is correct |
61 |
Correct |
44 ms |
22988 KB |
Output is correct |
62 |
Correct |
57 ms |
23672 KB |
Output is correct |
63 |
Correct |
56 ms |
23784 KB |
Output is correct |
64 |
Correct |
60 ms |
23760 KB |
Output is correct |
65 |
Correct |
296 ms |
24520 KB |
Output is correct |
66 |
Correct |
956 ms |
24488 KB |
Output is correct |
67 |
Correct |
959 ms |
24644 KB |
Output is correct |