Description has a tree with a number of N points, and the tree edge has edge rights. Give you a positive integer k within 0~ N, you want to select K points in this tree, dye it black, and turn the other n-k Dianran into white. After all points are dyed, you will get the distance between the black dots 22 plus the white Point 22 distance and the benefit. Ask what the maximum benefit is. Input
The first line consists of two integers N, K.
Next N-1 line three positive integer fr, to, dis, indicating that there is an edge (fr, to) in the tree with a length of dis. 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 Input3 1
1 2 1
1 3 2Sample Output3HINT
For 100% of the data, 0<=k<=n <=2000 This is a good question ... Set F[I][J] indicates that the maximum yield of J black Dots is chosen by the subtree of the root of I, which is calculated in advance. Then consider the sub-node V of U, in the subtree of v Select K, then F[u][j+k]=max (f[u][j]+f[v][k]) +v subtree in the black point to the outside of the black point distance from the white point of the +v subtree to the outside of the white point distance after the two Daoteng Daoteng formula on the line. Then note that this code is O (n^2) instead of O (n^3).
siz[x]=1if(to[i]!=FA) { dp (TO[I],X); Rep (J,0, Siz[x]) rep (K,0, siz[to[i]) update (f[x][j],f[to[i]][k]); SIZ[X]+ =siz[to[i]];}
View Code
Why is it? Consider that double loop, which can be seen as enumerating each point of the two subtrees trees separately. You will find that the point pair(u,v) , only in the calculationlCa(u,v) DP is only Considering that, so the complexity isO(n^2).
#include <cstdio>#include<cctype>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineDwn (i,s,t) for (int i=s;i>=t;i--)#defineren for (int i=first[x];i;i=next[i])using namespacestd;Const intBuffersize=1<< -;Charbuffer[buffersize],*head,*Tail;inlineCharGetchar () {if(head==tail) { intL=fread (Buffer,1, Buffersize,stdin); Tail= (Head=buffer) +M; } return*head++;} InlineintRead () {intx=0, f=1;CharC=GetChar (); for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;} typedefLong Longll;Constll inf=1ll<< -;Const intmaxn= .;intn,m,first[maxn],next[maxn<<1],to[maxn<<1],dis[maxn<<1],e;voidAddedge (intWintVintu) {to[++e]=v;dis[e]=w;next[e]=first[u];first[u]=e; to[++e]=u;dis[e]=w;next[e]=first[v];first[v]=e;} ll F[MAXN][MAXN],TMP[MAXN],SIZ[MAXN];voiddpintXintFA) {Siz[x]=1; renif(to[i]!=FA) {DP (TO[I],X); Rep (J,0, Siz[x]) rep (K,0, Siz[to[i]]) tmp[j+k]=max (tmp[j+k],f[x][j]+f[to[i]][k]+ (LL) dis[i]* (k* (m-k) + (siz[to[i]]-k) * (n-m-siz[to[i]]+k)); SIZ[X]+=Siz[to[i]]; Rep (J,0, Siz[x]) f[x][j]=tmp[j],tmp[j]=-inf; }}intMain () {n=read (); m=read (); Rep (I,2, N) Addedge (read (), read (), read ()); Rep (I,1, N) Rep (J,2, m) f[i][j]=-inf; Rep (I,0, N) tmp[i]=-inf; DP (1,0);p rintf ("%lld\n", f[1][m]); return 0;}
View Code
BZOJ4033: [Haoi2015]t1