From 3266ee94ccc982cb1821111ee84e215196be8f50 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 19 Feb 2023 10:20:14 +0800 Subject: [PATCH] =?UTF-8?q?P3354=20[IOI2005]Riv=20=E6=B2=B3=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/102483854 --- Luogu/P3354/P3354.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Luogu/P3354/P3354.cpp diff --git a/Luogu/P3354/P3354.cpp b/Luogu/P3354/P3354.cpp new file mode 100644 index 00000000..44e7d23a --- /dev/null +++ b/Luogu/P3354/P3354.cpp @@ -0,0 +1,63 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 105; + +int n, k, w[N], v[N], d[N], dis[N]; +int f[N][2][N][N]; +std::vector g[N], st; + +void dfs(int u) { + st.emplace_back(u); + + for (int v : g[u]) { + dis[v] = dis[u] + d[v]; + + dfs(v); + + for (int t : st) { + for (int i = k; i >= 0; i--) { + f[u][0][t][i] += f[v][0][t][0]; + f[u][1][t][i] += f[v][0][u][0]; + + for (int j = 1; j <= i; j++) { + f[u][0][t][i] = std::min(f[u][0][t][i], f[v][0][t][j] + f[u][0][t][i - j]); + f[u][1][t][i] = std::min(f[u][1][t][i], f[v][0][u][j] + f[u][1][t][i - j]); + } + } + } + } + + for (int t : st) { + for (int i = k; i; i--) { + f[u][0][t][i] = std::min(f[u][0][t][i] + w[u] * (dis[u] - dis[t]), f[u][1][t][i - 1]); + } + + f[u][0][t][0] += w[u] * (dis[u] - dis[t]); + } + + st.pop_back(); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> k; + + for (int i = 1; i <= n; i++) { + cin >> w[i] >> v[i] >> d[i]; + + g[v[i]].emplace_back(i); + } + + dfs(0); + + cout << f[0][0][0][k] << endl; + + return 0; +}