-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathGraph Paths II.cpp
51 lines (41 loc) · 1.15 KB
/
Graph Paths II.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
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxN = 100;
const ull INF = 1e19;
int N, M, K, a, b;
ull c, X[maxN][maxN], Y[maxN][maxN];
void combine(ull A[maxN][maxN], ull B[maxN][maxN]){
ull C[maxN][maxN];
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
C[i][j] = INF;
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
for(int k = 0; k < N; k++)
if(A[i][k] != INF && B[k][j] != INF)
C[i][j] = min(C[i][j], A[i][k] + B[k][j]);
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
A[i][j] = C[i][j];
}
int main(){
scanf("%d %d %d", &N, &M, &K);
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++)
X[i][j] = Y[i][j] = INF;
Y[i][i] = 0;
}
for(int i = 0; i < M; i++){
scanf("%d %d %llu", &a, &b, &c);
X[a-1][b-1] = min(X[a-1][b-1], c);
}
while(K){
if(K&1)
combine(Y, X);
combine(X, X);
K >>= 1;
}
if(Y[0][N-1] == INF) printf("-1\n");
else printf("%llu\n", Y[0][N-1]);
}