We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6e98192 commit 96d4b88Copy full SHA for 96d4b88
src/graph/01_bfs.md
@@ -21,20 +21,16 @@ The general form of Dijkstra's algorithm is (here a `set` is used for the priori
21
```cpp
22
d.assign(n, INF);
23
d[s] = 0;
24
- set<pair<int, int>> q;
25
- q.insert({0, s});
26
- while (!q.empty()) {
27
- int v = q.begin()->second;
28
- q.erase(q.begin());
29
-
30
- for (auto edge : adj[v]) {
31
- int u = edge.first;
32
- int w = edge.second;
33
34
- if (d[v] + w < d[u]) {
35
- q.erase({d[u], u});
36
- d[u] = d[v] + w;
37
- q.insert({d[u], u});
+ priority_queue<pair<int, int>> q = {{0, s}};
+ while (!empty(q)) {
+ int [dv, v] = q.top();
+ q.pop();
+ if (-dv == d[v]) {
+ for (auto [u, w] : adj[v]) {
+ if (d[v] + w < d[u]) {
+ d[u] = d[v] + w;
+ q.insert({-d[u], u});
+ }
38
}
39
40
0 commit comments