-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnegative_cycle.cpp
More file actions
52 lines (43 loc) · 1.35 KB
/
Copy pathnegative_cycle.cpp
File metadata and controls
52 lines (43 loc) · 1.35 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
#include <iostream>
#include <vector>
#include<cstdlib>
#include<limits.h>
using namespace std;
struct node
{
vector<int> children;
vector<int> cost;
};
bool negative_cycle(const vector<node> graph)
{
vector<int> dist(graph.size(), (int)(INT_MAX/10)*8);
dist[0] = 0;
for(int k=0; k<graph.size(); k++)
for(int i=0; i<graph.size(); i++)
for(int j=0; j<graph[i].children.size(); j++)
{
if(dist[graph[i].children[j]] > dist[i]+graph[i].cost[j] && dist[i]!= (int)(INT_MAX/10)*8)
dist[graph[i].children[j]] = dist[i]+graph[i].cost[j];
if(dist[graph[i].children[j]]!= (int)(INT_MAX/10)*8 && dist[i]== (int)(INT_MAX/10)*8)
dist[i] = dist[graph[i].children[j]] - graph[i].cost[j];
}
for(int i=0; i<graph.size(); i++)
for(int j=0; j<graph[i].children.size(); j++)
if(dist[graph[i].children[j]] > dist[i]+graph[i].cost[j] && dist[i]!=(int)(INT_MAX/10)*8)
return true;
return false;
}
int main()
{
int n, m;
cin >> n >> m;
vector<node> graph(n);
for (int i = 0; i < m; i++)
{
int x, y, w;
cin >> x >> y >> w;
graph[x-1].children.push_back(y-1);
graph[x-1].cost.push_back(w);
}
cout << negative_cycle(graph);
}