#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <cstring>
int64_t x, d;
int64_t src, dest;
int64_t dist[30005][175];
std::vector<int64_t> dogs_less_than_sqrt[30005];
std::vector<int64_t> dogs_greater_than_sqrt[30005];
int64_t bfs(int64_t src, int64_t dest) {
memset(dist,0x3f,sizeof(dist));
std::priority_queue<std::tuple<int64_t,int64_t,int64_t>, std::vector<std::tuple<int64_t,int64_t,int64_t>>, std::greater<std::tuple<int64_t,int64_t,int64_t>>> q;
q.emplace(0,src,0);
dist[src][0] = 0;
while (!q.empty()) {
auto [c, k, cap] = q.top();
q.pop();
// switch dog (<= sqrt)
for (const auto& i : dogs_less_than_sqrt[k]) {
if (dist[k][i]>dist[k][cap]) {
dist[k][i] = dist[k][cap];
q.emplace(dist[k][i], k, i);
}
}
// try to go as far as possible (> sqrt)
for (const auto& i : dogs_greater_than_sqrt[k]) {
for (int64_t j = 1; k+j*i < x; j++) {
if (dist[k+j*i][0]>dist[k][cap]+j) {
dist[k+j*i][0] = dist[k][cap]+j;
q.emplace(dist[k+j*i][0], k+j*i, 0);
}
}
for (int64_t j = 1; k-j*i >= 0; j++) {
if (dist[k-j*i][0]>dist[k][cap]+j) {
dist[k-j*i][0] = dist[k][cap]+j;
q.emplace(dist[k+j*i][0], k-j*i, 0);
}
}
}
// use the current dog (<= sqrt)
if (k-cap>=0&&dist[k-cap][cap]>dist[k][cap]+1) {
dist[k-cap][cap] = dist[k][cap]+1;
q.emplace(dist[k-cap][cap], k-cap, cap);
}
if (k+cap<x&&dist[k+cap][cap]>dist[k][cap]+1) {
dist[k+cap][cap] = dist[k][cap]+1;
q.emplace(dist[k+cap][cap], k+cap, cap);
}
}
int64_t ret = 0x3f3f3f3f3f3f3f3f;
for (int64_t i = 0; i < 175; i++) {
ret = std::min(ret, dist[dest][i]);
}
return ((ret==0x3f3f3f3f3f3f3f3f) ? -1 : ret);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::cin >> x >> d;
for (int64_t i = 0, a, b; i < d; i++) {
std::cin >> a >> b;
if (i==0) {
src = a;
}
else if (i==1) {
dest = a;
}
if (b<=173) {
dogs_less_than_sqrt[a].emplace_back(b);
}
else {
dogs_greater_than_sqrt[a].emplace_back(b);
}
}
for (int64_t i = 1; i <= x; i++) {
std::sort(dogs_less_than_sqrt[i].begin(), dogs_less_than_sqrt[i].end());
std::sort(dogs_greater_than_sqrt[i].begin(), dogs_greater_than_sqrt[i].end());
dogs_less_than_sqrt[i].erase(std::unique(dogs_less_than_sqrt[i].begin(), dogs_less_than_sqrt[i].end()),
dogs_less_than_sqrt[i].end());
dogs_greater_than_sqrt[i].erase(std::unique(dogs_greater_than_sqrt[i].begin(), dogs_greater_than_sqrt[i].end()),
dogs_greater_than_sqrt[i].end());
}
std::cout << bfs(src,dest) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
42844 KB |
Output is correct |
2 |
Correct |
16 ms |
42756 KB |
Output is correct |
3 |
Correct |
16 ms |
42772 KB |
Output is correct |
4 |
Correct |
16 ms |
42876 KB |
Output is correct |
5 |
Correct |
16 ms |
42880 KB |
Output is correct |
6 |
Correct |
16 ms |
42844 KB |
Output is correct |
7 |
Correct |
16 ms |
42888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
42840 KB |
Output is correct |
2 |
Correct |
16 ms |
42840 KB |
Output is correct |
3 |
Correct |
16 ms |
42788 KB |
Output is correct |
4 |
Correct |
16 ms |
42844 KB |
Output is correct |
5 |
Correct |
18 ms |
42844 KB |
Output is correct |
6 |
Correct |
16 ms |
42932 KB |
Output is correct |
7 |
Correct |
16 ms |
42868 KB |
Output is correct |
8 |
Correct |
16 ms |
42840 KB |
Output is correct |
9 |
Correct |
16 ms |
42840 KB |
Output is correct |
10 |
Correct |
16 ms |
42972 KB |
Output is correct |
11 |
Correct |
17 ms |
42840 KB |
Output is correct |
12 |
Correct |
16 ms |
42840 KB |
Output is correct |
13 |
Correct |
16 ms |
42892 KB |
Output is correct |
14 |
Correct |
17 ms |
42840 KB |
Output is correct |
15 |
Correct |
17 ms |
43044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
42840 KB |
Output is correct |
2 |
Correct |
16 ms |
42840 KB |
Output is correct |
3 |
Correct |
16 ms |
42840 KB |
Output is correct |
4 |
Correct |
16 ms |
42844 KB |
Output is correct |
5 |
Correct |
16 ms |
42732 KB |
Output is correct |
6 |
Correct |
17 ms |
42800 KB |
Output is correct |
7 |
Correct |
21 ms |
42708 KB |
Output is correct |
8 |
Correct |
15 ms |
42844 KB |
Output is correct |
9 |
Correct |
16 ms |
42960 KB |
Output is correct |
10 |
Correct |
16 ms |
42840 KB |
Output is correct |
11 |
Correct |
19 ms |
43100 KB |
Output is correct |
12 |
Correct |
16 ms |
42888 KB |
Output is correct |
13 |
Correct |
22 ms |
42844 KB |
Output is correct |
14 |
Correct |
17 ms |
43088 KB |
Output is correct |
15 |
Correct |
17 ms |
42844 KB |
Output is correct |
16 |
Correct |
16 ms |
42952 KB |
Output is correct |
17 |
Correct |
20 ms |
42844 KB |
Output is correct |
18 |
Correct |
16 ms |
42844 KB |
Output is correct |
19 |
Correct |
17 ms |
42844 KB |
Output is correct |
20 |
Correct |
17 ms |
42844 KB |
Output is correct |
21 |
Correct |
16 ms |
42844 KB |
Output is correct |
22 |
Correct |
21 ms |
42940 KB |
Output is correct |
23 |
Correct |
18 ms |
42844 KB |
Output is correct |
24 |
Correct |
21 ms |
43052 KB |
Output is correct |
25 |
Correct |
19 ms |
42844 KB |
Output is correct |
26 |
Correct |
17 ms |
42888 KB |
Output is correct |
27 |
Correct |
16 ms |
42800 KB |
Output is correct |
28 |
Correct |
18 ms |
43024 KB |
Output is correct |
29 |
Correct |
20 ms |
42844 KB |
Output is correct |
30 |
Correct |
17 ms |
42844 KB |
Output is correct |
31 |
Correct |
19 ms |
42840 KB |
Output is correct |
32 |
Correct |
18 ms |
42844 KB |
Output is correct |
33 |
Correct |
27 ms |
43096 KB |
Output is correct |
34 |
Correct |
25 ms |
42844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
42840 KB |
Output is correct |
2 |
Correct |
16 ms |
42840 KB |
Output is correct |
3 |
Correct |
16 ms |
42844 KB |
Output is correct |
4 |
Correct |
16 ms |
42844 KB |
Output is correct |
5 |
Correct |
16 ms |
42848 KB |
Output is correct |
6 |
Correct |
16 ms |
42844 KB |
Output is correct |
7 |
Correct |
17 ms |
42908 KB |
Output is correct |
8 |
Correct |
17 ms |
42840 KB |
Output is correct |
9 |
Correct |
16 ms |
42844 KB |
Output is correct |
10 |
Correct |
17 ms |
42844 KB |
Output is correct |
11 |
Correct |
17 ms |
42844 KB |
Output is correct |
12 |
Correct |
16 ms |
42764 KB |
Output is correct |
13 |
Correct |
19 ms |
42844 KB |
Output is correct |
14 |
Correct |
17 ms |
42844 KB |
Output is correct |
15 |
Correct |
17 ms |
42844 KB |
Output is correct |
16 |
Correct |
17 ms |
42844 KB |
Output is correct |
17 |
Correct |
19 ms |
42900 KB |
Output is correct |
18 |
Correct |
16 ms |
42844 KB |
Output is correct |
19 |
Correct |
16 ms |
42920 KB |
Output is correct |
20 |
Correct |
17 ms |
42840 KB |
Output is correct |
21 |
Correct |
17 ms |
42844 KB |
Output is correct |
22 |
Correct |
17 ms |
42952 KB |
Output is correct |
23 |
Correct |
18 ms |
42792 KB |
Output is correct |
24 |
Correct |
20 ms |
43056 KB |
Output is correct |
25 |
Correct |
21 ms |
42840 KB |
Output is correct |
26 |
Correct |
17 ms |
42772 KB |
Output is correct |
27 |
Correct |
16 ms |
42844 KB |
Output is correct |
28 |
Correct |
18 ms |
42844 KB |
Output is correct |
29 |
Correct |
21 ms |
42872 KB |
Output is correct |
30 |
Correct |
17 ms |
42776 KB |
Output is correct |
31 |
Correct |
19 ms |
43096 KB |
Output is correct |
32 |
Correct |
18 ms |
42844 KB |
Output is correct |
33 |
Correct |
26 ms |
43088 KB |
Output is correct |
34 |
Correct |
26 ms |
42844 KB |
Output is correct |
35 |
Correct |
48 ms |
43608 KB |
Output is correct |
36 |
Correct |
22 ms |
43096 KB |
Output is correct |
37 |
Correct |
46 ms |
43552 KB |
Output is correct |
38 |
Correct |
72 ms |
43612 KB |
Output is correct |
39 |
Correct |
54 ms |
43612 KB |
Output is correct |
40 |
Correct |
98 ms |
43612 KB |
Output is correct |
41 |
Correct |
85 ms |
43600 KB |
Output is correct |
42 |
Correct |
19 ms |
43228 KB |
Output is correct |
43 |
Correct |
19 ms |
43232 KB |
Output is correct |
44 |
Correct |
19 ms |
43284 KB |
Output is correct |
45 |
Correct |
90 ms |
44120 KB |
Output is correct |
46 |
Correct |
90 ms |
44184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
42844 KB |
Output is correct |
2 |
Correct |
16 ms |
42844 KB |
Output is correct |
3 |
Correct |
16 ms |
42844 KB |
Output is correct |
4 |
Correct |
16 ms |
42840 KB |
Output is correct |
5 |
Correct |
16 ms |
42844 KB |
Output is correct |
6 |
Correct |
16 ms |
42844 KB |
Output is correct |
7 |
Correct |
16 ms |
42840 KB |
Output is correct |
8 |
Correct |
16 ms |
43096 KB |
Output is correct |
9 |
Correct |
16 ms |
42744 KB |
Output is correct |
10 |
Correct |
16 ms |
42844 KB |
Output is correct |
11 |
Correct |
16 ms |
43052 KB |
Output is correct |
12 |
Correct |
16 ms |
42844 KB |
Output is correct |
13 |
Correct |
16 ms |
42996 KB |
Output is correct |
14 |
Correct |
16 ms |
42928 KB |
Output is correct |
15 |
Correct |
17 ms |
42844 KB |
Output is correct |
16 |
Correct |
16 ms |
42844 KB |
Output is correct |
17 |
Correct |
20 ms |
42872 KB |
Output is correct |
18 |
Correct |
16 ms |
42992 KB |
Output is correct |
19 |
Correct |
19 ms |
42828 KB |
Output is correct |
20 |
Correct |
17 ms |
42848 KB |
Output is correct |
21 |
Correct |
16 ms |
42844 KB |
Output is correct |
22 |
Correct |
16 ms |
42844 KB |
Output is correct |
23 |
Correct |
17 ms |
42992 KB |
Output is correct |
24 |
Correct |
21 ms |
42844 KB |
Output is correct |
25 |
Correct |
19 ms |
42844 KB |
Output is correct |
26 |
Correct |
17 ms |
42844 KB |
Output is correct |
27 |
Correct |
16 ms |
42924 KB |
Output is correct |
28 |
Correct |
17 ms |
42840 KB |
Output is correct |
29 |
Correct |
20 ms |
42844 KB |
Output is correct |
30 |
Correct |
16 ms |
42844 KB |
Output is correct |
31 |
Correct |
18 ms |
42844 KB |
Output is correct |
32 |
Correct |
17 ms |
42844 KB |
Output is correct |
33 |
Correct |
28 ms |
42844 KB |
Output is correct |
34 |
Correct |
26 ms |
42844 KB |
Output is correct |
35 |
Correct |
48 ms |
43612 KB |
Output is correct |
36 |
Correct |
19 ms |
43096 KB |
Output is correct |
37 |
Correct |
43 ms |
43548 KB |
Output is correct |
38 |
Correct |
66 ms |
43612 KB |
Output is correct |
39 |
Correct |
45 ms |
43612 KB |
Output is correct |
40 |
Correct |
96 ms |
43664 KB |
Output is correct |
41 |
Correct |
81 ms |
43608 KB |
Output is correct |
42 |
Correct |
19 ms |
43112 KB |
Output is correct |
43 |
Correct |
19 ms |
43232 KB |
Output is correct |
44 |
Correct |
19 ms |
43264 KB |
Output is correct |
45 |
Correct |
89 ms |
44112 KB |
Output is correct |
46 |
Correct |
95 ms |
44112 KB |
Output is correct |
47 |
Correct |
159 ms |
44124 KB |
Output is correct |
48 |
Correct |
20 ms |
43352 KB |
Output is correct |
49 |
Runtime error |
56 ms |
87548 KB |
Execution killed with signal 11 |
50 |
Halted |
0 ms |
0 KB |
- |