We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see ourdocumentation.
There was an error while loading.Please reload this page.
1 parent96d4b88 commit36a53daCopy full SHA for 36a53da
src/graph/01_bfs.md
@@ -15,21 +15,23 @@ In this article we demonstrate how we can use BFS to solve the SSSP (single-sour
15
##Algorithm
16
17
We can develop the algorithm by closely studying Dijkstra's algorithm and thinking about the consequences that our special graph implies.
18
-The general form of Dijkstra's algorithm is (here a`set` is used for the priority queue):
+The general form of Dijkstra's algorithm is:
19
20
=== "C++"
21
```cpp
22
+ int n = adj.size();
23
d.assign(n, INF);
24
d[s] = 0;
- priority_queue<pair<int, int>> q = {{0, s}};
25
- while (!empty(q)) {
26
- int[dv, v] = q.top();
+ priority_queue<pair<int, int>,vector<pair<int,int>>, greater<pair<int,int>> > q;
+ q.push({0,s});
27
+ while (!q.empty()) {
28
+ auto[dv, v] = q.top();
29
q.pop();
- if (-dv == d[v]) {
30
+ if (dv == d[v]) {
31
for (auto[u, w] : adj[v]) {
32
if (d[v] + w < d[u]) {
33
d[u] = d[v] + w;
- q.insert({-d[u], u});
34
+ q.push({d[u], u});
35
}
36
37