forked from codersinghal/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bellman_ford.cpp
63 lines (55 loc) · 1.19 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
/**
* 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
*/
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;
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;
}
}
for (int i = 0; i < n; i++) {
cout << i << " " << dist[i] << endl;
}
}