-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph Paths II.cpp
More file actions
executable file
·65 lines (60 loc) · 1.08 KB
/
Copy pathGraph Paths II.cpp
File metadata and controls
executable file
·65 lines (60 loc) · 1.08 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
65
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 1e18L + 5;
class Matrix {
public:
vector<vector<LL>> t;
Matrix(int n) {
t.assign(n, vector<LL>(n));
}
};
Matrix minmul(Matrix &A, Matrix &B, int n) {
Matrix C(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
C.t[i][j] = INF;
for (int k = 0; k < n; k++) {
C.t[i][j] = min(C.t[i][j], A.t[i][k] + B.t[k][j]);
}
}
}
return C;
}
Matrix minpow(Matrix A, LL p, int n) {
Matrix res(n);
bool st = true;
while (p) {
if (p & 1) {
if (st) {
st = false;
res = A;
} else {
res = minmul(A, res, n);
}
}
A = minmul(A, A, n);
p >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
int n, m, k;
cin >> n >> m >> k;
Matrix A(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A.t[i][j] = INF;
}
}
for (int i = 0; i < m; i++) {
int a, b;
LL c;
cin >> a >> b >> c;
A.t[a - 1][b - 1] = min(A.t[a - 1][b - 1], c);
}
LL res = minpow(A, k, n).t[0][n - 1];
cout << (res >= INF ? -1 : res) << "\n";
return 0;
}