-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeForces - F_Min_Path.cpp
93 lines (74 loc) · 2 KB
/
CodeForces - F_Min_Path.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <bits/stdc++.h>
#define MX vector<vector<T>>
#define MXLL vector<vector<long long>>
#define INF LLONG_MAX
#define MOD 1000000007
using namespace std;
template<typename T>
void print(MX a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a.size(); j++) {
if (a[i][j] == INF) cout << "INF ";
else cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
template<typename T>
MX mult(MX a, MX b) {
MX result(a.size(), vector<T>(a.size(), INF));
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a.size(); j++) {
T minPath = INF;
for (int k = 0; k < a.size(); k++) {
if (a[i][k] != INF && b[k][j] != INF)
minPath = min(minPath, (a[i][k]+b[k][j]));
}
if (minPath != INF) {
if (result[i][j] == INF) result[i][j] = minPath;
else result[i][j] = (result[i][j] + minPath);
}
}
}
return result;
}
map<long long, MXLL> mp;
MXLL binary(MXLL a, long long n) {
if (n == 1) return a;
long long nInit = n; // remember n for later
// search the cache
auto it = mp.find(n);
if (it != mp.end()) {
return it->second;
}
MXLL result;
if (n % 2 == 0) {
result = mult(binary(a, n/2), binary(a, n/2));
} else {
result = mult(binary(a, n-1), a);
}
mp[n] = result;
return result;
}
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<long long>> mat(n, vector<long long>(n, INF));
for (int i = 0; i < m; i++) {
int a, b, w;
cin >> a >> b >> w;
a--; b--;
mat[a][b] = w;
}
mat = binary(mat, k);
long long minPath = INF;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
minPath = min(minPath, mat[i][j]);
}
}
if (minPath == INF) cout << "IMPOSSIBLE" << endl;
else cout << minPath << endl;
return 0;
}