# Gym 101655D Delta Quadrant 树形dp

https://codeforces.com/gym/101655/attachments

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i = (int)a;i<=(int)b;i++)
#define pb push_back
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int maxn=10005;
const int maxm=2*maxn;
typedef long long ll;
typedef pair<int,ll> pil;
const ll inf=1e18;
ll dp[maxn][25],ans;
vector<pil> ve[maxn];
int n,k,sz[maxn];
void init(){rep(i,0,n-1) ve[i].clear(); }
void dfs(int u,int fa){
sz[u]=1;
int f=0;
for(auto p:ve[u]){
int v=p.first; ll w=p.second;
if(v==fa) continue;
dfs(v,u);
sz[u]+=sz[v];
if(!f){
rep(i,0,k){
dp[u][i]=w+dp[v][i];
}
if(sz[v]<=k) dp[u][sz[v]]=0;
}
else{
ll tmp[25];
rep(i,0,k) tmp[i]=inf;
for(int i=0;i<=k;i++){
for(int j=0;j<=i;j++){
if(sz[v]>i-j){
tmp[i]=min(tmp[i],dp[u][j]+dp[v][i-j]+w);
}
else{
tmp[i]=min(dp[u][j],tmp[i]);
}
}
}
rep(i,0,k) dp[u][i]=tmp[i];
}
f=1;
}
rep(i,0,k){
if(dp[u][i]>=inf) dp[u][i]=0;
}
if(n-sz[u]<=k){
ans=min(ans,dp[u][k-(n-sz[u])]);
}
}
int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
init();
rep(i,2,n){
int u,v; ll va;
scanf("%d%d%lld",&u,&v,&va);
}
ans=inf;
rep(i,0,n-1) rep(j,0,k) dp[i][j]=inf;
dfs(0,-1);
printf("%lld\n",ans*2);
}
return 0;
}


03-21 68
05-22 356

03-12 193
12-03 536
03-23 426