-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dijkstra.cpp
45 lines (41 loc) · 945 Bytes
/
Dijkstra.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
const int MAXN = 1'00'000;
const ll MAXW = 1'000'000ll;
constexpr ll OO = MAXW * MAXN + 1;
using Edge = pair<ll, int>; // { weigth, node}
using Adj = vector<vector<Edge>>;
template <typename T>
using min_heap =
priority_queue<T, vector<T>, greater<T>>;
pair<vll, vi> dijkstra(const Adj &g, int s) {
int n = len(g);
min_heap<Edge> pq;
vll ds(n, OO);
vi ps(n, -1);
pq.emp(0, s);
ds[s] = 0;
while (len(pq)) {
auto [du, u] = pq.top();
pq.pop();
if (ds[u] < du) continue;
for (auto [w, v] : g[u]) {
ll ndv = du + w;
if (chmin(ds[v], ndv)) {
ps[v] = u;
pq.emp(ndv, v);
}
}
return {ds, ps};
}
// optional !
vi recover_path(int source, int ending,
const vi &ps) {
if (ps[ending] == -1) return {};
int cur = ending;
vi ans;
while (cur != -1) {
ans.eb(cur);
cur = ps[cur];
}
reverse(all(ans));
return ans;
}