-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnegative_cycles.cpp
More file actions
62 lines (55 loc) · 1.4 KB
/
negative_cycles.cpp
File metadata and controls
62 lines (55 loc) · 1.4 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;
#define MAXN 10000
#define INF 100000001
int n;
int m;
int dist[MAXN];
vector<tuple<int, int, int>> edges;
bool negative_cycle = false;
//UTILIZA O ALGORITMO DE BELLMAN FORD, QUE APÓS BUSCAR O MENOR CAMINHO DE UM DADO VERTICE PARA OUTROS, FAZ A VERIFICACAO SE EXISTE UM CICLO NEGATIVO
void bellman_ford(int x)
{
for (int i = 1; i <= n; i++)
dist[i] = INF;
dist[x] = 0;
// int dist_prev = dist[x];
for (int i = 0; i < n; i++)
{
for (auto e : edges)
{
int a, b, w;
tie(a, b, w) = e;
dist[b] = min(dist[b], dist[a] + w);
}
// DETECTA SE TEM UM CICLO NEGATIVO
for (int i = 0; i < (int)edges.size(); i++)
{
int a, b, w;
// coloca os valores da tupla nas respectivas variáveis em ordem
tie(a, b, w) = edges[i];
if (dist[b] > dist[a] + w)
{
negative_cycle = true;
break;
}
}
}
}
int main()
{
int s;
cin >> n >> m >> s;
for (int i = 0; i < m; i++)
{
int u, v, w;
// Lê a descrição de uma aresta
cin >> u >> v >> w;
edges.push_back({u, v, w});
edges.push_back({v, u, w});
}
// verifica se temos um ciclo negativo
bellman_ford(s);
if (negative_cycle)
cout << "Negative Cycle\n";
}