-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphPaths2.cpp
More file actions
64 lines (57 loc) · 1.56 KB
/
GraphPaths2.cpp
File metadata and controls
64 lines (57 loc) · 1.56 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
//Graph Paths I - https://cses.fi/problemset/task/1724
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18 + 170;
vector<vector<ll>> matrix_mult(vector<vector<ll>>& A, vector<vector<ll>>& B) {
int a_rows = (int) A.size(), a_cols = (int) A[0].size();
int b_rows = (int) B.size(), b_cols = (int) B[0].size();
assert(a_cols == b_rows);
vector<vector<ll>> C(a_rows, vector<ll>(b_cols, INF));
for (int i = 0; i < a_rows; i++) {
for (int j = 0; j < b_cols; j++) {
for (int k = 0; k < a_cols; k++) {
if (A[i][k] == INF || B[k][j] == INF) continue;
C[i][j] = min(C[i][j], A[i][k] + B[k][j]);
}
}
}
return C;
}
vector<vector<ll>> matrix_expo(vector<vector<ll>>& A, ll b) {
bool res_is_id = true;
vector<vector<ll>> res;
while (b > 0) {
if (b & 1) {
res = res_is_id ? A : matrix_mult(res, A);
res_is_id = false;
}
A = matrix_mult(A, A);
b >>= 1;
}
return res;
}
void solve() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<ll>> adj(n, vector<ll>(n, INF));
for (int i = 0; i < m; i++) {
ll u, v, w;
cin >> u >> v >> w;
u--, v--;
adj[u][v] = min(adj[u][v], w);
}
ll ans = matrix_expo(adj, k)[0][n - 1];
cout << (ans < INF ? ans : -1) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}