-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1197.cpp
47 lines (45 loc) · 944 Bytes
/
1197.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
#include <bits/stdc++.h>
using namespace std;
const int N = 2505;
int n, m, p[N];
long long dist[N];
vector<tuple<int, int, int>> edges;
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int u, v, c; m--;) {
cin >> u >> v >> c;
edges.emplace_back(u, v, c);
}
fill(dist + 1, dist + 1 + n, 1e18);
dist[1] = 0;
int x = -1;
for (int i = 0; i < n; ++i) {
x = -1;
for (auto [u, v, c]: edges) {
if (dist[v] > dist[u] + c) {
dist[v] = dist[u] + c;
p[v] = u;
x = v;
}
}
}
if (x == -1) {
cout << "NO\n";
} else {
cout << "YES\n";
for (int i = 0; i < n; ++i) {
x = p[x];
}
vector<int> path;
do {
path.emplace_back(x);
x = p[x];
} while (path.size() < 2 || path.back() != path[0]);
reverse(path.begin(), path.end());
for (int u: path) {
cout << u << " ";
}
}
return 0;
}