4033: [Haoi2015]t1
Time Limit:10 Sec Memory limit:256 MB
submit:226 solved:111
[Submit] [Status] [Discuss]
Description
There is a tree with a number of N, and the edge of the tree is right. Give you a positive integer within 0~ N
K, you have to choose K dots in this tree, dye it black, and put the other
N-k a Dianran into white. After all points are dyed, you will get the distance between the black dots 22
And the benefit of the distance plus the white point 22. Ask what the maximum benefit is.
Input
The first line consists of two integers N, K.
Next N-1 line three positive integers per line fr, to, dis, indicating that there is a length in the tree
For the side of the dis (fr, to). Enter to ensure that all points are connected to each other.
Output
Outputs a positive integer that represents the maximum value of the benefit.
Sample Input
3 1
1 2 1
1 3 2
Sample Output
3
HINT
For 100% of data, 0<=k<=n <=2000
The main point: we can easily think of using f[i][j] to indicate that I node of the subtree of K has been dyed black when the maximum benefit.
But we can only handle the situation in the subtree, there is no way out of the subtree, so we need to redefine the state.
F[I][J] is a contribution to the answer, so we just need to multiply the number of black dots and white dots in the subtree, and then take the right value on the road.
#include <iostream>#include <cstdio>#include <cstring>#define LL Long Longusing namespace STD;Const intn=2100;intn,k,tot=1, point[n],next[n*4],siz[n];Long LongF[n][n];structs{intSt,en,va;} aa[n*4];inline voidAddintXintYintZ) {tot+=1; next[tot]=point[x];p Oint[x]=tot; Aa[tot].st=x;aa[tot].en=y;aa[tot].va=z; tot+=1; next[tot]=point[y];p Oint[y]=tot; Aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;}inline voiddpintXintFA) {inti,j,u,l; siz[x]=1; f[x][0]=f[x][1]=0; for(I=point[x];i;i=next[i])if(AA[I].EN!=FA) {u=aa[i].en; DP (U,X); Siz[x]+=siz[u]; for(j=siz[x];j>=0;--j) for(l=0; L<=min (siz[u],j); ++l) F[x][j]=max (f[x][j],f[x][j-l]+ (LL) (l* (K-L) + (LL) (siz[u]-l) * (LL) (n-k-(SIZ[U]-L)) * (LL ) aa[i].va+f[u][l]); }}intMain () {intI,j,x,y,z;scanf("%d%d", &n,&k);memset(F, -,sizeof(f)); for(i=1; i<n;++i) {scanf("%d%d%d", &x,&y,&z); Add (x, y, z); } DP (1,0);cout<<f[1][k]<<endl;}
"HAOI2015" "bzoj4033" "T1"