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
| #include <bits/stdc++.h> #define LL long long using namespace std; const int N = 5e4 + 5, M = 1.5e5 + 5, K = 10 + 5; int n, m, k; LL f[N][K]; bool vis[N][K]; struct Edge { int ne, ver; LL w; } e[M]; int h[N], idx; void add(int x, int y, LL z) { e[idx] = (Edge){h[x], y, z}, h[x] = idx++; } void dp(int x, int j) { if (vis[x][j]) return ; for (int i = h[x], y; i != -1; i = e[i].ne) { y = e[i].ver; dp(y, j); f[x][j] = max(f[y][j] + e[i].w, f[x][j]); } if (j) for (int i = h[x], y; i != -1; i = e[i].ne) { y = e[i].ver; dp(y, j - 1); f[x][j] = min(f[y][j - 1] + e[i].w, f[x][j]); } vis[x][j] = true; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) h[i] = -1; idx = 0; int u, v; LL w; for (int i = 1; i <= m; ++i) { scanf("%d%d%lld", &u, &v, &w); add(u, v, w); } f[n][0] = 0; vis[n][0] = true; dp(1, k); printf("%lld\n", f[1][k]); return 0; }
|