思路:
树形dp + 分组背包dp。
参考https://www.cnblogs.com/kuangbin/archive/2012/08/29/2661928.html
实现:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii; const int MAXN = ;
vector<pii> G[MAXN];
int dp[MAXN][], n, s, k; void dfs(int u, int f)
{
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i].first, w = G[u][i].second;
if (v == f) continue;
dfs(v, u);
for (int j = k; j >= ; j--)
{
dp[u][j] += dp[v][] + * w;
for (int l = ; l <= j; l++)
{
dp[u][j] = min(dp[u][j], dp[v][l] + l * w + dp[u][j - l]);
}
}
}
} int main()
{
while (scanf("%d %d %d", &n, &s, &k) != EOF)
{
memset(dp, , sizeof dp);
for (int i = ; i <= n; i++) G[i].clear();
int a, b, w;
for (int i = ; i < n - ; i++)
{
scanf("%d %d %d", &a, &b, &w);
G[a].push_back(pii(b, w));
G[b].push_back(pii(a, w));
}
dfs(s, );
printf("%d\n", dp[s][k]);
}
return ;
}