Dp[u][k1]=min (dp[u][k1-k2]+dp[v][k2]+edge*k2* (K-K2))
Dp[u][k] represents the optimal solution for the K nodes in the subtree of the U node as the root node
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace Std;
const int maxn=2010;
Const __int64 INF=1E18;
int Nedge;
struct node
{
int u,v,w,next;
}EDGE[2*MAXN];
int HEAD[MAXN];
__int64 DP[MAXN][MAXN];
int n,k;
void Addedge (int u,int v,int W)
{
Edge[nedge].u=u;
Edge[nedge].v=v;
Edge[nedge].w=w;
Edge[nedge].next=head[u];
head[u]=nedge++;
}
void Dfs (int u,int pre)
{
int i;
dp[u][1]=0;
for (I=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
int W=EDGE[I].W;
if (v==pre) continue;
DFS (V,U);
int k1,k2;
for (k1=k;k1>=0;k1--)
{
for (k2=1;k2<=k1;k2++)
{
Dp[u][k1]=min (dp[u][k1],dp[u][k1-k2]+dp[v][k2]+w*k2* (K-K2));
}
}
}
}
void Init ()
{
int i,j;
memset (head,-1,sizeof (head));
memset (Edge,0,sizeof (Edge));
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
Dp[i][j]=inf;
nedge=0;
}
int main ()
{
Freopen ("In.txt", "R", stdin);
Freopen ("OUT.txt", "w", stdout);
int T;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d", &n,&k);
Init ();
int a,b,c;
n--;
while (n--)
{
scanf ("%d%d%d", &a,&b,&c);
Addedge (A,B,C);
Addedge (B,A,C);
}
DFS (1,0);
printf ("%i64d\n", Dp[1][k]);
}
return 0;
}
HDU 5148Cities Tree DP