|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#include <ext/rope> |
| 3 | +using namespace std; |
| 4 | +using namespace __gnu_cxx; |
| 5 | + |
| 6 | +using ci = const int; |
| 7 | +using ld = long double; |
| 8 | +using llint = long long; |
| 9 | +using ullint = unsigned long long; |
| 10 | +using pii = pair <int,int>; |
| 11 | +using pcc = pair <char,char>; |
| 12 | +using pss = pair <string,string>; |
| 13 | +using vi = vector <int>; |
| 14 | +using vb = vector <bool>; |
| 15 | +using vii = vi::iterator; |
| 16 | + |
| 17 | +#define INF (1<<30) |
| 18 | +#define MOD 1000000007 |
| 19 | +#define mp make_pair |
| 20 | +#define mt make_tuple |
| 21 | +#define all(c) c.begin(), c.end() |
| 22 | +#define ms(name,val) memset(name, val, sizeof name) |
| 23 | +#define np nullptr |
| 24 | + |
| 25 | + |
| 26 | +const int MX = 100; |
| 27 | +int n, DP[MX][MX][MX], g[MX][MX]; |
| 28 | + |
| 29 | +int shortestPath(int a, int b, int k) |
| 30 | +{ |
| 31 | + for (int len = 0; len <= k; ++len) |
| 32 | + { |
| 33 | + for (int src = 0; src < n; ++src) |
| 34 | + { |
| 35 | + for (int dest = 0; dest < n; ++dest) |
| 36 | + { |
| 37 | + // initialize value |
| 38 | + DP[src][dest][len] = INF; |
| 39 | + |
| 40 | + // 1st base case |
| 41 | + if (!len && src == dest) |
| 42 | + DP[src][dest][0] = 0; |
| 43 | + |
| 44 | + // 2nd base case |
| 45 | + if (len == 1 && g[src][dest] != INF) |
| 46 | + DP[src][dest][1] = g[src][dest]; |
| 47 | + |
| 48 | + if (len < 2) continue; |
| 49 | + // now solve for general cases |
| 50 | + |
| 51 | + // adjacents of src |
| 52 | + for (int adj = 0; adj < n; ++adj) |
| 53 | + { |
| 54 | + if (adj != src && adj != dest && |
| 55 | + g[src][adj] != INF && DP[adj][dest][len-1] != INF) |
| 56 | + { |
| 57 | + DP[src][dest][len] = min(DP[src][dest][len], |
| 58 | + g[src][adj] + DP[adj][dest][len-1]); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return DP[a][b][k]; |
| 66 | +} |
| 67 | + |
| 68 | +int main() |
| 69 | +{ |
| 70 | + ios_base::sync_with_stdio(0); |
| 71 | + //cin.tie(0); |
| 72 | + |
| 73 | + int m; |
| 74 | + cin >> n >> m; |
| 75 | + |
| 76 | + for (int t1 = 0; t1 < n; ++t1) |
| 77 | + { |
| 78 | + for (int t2 = 0; t2 < n; ++t2) |
| 79 | + g[t1][t2] = INF; |
| 80 | + |
| 81 | + g[t1][t1] = 0; |
| 82 | + } |
| 83 | + |
| 84 | + while (m) |
| 85 | + { |
| 86 | + --m; |
| 87 | + int a, b, c; |
| 88 | + cin >> a >> b >> c; |
| 89 | + g[a][b] = c; |
| 90 | + } |
| 91 | + |
| 92 | + cout << shortestPath(0, n-1, n>>1) << '\n'; |
| 93 | + |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
0 commit comments