diff options
| author | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
|---|---|---|
| committer | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
| commit | 5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch) | |
| tree | ed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /graph/bellmannFord.cpp | |
| parent | adabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff) | |
updated tcr
Diffstat (limited to 'graph/bellmannFord.cpp')
| -rw-r--r-- | graph/bellmannFord.cpp | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/graph/bellmannFord.cpp b/graph/bellmannFord.cpp index d3d6094..21c7dbe 100644 --- a/graph/bellmannFord.cpp +++ b/graph/bellmannFord.cpp @@ -1,20 +1,19 @@ -// Laufzeit: O(|V|*|E|) -vector<edge> edges; // Kanten einfügen! -vector<int> dist, parent; - -void bellmannFord() { - dist.assign(NUM_VERTICES, INF); dist[0] = 0; - parent.assign(NUM_VERTICES, -1); - for (int i = 0; i < NUM_VERTICES - 1; i++) { - for (auto &e : edges) { - if (dist[e.from] + e.cost < dist[e.to]) { +void bellmannFord(int n, vector<edge> edges, int start) { + vector<ll> dist(n, INF), parent(n, -1); + dist[start] = 0; + + for (int i = 1; i < n; i++) { + for (edge& e : edges) { + if (dist[e.from] != INF && + dist[e.from] + e.cost < dist[e.to]) { dist[e.to] = dist[e.from] + e.cost; parent[e.to] = e.from; }}} - // "dist" und "parent" sind korrekte kürzeste Pfade. - // Folgende Zeilen prüfen nur negative Kreise. - for (auto &e : edges) { - if (dist[e.from] + e.cost < dist[e.to]) { + for (edge& e : edges) { + if (dist[e.from] != INF && + dist[e.from] + e.cost < dist[e.to]) { // Negativer Kreis gefunden. -}}} + }} + //return dist, parent; +} |
