-
Notifications
You must be signed in to change notification settings - Fork 0
/
texassummers.cpp
56 lines (52 loc) · 1.39 KB
/
texassummers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#define INF 1e9
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
void solve() {
int n;
std::cin >> n;
std::vector<std::pair<int, int>> coordinates(n + 2);
for (auto &[x, y]: coordinates) std::cin >> x >> y;
std::vector<int> distances(n + 2, INF);
distances[n] = 0;
std::vector<int> parents(n + 2, -1);
std::priority_queue<std::pair<int, int>> pq;
pq.push({0, n});
while (not pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (-d > distances[u]) continue;
for (int v = 0; v < n + 2; ++v) {
if (u == v) continue;
int dx = coordinates[u].first - coordinates[v].first;
int dy = coordinates[u].second - coordinates[v].second;
int w = dx * dx + dy * dy;
if (distances[u] + w >= distances[v]) continue;
distances[v] = distances[u] + w;
parents[v] = u;
pq.push({-distances[v], v});
}
}
if (parents[n + 1] == n) {
std::cout << "-\n";
return;
}
int current = parents[n + 1];
std::stack<int> s;
while (current != n) {
s.push(current);
current = parents[current];
}
while (not s.empty()) {
std::cout << s.top() << "\n";
s.pop();
}
}
int main(void) {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
solve();
return 0;
}