zoukankan      html  css  js  c++  java
  • [BZOJ4557][JLOI2016]侦查守卫

    4557: [JLoi2016]侦察守卫

    Time Limit: 20 Sec  Memory Limit: 256 MB
    Submit: 297  Solved: 200
    [Submit][Status][Discuss]

    Description

    小R和B神正在玩一款游戏。这款游戏的地图由N个点和N-1条无向边组成,每条无向边连接两个点,且地图是连通的
    。换句话说,游戏的地图是一棵有N个节点的树。游戏中有一种道具叫做侦查守卫,当一名玩家在一个点上放置侦
    查守卫后,它可以监视这个点以及与这个点的距离在D以内的所有点。这里两个点之间的距离定义为它们在树上的
    距离,也就是两个点之间唯一的简单路径上所经过边的条数。在一个点上放置侦查守卫需要付出一定的代价,在不
    同点放置守卫的代价可能不同。现在小R知道了所有B神可能会出现的位置,请你计算监视所有这些位置的最小代价

    Input

    第一行包含两个正整数N和D,分别表示地图上的点数和侦查守卫的视野范围。约定地图上的点用1到N的整数编号。
    第二行N个正整数,第i个正整数表示在编号为i的点放置侦查守卫的代价Wi。保证Wi≤1000。第三行一个正整数M,
    表示B神可能出现的点的数量。保证M≤N。第四行M个正整数,分别表示每个B神可能出现的点的编号,从小到大不
    重复地给出。接下来N–1行,每行包含两个正整数U,V,表示在编号为U的点和编号为V的点之间有一条无向边。N<=
    500000,D<=20

    Output

     仅一行一个整数,表示监视所有B神可能出现的点所需要的最小代价

    Sample Input

    12 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 12

    Sample Output

    10
     
     
     
    题解:树形DP
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<string>
    #include<cmath>
    #include<ctime>
    #include<algorithm>
    #include<map>
    #include<set>
    #include<queue>
    #include<iomanip>
    using namespace std;
    #define ll long long
    #define db double 
    #define up(i,j,n) for(int i=j;i<=n;i++)
    #define pii pair<int,int>
    #define uint unsigned int
    #define FILE "dealing"
    #define eps 1e-4
    int read(){
    	int x=0,f=1,ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    	return x*f;
    }
    template<class T> bool cmax(T& a,T b){return a<b?a=b,true:false;}
    template<class T> bool cmin(T& a,T b){return a>b?a=b,true:false;}
    const int maxn=505000,limit=50100,inf=1000000000,r=3,mod=1000000007;
    int N,D;
    struct node{
    	int y,next;
    }e[maxn<<1];
    int len=0,linkk[maxn],vis[maxn];
    void insert(int x,int y){
    	e[++len].y=y;
    	e[len].next=linkk[x];
    	linkk[x]=len;
    }
    int f[maxn][22],g[maxn][22],w[maxn];
    void dp(int x,int fa){
    	g[x][D+1]=inf;
    	if(vis[x])f[x][0]=g[x][0]=w[x];
    	up(i,1,D)g[x][i]=w[x];
    	for(int i=linkk[x];i;i=e[i].next){
    		int v=e[i].y;
    		if(v==fa)continue;
    		dp(v,x);
    		for(int j=D;j>=0;i--)g[x][j]=min(g[x][j+1],min(g[x][j]+f[v][j],g[v][j+1]+f[x][j+1]));
    		f[x][0]=g[x][0];
    		up(j,1,D+1)f[x][j]=min(f[x][j-1],f[x][j]+f[v][j-1]);
    	}
    }
    int main(){
    	freopen(FILE".in","r",stdin);
    	freopen(FILE".out","w",stdout);
    	N=read(),D=read();
    	up(i,1,N)w[i]=read();
    	int M=read();
    	up(i,1,M){
    		int x=read();
    		vis[x]=1;
    	}
    	up(i,2,N){
    		int x=read(),y=read();
    		insert(x,y);
    		insert(y,x);
    	}
    	dp(1,0);
    	cout<<f[1][0]<<endl;
    	return 0;
    }
    

      

  • 相关阅读:
    9、Spring Boot 2.x 集成 Thymeleaf
    【专题】Spring Boot 2.x 面试题
    8、Spring Boot 2.x 服务器部署
    7、Spring Boot 2.x 集成 Redis
    6、Spring Boot 2.x 集成 MyBatis
    5、Spring Boot 2.x 启动原理解析
    4、Spring Boot 2.x 自动配置原理
    3、Spring Boot 2.x 核心技术
    2、Spring Boot 2.x 快速入门
    centOS下安装JDK1.8.60,glassfish4.1.1以及MySQL
  • 原文地址:https://www.cnblogs.com/chadinblog/p/6559640.html
Copyright © 2011-2022 走看看