#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <cstring>
const int BLK_SIZE = 269;
int x, d;
int src, dest;
int dist[30005][BLK_SIZE+1];
std::vector<int> dogs_less_than_sqrt[30005];
std::vector<int> dogs_greater_than_sqrt[30005];
int bfs(int src, int dest) {
memset(dist,0x3f,sizeof(dist));
std::priority_queue<std::tuple<int,int,int>, std::vector<std::tuple<int,int,int>>, std::greater<std::tuple<int,int,int>>> q;
q.emplace(0,src,0);
dist[src][0] = 0;
while (!q.empty()) {
auto [c, k, cap] = q.top();
q.pop();
// switch dog (<= sqrt)
if (dist[k][0]>dist[k][cap]) {
dist[k][0] = dist[k][cap];
q.emplace(dist[k][0], k, 0);
}
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 (int 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 (int 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 (cap!=0) {
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);
}
}
}
int ret = 0x3f3f3f3f;
for (int i = 0; i < BLK_SIZE; i++) {
ret = std::min(ret, dist[dest][i]);
}
return ((ret==0x3f3f3f3f) ? -1 : ret);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::cin >> x >> d;
for (int i = 0, a, b; i < d; i++) {
std::cin >> a >> b;
if (i==0) {
src = a;
}
else if (i==1) {
dest = a;
}
if (b<=BLK_SIZE) {
dogs_less_than_sqrt[a].emplace_back(b);
}
else {
dogs_greater_than_sqrt[a].emplace_back(b);
}
}
for (int i = 0; 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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
33368 KB |
Output is correct |
2 |
Correct |
15 ms |
33368 KB |
Output is correct |
3 |
Correct |
9 ms |
33372 KB |
Output is correct |
4 |
Correct |
9 ms |
33372 KB |
Output is correct |
5 |
Correct |
9 ms |
33372 KB |
Output is correct |
6 |
Correct |
9 ms |
33516 KB |
Output is correct |
7 |
Correct |
9 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
33368 KB |
Output is correct |
2 |
Correct |
11 ms |
33372 KB |
Output is correct |
3 |
Correct |
8 ms |
33372 KB |
Output is correct |
4 |
Correct |
8 ms |
33372 KB |
Output is correct |
5 |
Correct |
8 ms |
33376 KB |
Output is correct |
6 |
Correct |
9 ms |
33372 KB |
Output is correct |
7 |
Correct |
8 ms |
33516 KB |
Output is correct |
8 |
Correct |
8 ms |
33408 KB |
Output is correct |
9 |
Correct |
8 ms |
33372 KB |
Output is correct |
10 |
Correct |
9 ms |
33372 KB |
Output is correct |
11 |
Correct |
9 ms |
33596 KB |
Output is correct |
12 |
Correct |
9 ms |
33372 KB |
Output is correct |
13 |
Correct |
8 ms |
33448 KB |
Output is correct |
14 |
Correct |
11 ms |
33628 KB |
Output is correct |
15 |
Correct |
9 ms |
33628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
33428 KB |
Output is correct |
2 |
Correct |
9 ms |
33368 KB |
Output is correct |
3 |
Correct |
9 ms |
33416 KB |
Output is correct |
4 |
Correct |
9 ms |
33372 KB |
Output is correct |
5 |
Correct |
8 ms |
33508 KB |
Output is correct |
6 |
Correct |
9 ms |
33332 KB |
Output is correct |
7 |
Correct |
10 ms |
33368 KB |
Output is correct |
8 |
Correct |
9 ms |
33372 KB |
Output is correct |
9 |
Correct |
9 ms |
33372 KB |
Output is correct |
10 |
Correct |
9 ms |
33372 KB |
Output is correct |
11 |
Correct |
10 ms |
33628 KB |
Output is correct |
12 |
Correct |
11 ms |
33372 KB |
Output is correct |
13 |
Correct |
8 ms |
33372 KB |
Output is correct |
14 |
Correct |
11 ms |
33544 KB |
Output is correct |
15 |
Correct |
10 ms |
33448 KB |
Output is correct |
16 |
Correct |
9 ms |
33372 KB |
Output is correct |
17 |
Correct |
11 ms |
33600 KB |
Output is correct |
18 |
Correct |
9 ms |
33368 KB |
Output is correct |
19 |
Correct |
9 ms |
33372 KB |
Output is correct |
20 |
Correct |
9 ms |
33628 KB |
Output is correct |
21 |
Correct |
8 ms |
33496 KB |
Output is correct |
22 |
Correct |
9 ms |
33372 KB |
Output is correct |
23 |
Correct |
9 ms |
33372 KB |
Output is correct |
24 |
Correct |
10 ms |
33444 KB |
Output is correct |
25 |
Correct |
10 ms |
33368 KB |
Output is correct |
26 |
Correct |
11 ms |
33372 KB |
Output is correct |
27 |
Correct |
9 ms |
33372 KB |
Output is correct |
28 |
Correct |
10 ms |
33628 KB |
Output is correct |
29 |
Correct |
13 ms |
33604 KB |
Output is correct |
30 |
Correct |
10 ms |
33368 KB |
Output is correct |
31 |
Correct |
11 ms |
33608 KB |
Output is correct |
32 |
Correct |
12 ms |
33452 KB |
Output is correct |
33 |
Correct |
18 ms |
33628 KB |
Output is correct |
34 |
Correct |
22 ms |
33628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
33368 KB |
Output is correct |
2 |
Correct |
9 ms |
33368 KB |
Output is correct |
3 |
Correct |
9 ms |
33372 KB |
Output is correct |
4 |
Correct |
8 ms |
33372 KB |
Output is correct |
5 |
Correct |
9 ms |
33368 KB |
Output is correct |
6 |
Correct |
9 ms |
33368 KB |
Output is correct |
7 |
Correct |
9 ms |
33360 KB |
Output is correct |
8 |
Correct |
8 ms |
33368 KB |
Output is correct |
9 |
Correct |
12 ms |
33368 KB |
Output is correct |
10 |
Correct |
8 ms |
33404 KB |
Output is correct |
11 |
Correct |
10 ms |
33628 KB |
Output is correct |
12 |
Correct |
13 ms |
33372 KB |
Output is correct |
13 |
Correct |
9 ms |
33368 KB |
Output is correct |
14 |
Correct |
9 ms |
33628 KB |
Output is correct |
15 |
Correct |
10 ms |
33452 KB |
Output is correct |
16 |
Correct |
10 ms |
33352 KB |
Output is correct |
17 |
Correct |
11 ms |
33628 KB |
Output is correct |
18 |
Correct |
9 ms |
33368 KB |
Output is correct |
19 |
Correct |
9 ms |
33372 KB |
Output is correct |
20 |
Correct |
9 ms |
33628 KB |
Output is correct |
21 |
Correct |
8 ms |
33496 KB |
Output is correct |
22 |
Correct |
8 ms |
33368 KB |
Output is correct |
23 |
Correct |
10 ms |
33372 KB |
Output is correct |
24 |
Correct |
10 ms |
33628 KB |
Output is correct |
25 |
Correct |
10 ms |
33416 KB |
Output is correct |
26 |
Correct |
9 ms |
33372 KB |
Output is correct |
27 |
Correct |
9 ms |
33372 KB |
Output is correct |
28 |
Correct |
12 ms |
33628 KB |
Output is correct |
29 |
Correct |
15 ms |
33372 KB |
Output is correct |
30 |
Correct |
10 ms |
33372 KB |
Output is correct |
31 |
Correct |
14 ms |
33372 KB |
Output is correct |
32 |
Correct |
10 ms |
33368 KB |
Output is correct |
33 |
Correct |
19 ms |
33628 KB |
Output is correct |
34 |
Correct |
18 ms |
33624 KB |
Output is correct |
35 |
Correct |
25 ms |
33884 KB |
Output is correct |
36 |
Correct |
11 ms |
33628 KB |
Output is correct |
37 |
Correct |
34 ms |
33880 KB |
Output is correct |
38 |
Correct |
33 ms |
33880 KB |
Output is correct |
39 |
Correct |
33 ms |
33884 KB |
Output is correct |
40 |
Correct |
32 ms |
33880 KB |
Output is correct |
41 |
Correct |
32 ms |
34008 KB |
Output is correct |
42 |
Correct |
12 ms |
33628 KB |
Output is correct |
43 |
Correct |
11 ms |
33628 KB |
Output is correct |
44 |
Correct |
12 ms |
33624 KB |
Output is correct |
45 |
Correct |
66 ms |
34136 KB |
Output is correct |
46 |
Correct |
69 ms |
34136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
33372 KB |
Output is correct |
2 |
Correct |
9 ms |
33372 KB |
Output is correct |
3 |
Correct |
8 ms |
33368 KB |
Output is correct |
4 |
Correct |
9 ms |
33372 KB |
Output is correct |
5 |
Correct |
9 ms |
33372 KB |
Output is correct |
6 |
Correct |
10 ms |
33412 KB |
Output is correct |
7 |
Correct |
8 ms |
33412 KB |
Output is correct |
8 |
Correct |
9 ms |
33372 KB |
Output is correct |
9 |
Correct |
9 ms |
33368 KB |
Output is correct |
10 |
Correct |
9 ms |
33372 KB |
Output is correct |
11 |
Correct |
10 ms |
33628 KB |
Output is correct |
12 |
Correct |
8 ms |
33372 KB |
Output is correct |
13 |
Correct |
12 ms |
33536 KB |
Output is correct |
14 |
Correct |
10 ms |
33624 KB |
Output is correct |
15 |
Correct |
10 ms |
33580 KB |
Output is correct |
16 |
Correct |
9 ms |
33372 KB |
Output is correct |
17 |
Correct |
10 ms |
33628 KB |
Output is correct |
18 |
Correct |
8 ms |
33372 KB |
Output is correct |
19 |
Correct |
9 ms |
33372 KB |
Output is correct |
20 |
Correct |
9 ms |
33624 KB |
Output is correct |
21 |
Correct |
8 ms |
33372 KB |
Output is correct |
22 |
Correct |
12 ms |
33372 KB |
Output is correct |
23 |
Correct |
9 ms |
33372 KB |
Output is correct |
24 |
Correct |
10 ms |
33624 KB |
Output is correct |
25 |
Correct |
10 ms |
33428 KB |
Output is correct |
26 |
Correct |
9 ms |
33368 KB |
Output is correct |
27 |
Correct |
9 ms |
33524 KB |
Output is correct |
28 |
Correct |
11 ms |
33640 KB |
Output is correct |
29 |
Correct |
13 ms |
33460 KB |
Output is correct |
30 |
Correct |
10 ms |
33372 KB |
Output is correct |
31 |
Correct |
12 ms |
33604 KB |
Output is correct |
32 |
Correct |
10 ms |
33432 KB |
Output is correct |
33 |
Correct |
18 ms |
33628 KB |
Output is correct |
34 |
Correct |
20 ms |
33628 KB |
Output is correct |
35 |
Correct |
25 ms |
33980 KB |
Output is correct |
36 |
Correct |
12 ms |
33628 KB |
Output is correct |
37 |
Correct |
35 ms |
33920 KB |
Output is correct |
38 |
Correct |
34 ms |
33884 KB |
Output is correct |
39 |
Correct |
32 ms |
33880 KB |
Output is correct |
40 |
Correct |
32 ms |
33884 KB |
Output is correct |
41 |
Correct |
32 ms |
33884 KB |
Output is correct |
42 |
Correct |
12 ms |
33624 KB |
Output is correct |
43 |
Correct |
11 ms |
33640 KB |
Output is correct |
44 |
Correct |
11 ms |
33624 KB |
Output is correct |
45 |
Correct |
66 ms |
34136 KB |
Output is correct |
46 |
Correct |
69 ms |
34136 KB |
Output is correct |
47 |
Correct |
104 ms |
34332 KB |
Output is correct |
48 |
Correct |
13 ms |
33900 KB |
Output is correct |
49 |
Correct |
13 ms |
33884 KB |
Output is correct |
50 |
Correct |
12 ms |
33884 KB |
Output is correct |
51 |
Correct |
44 ms |
34592 KB |
Output is correct |
52 |
Correct |
61 ms |
34588 KB |
Output is correct |
53 |
Correct |
19 ms |
34392 KB |
Output is correct |
54 |
Correct |
9 ms |
33372 KB |
Output is correct |
55 |
Correct |
10 ms |
33372 KB |
Output is correct |
56 |
Correct |
15 ms |
34396 KB |
Output is correct |
57 |
Correct |
53 ms |
33628 KB |
Output is correct |
58 |
Correct |
13 ms |
33628 KB |
Output is correct |
59 |
Correct |
15 ms |
33628 KB |
Output is correct |
60 |
Correct |
17 ms |
33628 KB |
Output is correct |
61 |
Correct |
16 ms |
33628 KB |
Output is correct |
62 |
Correct |
36 ms |
34396 KB |
Output is correct |
63 |
Correct |
139 ms |
35284 KB |
Output is correct |
64 |
Correct |
264 ms |
35276 KB |
Output is correct |
65 |
Correct |
651 ms |
35300 KB |
Output is correct |
66 |
Correct |
719 ms |
34132 KB |
Output is correct |
67 |
Correct |
753 ms |
34396 KB |
Output is correct |