-
Notifications
You must be signed in to change notification settings - Fork 0
/
bellman_ford.cpp
55 lines (52 loc) · 1.56 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
#include <iostream>
#include <vector>
#include <cmath>
using std::vector;
int negative_cycle(vector<vector<int> > &adj, vector<vector<int> > &cost)
{
//write your code here
vector<double> dist(adj.size(), INFINITY);
dist[0] = 0;
bool relax = true;
for (int i = 1; i <= (adj.size() - 1); i++)
{
if (!relax)
break;
relax = false;
for (int j = 0; j < adj.size(); j++)
for (int k = 0; k < adj[j].size(); k++)
{
if (dist[adj[j][k]] > (dist[j] + cost[j][k])) //relax
{
dist[adj[j][k]] = (dist[j] + cost[j][k]);
relax = true;
}
else if ((dist[adj[j][k]] == INFINITY) && (dist[j] == INFINITY))
{
dist[adj[j][k]] = (cost[j][k]);
relax = true;
}
}
}
if (relax) // V-1 iteration result in relax, so possible negative cycle. Check in last iteration.
for (int j = 0; j < adj.size(); j++)
for (int k = 0; k < adj[j].size(); k++)
if (dist[adj[j][k]] > (dist[j] + cost[j][k])) //relax
return 1;
return 0;
}
int main()
{
int n, m;
std::cin >> n >> m;
vector < vector<int> > adj(n, vector<int>());
vector < vector<int> > cost(n, vector<int>());
for (int i = 0; i < m; i++)
{
int x, y, w;
std::cin >> x >> y >> w;
adj[x - 1].push_back(y - 1);
cost[x - 1].push_back(w);
}
std::cout << negative_cycle(adj, cost);
}