-
Notifications
You must be signed in to change notification settings - Fork 153
/
bellman_ford.cpp
75 lines (65 loc) · 1.44 KB
/
bellman_ford.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Descrption: BellmanFord (Finds the shortest path from source s to all vertices v. Detects a negative weight cycle if present.)
* Usage: See below O(V E)
* Source: https://github.com/dragonslayerx
*/
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int MAX = 100;
const int INF = 1e9+5;
struct edges {
int u;
int v;
long long w;
edges(int u, int v, long long w): u(u), v(v), w(w) {}
};
int main(){
int n, m;
scanf("%d %d", &n, &m);
vector<edges> edge;
for (int i = 0; i < m; i++) {
int a, b;
long long w;
scanf("%d%d%lld", &a, &b, &w);
edge.push_back(edges(a, b, w));
}
int parent[MAX];
long long dist[MAX];
for (int i = 0; i < n; i++) {
parent[i] = 0;
dist[i] = INF;
}
dist[0] = 0; // distance of source node = 0
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < edge.size(); j++) {
int u = edge[j].u;
int v = edge[j].v;
long long w = edge[j].w;
if (dist[u] != INF) {
if (dist[v] > dist[u] + w){
dist[v] = dist[u] + w;
parent[v] = u;
}
}
}
}
bool negCycleExists = false;
for (int j = 0; j < edge.size(); j++) {
int u = edge[j].u;
int v = edge[j].v;
long long w = edge[j].w;
if (dist[v] > (dist[u] + w)) {
negCycleExists = true;
break;
}
}
if (negCycleExists) {
cout << "Negative Cycle Exists";
} else {
for (int i = 0; i < n; i++) {
cout << i << "=>" << dist[i] << endl;
}
}
}