-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbellman_ford.cpp
95 lines (82 loc) · 1.68 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Bellman Ford Algorithms (shortest path)
checks negative weight cycles
time complexity - O(V*E)
*/
#include <bits/stdc++.h>
using namespace std;
struct edge {
int u, v, w;
};
const int mx = 1e4;
const int inf = INT_MAX;
edge g[mx];
int nodes, edges, uu, vv, ww, dis[mx];
bool BellmanFord(int src) {
for (int i = 1; i <= nodes; i++) dis[i] = inf;
dis[src] = 0;
// relax all edges
for (int i = 1; i <= nodes; i++) {
for (int j = 1; j <= edges; j++) {
uu = g[j].u;
vv = g[j].v;
ww = g[j].w;
if (dis[uu] != inf && dis[uu] + ww < dis[vv]) {
dis[vv] = dis[uu] + ww;
}
}
}
// check negative-weight cycles
// return true if exist, false otherwise
for (int i = 1; i <= edges; i++) {
uu = g[i].u;
vv = g[i].v;
ww = g[i].w;
if (dis[uu] != inf && dis[uu] + ww < dis[vv]) return true;
}
return false;
}
int main() {
//freopen("in", "r", stdin);
scanf("%d %d", &nodes, &edges);
for (int i = 1; i <= edges; i++) {
scanf("%d %d %d", &uu, &vv, &ww);
g[i] = {uu, vv, ww};
}
int src_node = 1;
bool cycle = BellmanFord(src_node);
// print answer
if (cycle) printf("Negative weight cycle detected.");
else {
printf("u - v = w\n-----------\n");
for (int i = 1; i <= nodes; i++) {
printf("%d - %d = %d\n", src_node, i, dis[i]);
}
}
}
/*
Input1:
5 7
1 4 35
1 2 40
1 5 5
5 3 10
1 3 25
3 2 10
2 4 2
Output1:
u - v = w
-----------
1 - 1 = 0
1 - 2 = 25
1 - 3 = 15
1 - 4 = 27
1 - 5 = 5
Input2:
3 3
1 2 3
2 3 2
3 1 -10
Output:
Negative weight cycle detected.
*/