Little R and B God is playing a game. The game's map consists of n-and N-1 non-edged edges, each of which connects two points, and the map is connected. In other words, the map of the game is a tree with n nodes.
In the game there is a prop called the Scout Guard, and when a player places a scout guard at a point, it can monitor the point and all points within D from the point. The distance between the two points is defined as their distance on the tree, which is the number of edges passing through the only simple path between two points. It takes a certain price to place the Scout guard at one point, and the cost of placing the guard at a different point may be different.
Now that little R knows where all B-gods might appear, please calculate the minimum cost of monitoring all these locations.
Solution
Divine questions.
Notice that D is not very large, so you can design a NK state: Dp[i][j] indicates that the sub-tree of the root of I has been processed, and it can cover the minimum cost of J points upwards.
However, there is also the possibility that the sub-trees will cover each other, so we use f[i][j] to indicate that the subtree with the root of I is down and the minimum cost of J points is not covered.
Transfer:
Consider how the DP array is transferred.
Dp[i][j]<-min (Dp[i][j]+dp[v][j] (I just can cover J), Dp[v][j+1]+f[u][j+1]);
It is equivalent to merging V into the current subtree.
The F array can accumulate the answer directly, f[i][j]+=f[v][j-1].
Finally combined with the greedy thought, for the F array, J bigger answer should be smaller, for the DP array, J Smaller answer should also be smaller, after finished take Max.
Then pay attention to the border, Dp[u][0]=0.dp[u][~]=w[u]. When Vis[u]=1 F[u][0]=dp[u][0]=w[u];
Code
#include <iostream>#include<cstdio>#defineN 500003#defineINF 0x3f3f3f3fusing namespacestd;intx,y,w[n],head[n],tot,f[n][ A],d,dp[n][ A],n,m,tag[n];structzzh{intN,to;} E[n<<1];inlinevoidAddintUintv) {e[++tot].n=Head[u]; E[tot].to=v; Head[u]=tot;}voidDfsintUintFA) { if(Tag[u]) dp[u][0]=f[u][0]=W[u]; for(intI=1; i<=d;++i) dp[u][i]=W[u]; Dp[u][d+1]=inf; for(intI=HEAD[U];I;I=E[I].N)if(e[i].to!=FA) { intv=e[i].to; DFS (V,U); for(intj=d;j>=0;--j) Dp[u][j]=min (dp[u][j]+f[v][j],dp[v][j+1]+f[u][j+1]); for(intj=d;j>=0;--j) Dp[u][j]=min (dp[u][j],dp[u][j+1]); f[u][0]=dp[u][0]; for(intj=1; j<=d+1; ++j) f[u][j]+=f[v][j-1]; for(intj=1; j<=d+1; ++j) F[u][j]=min (f[u][j],f[u][j-1]); }}inlineintRd () {intx=0;BOOLf=0;CharC=GetChar (); while(!IsDigit (c)) { if(c=='-') f=1; C=GetChar (); } while(IsDigit (c)) {x= (x<<1) + (x<<3) + (c^ -); C=GetChar (); } returnf?-x:x;}intMain () {n=rd ();d =Rd (); for(intI=1; i<=n;++i) w[i]=Rd (); M=Rd (); for(intI=1; i<=m;++i) X=rd (), tag[x]=1; for(intI=1; i<n;++i) X=rd (), y=Rd (), add (x, y), add (y,x); DFS (1,0); cout<<f[1][0]; return 0; }
[jloi2016/shoi2016] Reconnaissance guard (tree DP)