-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnegativeCycle.cpp
More file actions
41 lines (32 loc) · 966 Bytes
/
negativeCycle.cpp
File metadata and controls
41 lines (32 loc) · 966 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int oo = 1e9+7;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
int main() {
int n = 10; //n-vertices
vector<vii> AdjList(n);
vi dist(n, oo); dist[0] = 0;
//build dist table O(VE)
for(int i = 0; i < n-1; ++i) {
for(int u = 0; u < n; ++u) {
for(int j = 0; j < (int)AdjList[u].size(); ++j) {
ii v = AdjList[u][j];
dist[v.first] = min(dist[v.first], dist[u]+v.second);
}
}
}
//check for negative cycle O(E)
bool hasNegativeCycle = false;
for(int u = 0; u < n; ++u) {
for(int j = 0; j < (int)AdjList[u].size(); ++j) {
ii v = AdjList[u][j];
if(dist[v.first] > dist[u] + v.second) {
//still not relaxed yet, so negative cycle
hasNegativeCycle = true;
}
}
}
return 0;
}