P2015
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 const int maxn=200; 7 struct edge{ 8 int to,nxt,dis; 9 }e[maxn]; 10 int n,m; 11 inline int read() 12 { 13 int x;char c=getchar(); 14 while(c<'0' or c>'9')c=getchar(); 15 x=c-'0',c=getchar(); 16 while(c>='0' and c<='9')x=x*10+c-'0',c=getchar(); 17 return x; 18 } 19 int head[maxn],ecnt,f[maxn][maxn]; 20 inline void addedge(int from,int to,int dis) 21 { 22 e[++ecnt]=(edge){to,head[from],dis},head[from]=ecnt; 23 } 24 int siz[maxn]; 25 void dfs(int x,int fa) 26 { 27 for(int i=head[x];i;i=e[i].nxt) 28 { 29 int u=e[i].to; 30 if(u==fa)continue; 31 dfs(u,x); 32 siz[x]+=siz[u]+1; 33 for(int j=min(siz[x],m);j;j--) 34 for(int k=min(siz[u],j-1);k>=0;k--) 35 f[x][j]=max(f[x][j],f[x][j-k-1]+f[u][k]+e[i].dis); 36 //j表示保留j时,k全部遍历所有情况 37 } 38 } 39 40 int main() 41 { 42 n=read(),m=read(); 43 for(int a,b,c,i=1;i<n;i++) 44 { 45 a=read(),b=read(),c=read(); 46 addedge(a,b,c);addedge(b,a,c); 47 } 48 dfs(1,0); 49 printf("%d",f[1][m]); 50 return 0; 51 }