-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBellman_Ford.cpp
More file actions
62 lines (60 loc) · 1.13 KB
/
Bellman_Ford.cpp
File metadata and controls
62 lines (60 loc) · 1.13 KB
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
#include<bits/stdc++.h>
using namespace std;
//works on directed graph with no negative cycle
//can detect negative cycle
//works on unweighted graph with no negative edge coz unweighted edge eg A - B can be converted
//to A->B and B->A
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("opts.txt", "w", stdout);
#endif
int n, m;
cin >> n >> m;
vector<pair<int, int>> adj[n];
vector<array<int, 3>> edges;
for (int i = 0; i < m; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
edges.push_back({u, v, w});
}
int dist[n];
for (int i = 0; i < n; i++)
{
dist[i] = INT_MAX;
}
dist[0] = 0; //distance of src is 0
for (int i = 0; i < n - 1; i++)
{
for (auto x : edges)
{
if (dist[x[0]] + x[2] < dist[x[1]])
{
dist[x[1]] = dist[x[0]] + x[2];
}
}
}
bool f = false;
for (auto x : edges)
{
if (dist[x[0]] + x[2] < dist[x[1]])
{
f = true;
dist[x[1]] = dist[x[0]] + x[2];
}
}
if (f)
{
cout << "Negative cycle detected" << endl;
}
else
{
for (int i = 0; i < n; i++)
{
cout << "Distance from " << 0 << " to " << i << " is : " << dist[i] << endl;
}
}
}