Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit36a53da

Browse files
authored
C++ code compiles + extra parallel to python
1 parent96d4b88 commit36a53da

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

‎src/graph/01_bfs.md‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,23 @@ In this article we demonstrate how we can use BFS to solve the SSSP (single-sour
1515
##Algorithm
1616

1717
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):
18+
The general form of Dijkstra's algorithm is:
1919

2020
=== "C++"
2121
```cpp
22+
int n = adj.size();
2223
d.assign(n, INF);
2324
d[s] = 0;
24-
priority_queue<pair<int, int>> q = {{0, s}};
25-
while (!empty(q)) {
26-
int[dv, v] = q.top();
25+
priority_queue<pair<int, int>,vector<pair<int,int>>, greater<pair<int,int>> > q;
26+
q.push({0,s});
27+
while (!q.empty()) {
28+
auto[dv, v] = q.top();
2729
q.pop();
28-
if (-dv == d[v]) {
30+
if (dv == d[v]) {
2931
for (auto[u, w] : adj[v]) {
3032
if (d[v] + w < d[u]) {
3133
d[u] = d[v] + w;
32-
q.insert({-d[u], u});
34+
q.push({d[u], u});
3335
}
3436
}
3537
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp