From 242cc11b488821da9f3a82cbe03b101cc18ea392 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 8 Jun 2022 10:49:15 +0800 Subject: [PATCH] =?UTF-8?q?P4362=20[NOI2002]=20=E8=B4=AA=E5=90=83=E7=9A=84?= =?UTF-8?q?=E4=B9=9D=E5=A4=B4=E9=BE=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/77088166 --- Luogu/P4362/P4362.cpp | 72 ++++++++++++++++++++++++++++++++++++ Luogu/P4362/data/P4362_3.in | 3 ++ Luogu/P4362/data/P4362_3.out | 3 ++ 3 files changed, 78 insertions(+) create mode 100644 Luogu/P4362/P4362.cpp create mode 100644 Luogu/P4362/data/P4362_3.in create mode 100644 Luogu/P4362/data/P4362_3.out diff --git a/Luogu/P4362/P4362.cpp b/Luogu/P4362/P4362.cpp new file mode 100644 index 00000000..953efc1d --- /dev/null +++ b/Luogu/P4362/P4362.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 305; + +// f[子树根节点][多少个果子是被大头吃的][是否是大头吃掉了这个果子] +int n, m, k, f[N][N][2], f2[N][2]; +std::vector> g[N]; + +void dfs(int u, int fa) { + f[u][0][0] = f[u][1][1] = 0; + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (v == fa) continue; + dfs(v, u); + + memcpy(f2, f[u], sizeof(f[u])); + memset(f[u], 0x3f, sizeof(f[u])); + + for (int i = 0; i <= k; i++) { + for (int j = 0; j <= i; j++) { + f[u][i][0] = std::min(f[u][i][0], f2[i - j][0] + f[v][j][1]); + + if (m == 2) { + f[u][i][0] = std::min(f[u][i][0], f2[i - j][0] + f[v][j][0] + w); + } else { + f[u][i][0] = std::min(f[u][i][0], f2[i - j][0] + f[v][j][0]); + } + + f[u][i][1] = std::min({f[u][i][1], f2[i - j][1] + f[v][j][1] + w, f2[i - j][1] + f[v][j][0]}); + } + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m >> k; + + if (n - k < m - 1) { + cout << -1 << endl; + + exit(0); + } + + for (int i = 1, u, v, w; i < n; i++) { + cin >> u >> v >> w; + + g[u].push_back(std::make_pair(v, w)); + g[v].push_back(std::make_pair(u, w)); + } + + memset(f, 0x3f, sizeof(f)); + + dfs(1, 0); + + cout << f[1][k][1] << endl; + + return 0; +} diff --git a/Luogu/P4362/data/P4362_3.in b/Luogu/P4362/data/P4362_3.in new file mode 100644 index 00000000..ded050e1 --- /dev/null +++ b/Luogu/P4362/data/P4362_3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ba438d2f29f92006dffacfe92108a5d3ba75d295d24710b07aa3c211d95c3ed3 +size 274 diff --git a/Luogu/P4362/data/P4362_3.out b/Luogu/P4362/data/P4362_3.out new file mode 100644 index 00000000..d86e9e90 --- /dev/null +++ b/Luogu/P4362/data/P4362_3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b3e58b2a0add30249efe38cfe9955d765d8a0a882b239ed208f16e38db62a00c +size 4