#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <unordered_map>
int x, dog_cnt;
std::vector<std::pair<int,int>> dogs;
std::unordered_map<int,int> dist[30005];
std::vector<int> dogs_in[30005];
int astar(int pos, int dog) {
std::priority_queue<std::tuple<int,int,int>, std::vector<std::tuple<int,int,int>>, std::greater<std::tuple<int,int,int>>> pq;
pq.emplace(0+std::abs(pos-dogs[1].first),pos,dog);
dist[pos][dog] = 0;
int iterations = 2000000;
while (!pq.empty()&&iterations--) {
auto [c, pos, dog] = pq.top();
pq.pop();
if (pos-dogs[dog].second>=0) {
if (!dist[pos-dogs[dog].second].count(dog)||dist[pos-dogs[dog].second][dog]>dist[pos][dog]+1) {
dist[pos-dogs[dog].second][dog] = dist[pos][dog]+1;
pq.emplace(dist[pos-dogs[dog].second][dog]+std::abs((pos-dogs[dog].second)-dogs[1].first),
pos-dogs[dog].second,
dog);
}
}
if (pos+dogs[dog].second<x) {
if (!dist[pos+dogs[dog].second].count(dog)||dist[pos+dogs[dog].second][dog]>dist[pos][dog]+1) {
dist[pos+dogs[dog].second][dog] = dist[pos][dog]+1;
pq.emplace(dist[pos+dogs[dog].second][dog]+std::abs((pos+dogs[dog].second)-dogs[1].first),
pos+dogs[dog].second,
dog);
}
}
for (const auto& i : dogs_in[pos]) {
if (!dist[pos].count(i)||dist[pos][i]>dist[pos][dog]) {
dist[pos][i] = dist[pos][dog];
pq.emplace(dist[pos][i]+std::abs(pos-dogs[1].first),
pos,
i);
}
}
}
int min = 0x3f3f3f3f;
for (int i = 0; i < dog_cnt; i++) {
if (!dist[dogs[1].first].count(i)) {
continue;
}
min = std::min(min, dist[dogs[1].first][i]);
}
return ((min==0x3f3f3f3f) ? -1 : min);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::cin >> x >> dog_cnt;
dogs.reserve(dog_cnt);
for (int i = 0, a, b; i < dog_cnt; i++) {
std::cin >> a >> b;
dogs.emplace_back(a,b);
dogs_in[a].emplace_back(i);
}
std::cout << astar(dogs[0].first, 0) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
8 ms |
3164 KB |
Output is correct |
12 |
Correct |
72 ms |
10760 KB |
Output is correct |
13 |
Correct |
74 ms |
10832 KB |
Output is correct |
14 |
Correct |
9 ms |
2904 KB |
Output is correct |
15 |
Correct |
7 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
3160 KB |
Output is correct |
11 |
Correct |
9 ms |
3160 KB |
Output is correct |
12 |
Correct |
70 ms |
10836 KB |
Output is correct |
13 |
Correct |
72 ms |
10836 KB |
Output is correct |
14 |
Correct |
8 ms |
2904 KB |
Output is correct |
15 |
Correct |
7 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2628 KB |
Output is correct |
17 |
Correct |
22 ms |
3664 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
358 ms |
88516 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
6 ms |
3072 KB |
Output is correct |
24 |
Correct |
17 ms |
3420 KB |
Output is correct |
25 |
Correct |
9 ms |
3312 KB |
Output is correct |
26 |
Correct |
330 ms |
86096 KB |
Output is correct |
27 |
Correct |
356 ms |
87196 KB |
Output is correct |
28 |
Correct |
6 ms |
3672 KB |
Output is correct |
29 |
Correct |
22 ms |
5740 KB |
Output is correct |
30 |
Correct |
4 ms |
3416 KB |
Output is correct |
31 |
Correct |
8 ms |
4424 KB |
Output is correct |
32 |
Correct |
8 ms |
3672 KB |
Output is correct |
33 |
Correct |
65 ms |
8392 KB |
Output is correct |
34 |
Correct |
112 ms |
8388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
2 ms |
2816 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2800 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2744 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
10 ms |
3160 KB |
Output is correct |
12 |
Correct |
88 ms |
10836 KB |
Output is correct |
13 |
Correct |
82 ms |
10944 KB |
Output is correct |
14 |
Correct |
9 ms |
3160 KB |
Output is correct |
15 |
Correct |
7 ms |
3052 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
22 ms |
3672 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
365 ms |
88420 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
6 ms |
3164 KB |
Output is correct |
24 |
Correct |
16 ms |
3376 KB |
Output is correct |
25 |
Correct |
17 ms |
3284 KB |
Output is correct |
26 |
Correct |
352 ms |
85840 KB |
Output is correct |
27 |
Correct |
367 ms |
87376 KB |
Output is correct |
28 |
Correct |
6 ms |
3672 KB |
Output is correct |
29 |
Correct |
17 ms |
5720 KB |
Output is correct |
30 |
Correct |
7 ms |
3420 KB |
Output is correct |
31 |
Correct |
7 ms |
4388 KB |
Output is correct |
32 |
Correct |
4 ms |
3836 KB |
Output is correct |
33 |
Correct |
65 ms |
8540 KB |
Output is correct |
34 |
Correct |
112 ms |
8532 KB |
Output is correct |
35 |
Correct |
361 ms |
10124 KB |
Output is correct |
36 |
Correct |
47 ms |
3664 KB |
Output is correct |
37 |
Correct |
535 ms |
16656 KB |
Output is correct |
38 |
Correct |
690 ms |
14160 KB |
Output is correct |
39 |
Correct |
372 ms |
15372 KB |
Output is correct |
40 |
Execution timed out |
1051 ms |
17380 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
15 ms |
3176 KB |
Output is correct |
12 |
Correct |
73 ms |
10772 KB |
Output is correct |
13 |
Correct |
75 ms |
10836 KB |
Output is correct |
14 |
Correct |
9 ms |
2908 KB |
Output is correct |
15 |
Correct |
8 ms |
2904 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
22 ms |
3676 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
342 ms |
88576 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
6 ms |
3164 KB |
Output is correct |
24 |
Correct |
16 ms |
3536 KB |
Output is correct |
25 |
Correct |
10 ms |
3420 KB |
Output is correct |
26 |
Correct |
347 ms |
85876 KB |
Output is correct |
27 |
Correct |
346 ms |
87328 KB |
Output is correct |
28 |
Correct |
5 ms |
3816 KB |
Output is correct |
29 |
Correct |
16 ms |
5744 KB |
Output is correct |
30 |
Correct |
4 ms |
3420 KB |
Output is correct |
31 |
Correct |
9 ms |
4276 KB |
Output is correct |
32 |
Correct |
5 ms |
3676 KB |
Output is correct |
33 |
Correct |
64 ms |
8460 KB |
Output is correct |
34 |
Correct |
112 ms |
8536 KB |
Output is correct |
35 |
Correct |
334 ms |
10164 KB |
Output is correct |
36 |
Correct |
29 ms |
3848 KB |
Output is correct |
37 |
Correct |
471 ms |
16816 KB |
Output is correct |
38 |
Correct |
610 ms |
14092 KB |
Output is correct |
39 |
Correct |
374 ms |
15244 KB |
Output is correct |
40 |
Execution timed out |
1075 ms |
17404 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |