-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtoi18_gecko.cpp
80 lines (74 loc) · 2.4 KB
/
toi18_gecko.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
ShortestPath (Dijkstra's algorithm) + Backtrack of DFS
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <queue>
#include <stack>
#define for_(i, k, n) for (int i = k; i < n; ++i)
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T>
using Vec = std::vector<T>;
const int INFINITY = 2e9;
struct Bridge {
int from;
int to;
int weight;
friend bool operator > (const Bridge& lhs, const Bridge& rhs) {
return lhs.weight > rhs.weight;
}
};
struct Ceil {
int min_distance = INFINITY; // mutable
Vec<Bridge> bridges;
};
int main() {
// input
int n_ceil, n_bridge, n_end_position, begin_position;
std::cin >> n_ceil >> n_bridge >> n_end_position >> begin_position;
assert(n_ceil <= 20000 && n_bridge <= 2000000 && begin_position < n_ceil);
Vec<int> end_positions(n_end_position); // use in answer section
for (int& pos : end_positions) {
std::cin >> pos;
assert(pos < n_ceil);
}
Vec<Ceil> ceil(n_ceil); // use in DP section
while (n_bridge-- > 0) {
int u, v, weight;
std::cin >> u >> v >> weight;
assert(u < v && v < n_ceil);
ceil[u].bridges.push_back(Bridge { u, v, weight });
ceil[v].bridges.push_back(Bridge { v, u, weight });
}
// shortest path with backtrack used bridge
std::priority_queue<Bridge, Vec<Bridge>, std::greater<Bridge>> pq;
std::stack<Bridge> backtrack; // use for print output only
pq.push(Bridge { -1, begin_position, ceil[begin_position].min_distance = 0 });
while (!pq.empty()) {
Bridge curr = pq.top(); pq.pop();
if (ceil[curr.to].min_distance != curr.weight) continue; // better value is updated. so skip
backtrack.push(curr); // 1 node จะทำ 1 ครั้ง เพราะเป็น dijktra
for (const Bridge nx : ceil[curr.to].bridges) {
if (curr.weight + nx.weight < ceil[nx.to].min_distance) {
pq.push(Bridge {
nx.from,
nx.to,
ceil[nx.to].min_distance = curr.weight + nx.weight
});
}}}
// answer
for (int pos : end_positions) {
std::cout << ceil[pos].min_distance << ' ';
assert(ceil[pos].min_distance < INFINITY);
}
std::cout << std::endl;
std::cout << backtrack.size() - 1 << std::endl; // exclude root node
while (backtrack.size() > 1) {
Bridge b = backtrack.top(); backtrack.pop();
std::cout << std::min(b.from, b.to)
<< ' ' << std::max(b.from, b.to) << std::endl;
}
}