summaryrefslogtreecommitdiff
path: root/graph/bellmannFord.cpp
diff options
context:
space:
mode:
authormzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
committermzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
commit5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch)
treeed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /graph/bellmannFord.cpp
parentadabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff)
updated tcr
Diffstat (limited to 'graph/bellmannFord.cpp')
-rw-r--r--graph/bellmannFord.cpp29
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;
+}