-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycle Finding.cpp
More file actions
executable file
·64 lines (61 loc) · 1.1 KB
/
Copy pathCycle Finding.cpp
File metadata and controls
executable file
·64 lines (61 loc) · 1.1 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
63
64
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
vector<tuple<LL, LL, LL> > edges;
LL odl[5005];
LL pr[5005];
int n, m;
void bellman_ford() {
for (int i = 0; i <= n; i++) odl[i] = INT_MAX;
for (int i = 0; i <= n; i++) {
for (auto e : edges) {
auto& [a, b, c] = e;
if (odl[b] > odl[a] + c) {
odl[b] = odl[a] + c;
pr[b] = a;
}
}
}
bool n_cycle = false;
LL start;
for (auto e : edges) {
auto& [a, b, c] = e;
if (odl[b] > odl[a] + c) {
n_cycle = true;
start = b;
break;
}
}
if (n_cycle) {
cout << "YES\n";
vector<LL> res;
set<LL> s;
LL x = start;
do {
res.push_back(x);
s.insert(x);
x = pr[x];
} while (s.find(x) == s.end());
res.push_back(x);
reverse(res.begin(), res.end());
for (unsigned i = 0; i < res.size(); i++) {
cout << res[i] << " ";
if (i > 0 && res[i] == res[0]) {
break;
}
}
} else {
cout << "NO\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++) {
LL a, b, c;
cin >> a >> b >> c;
edges.push_back(make_tuple(a, b, c));
}
bellman_ford();
return 0;
}