4557: [JLoi2016] reconnaissance guard Time limit: Sec Memory Limit: + MB
Submit: Solved: 70
[Submit] [Status] [Discuss] Description 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. The first line of input contains two positive integers n and D, each representing the number of points on the map and the field of view of the Scout Guard. The points on the contract map are numbered with integers 1 through n. The second row n positive integers, and the first I positive integers represent the cost of placing the Scout Guard at the point numbered I wi. Guaranteed wi≤1000. The third line is a positive integer m, which represents the number of points that B God may appear. Guaranteed M≤n. The four m positive integers, respectively, represent the number of points that each B God may appear, given from small to large. Next n–1 line, each line contains two positive integer u,v, which indicates that there is an no-forward edge between the point numbered U and the point with the number V. N<=500000,d<=20output
Only one line of integers, which represents the minimum cost of monitoring all possible points that a god may have
Sample Input12 2
8 9 12 6 1 1 5 1 4 8 10 6
10
1 2 3 5 6 7 8 9 10 11
1 3
2 3
3 4
4 5
4 6
4 7
7 8
8 9
9 10
10 11
11 12Sample OutputTen
Tree-shaped DP
F[I][J] represents the minimum cost of the sub-tree of I, which covers up to i down the J layer.
G[I][J] indicates that the subtree of I is all covered and can cover the minimum cost of the J layer upwards.
The transfer is troublesome, the relevant comments are written in the code.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring > #include <algorithm> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) # Define ll long Long#define N 500005#define inf 1000000000using namespace Std;int n,m,d,cnt;int W[n],head[n],f[n][25],g[n] [25];bool mark[n];struct edge{int next,to;} E[n*2];inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline void Add_edge (int x,int y) {e[++cnt]= (edge) {head[x],y};head[x]=cnt;e[++cnt]= (edge) {head[y],x};head[y]=cnt;} void dp (int x,int FA) {if (mark[x]) f[x][0]=g[x][0]=w[x];//This position must be put F (i,1,d) g[x][i]=w[x];//Initial state is assumed X position is placed guard, then may be updated g[x] [d+1]=inf;//is not possible to overwrite d+1 layer with points in x subtree, so equals inf for (int i=head[x];i;i=e[i].next) {int y=e[i].to;if (Y==FA) CONTINUE;DP (y,x) ;D(j,d,0) g[x][j]=min (g[x][j]+f[y][j],g[y][j+1]+f[x][j+1]);//The Guardians in the subtree of y may spend moreLess D (j,d,0) g[x][j]=min (g[x][j],g[x][j+1]);//with g[x][j+1] to update g[x][j] f[x][0]=g[x][0];//here is equivalent to neither upward nor downward expansion f (j,1,d+1) f[x ][j]+=f[y][j-1];//directly plus can f (j,1,d+1) F[x][j]=min (F[x][j-1],f[x][j]);//f[x][j-1] to update f[x][j]}}int main () {N=read ();d =read (); F (I,1,n) w[i]=read (); M=read (); F (i,1,m) {int x=read (); mark[x]=true;} F (i,1,n-1) {int x=read (), Y=read (); Add_edge (x, y);} DP (1,0);p rintf ("%d\n", F[1][0]); return 0;}
bzoj4557 "JZOI2016" reconnaissance Guard