-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy path1197.cc
57 lines (53 loc) · 1.06 KB
/
1197.cc
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
// https://cses.fi/problemset/task/1197
#include <algorithm>
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
typedef long long ll;
typedef tuple<ll, ll, ll> e;
typedef vector<e> ve;
typedef vector<ll> vi;
const ll INF = 100000000000000000LL;
int main() {
int n, m;
cin >> n >> m;
ve e(m);
for (int i = 0; i < m; i++) {
ll a, b, c;
cin >> a >> b >> c;
e[i] = {a-1, b-1, c};
}
vi d(n), p(n);
for (int i = 0; i < n; i++) d[i] = INF;
int x;
for (int i = 0; i < n; i++) {
x = -1;
for (auto z:e) {
ll a, b, c;
tie(a, b, c) = z;
if (d[a] + c < d[b]) {
d[b] = d[a] + c;
p[b] = a;
x = b;
}
}
}
if (x == -1) cout << "NO\n";
else {
for (int i = 0; i < n; i++) x = p[x];
int o = x;
vi a(1, o);
do {
x = p[x];
a.push_back(x);
} while (x != o);
reverse(a.begin(), a.end());
cout << "YES\n";
for (int i = 0; i < a.size(); i++) {
cout << a[i] + 1;
if (i < a.size() - 1) cout << " ";
}
cout << endl;
}
}