-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathDijkstra.cpp
33 lines (26 loc) · 1003 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
/* Dijkstra's */
/*
https://github.com/the-hyp0cr1t3/CC/blob/master/Beginner%20Topics/%5BS5%5D%20Do%20you%20understand%20the%20graphity%20of%20this%20situation/%5BEP%202%5D%20Shortest%20paths/%5BPt%202%5D%20Dijkstra%27s.md
*/
auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
vector<vector<int>> g(n); // adjacency list
vector<int64_t> d(n, INF);
vector<int> par(n, -1);
auto dijkstra = [&] (int root) {
struct state {
int v; int64_t dist;
state(int v, int64_t dist): v(v), dist(dist) {}
bool operator<(const state& o) const {
return dist > o.dist;
}
};
priority_queue<state> pq;
pq.emplace(root, d[root] = 0);
while(!pq.empty()) {
state top = pq.top(); pq.pop();
if(top.dist > d[top.v]) continue;
for(auto& [to, w]: g[top.v])
if(chmin(d[to], top.dist + w))
pq.emplace(to, d[to]), par[to] = top.v;
}
};