bzoj4033- tree dp

. 1 #include <the iostream>
 2 #include <cstdio>
 . 3 #include <the cmath>
 . 4 #include < String >
 . 5 #include <CString>
 . 6 #include <algorithm>
 . 7 #include <The iomanip>
 . 8  the using  namespace STD;
 . 9  // F [i] [t] i is expressed in root subtree Black dots t benefits
 10  // consider contributions calculated for each edge: there are several points on the path are also included this edge.
. 11  // F [X] [J + T] = max (F [X] [J + T], F [X] [J] + F [Y] [T] + (Long Long) Edge [I]. * DIS (T * (KT) + (size [Y] -t) * (NK- (size [Y] -t))));
 12 is  // updated size [X] 
13 is  namespace Moxing {
 14     const int N=2005;
15     int n,k,last[N],cnt,size[N],son[N<<1];
16     long long f[N][N];
17     struct node{
18         int to,dis,nxt;
19     }edge[N<<1];
20     void add(int from,int to,int dis){
21         edge[++cnt].to=to,edge[cnt].dis=dis,edge[cnt].nxt=last[from],last[from]=cnt;
22     }
23     void dfs(int x,int fa){
24     //    f[x][0]=f[x][1]=0;
25         size[x]=1;
26         for(int i=last[x];i;i=edge[i].nxt){
27             int y=edge[i].to;
28             if(y==fa) continue ;
29             dfs(y,x);
30             for(int j=min(k,size[x]);j>=0;j--){
31                 for(int t=min(k-j,size[y]);t>=0&&(j+t)<=k;t--){// j+t<=k -> t<=k-j
32                     f[x][j+t]=max(f[x][j+t],f[x][j]+f[y][t]+(long long)edge[i].dis*(t*(k-t)+(size[y]-t)*(n-k-(size[y]-t))));
33                 }
34             }
35             size[x]+=size[y];
36         }
37     } 
38     struct main{a
39         main(){
40             scanf("%d%d",&n,&k);
41             for(int i=1;i<=n-1;i++){
42                 int from,to,dis;
43                 scanf("%d%d%d",&from,&to,&dis);
44                 add(from,to,dis),add(to,from,dis);
45             }
46             dfs(1,1);
47             printf("%lld\n",f[1][k]);
48             exit(0);
49         }
50     }UniversalLove;
51 } 
52 int main(){
53     Moxing::main();
54 }
View Code

 

Guess you like

Origin www.cnblogs.com/Moxingtianxia/p/11360384.html