【BZOJ4557】[JLoi2016]侦察守卫 树形DP
【BZOJ4557】[JLoi2016]侦察守卫
Description
Input
Output
仅一行一个整数,表示监视所有B神可能出现的点所需要的最小代价
Sample Input
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
题解:状态比较好想,但是转移稍微复杂。
f[x][y]表示x子树中所有点都已经覆盖完,并且x还能向上覆盖y层的最小代价。
g[x][y]表示x的y层以下的所有点都已经覆盖完,还需要覆盖上面的y层的最小代价。
转移时注意一下顺序,不要重复计算,细节还是见代码吧~
#include <cstdio>
#include <cstring>
#include <iostream> using namespace std;
const int maxn=500010;
const int inf=1<<29;
int n,m,D,cnt;
int f[maxn][22],g[maxn][22],s[maxn][22],w[maxn],to[maxn<<1],next[maxn<<1],head[maxn],vis[maxn],fa[maxn];
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
inline void add(int a,int b)
{
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
void dfs(int x)
{
int i,j,y;
if(vis[x]) f[x][0]=g[x][0]=w[x];
for(i=1;i<=D;i++) f[x][i]=w[x];
f[x][D+1]=inf;
for(i=head[x];i!=-1;i=next[i]) if(to[i]!=fa[x])
{
y=to[i],fa[y]=x,dfs(y);
for(j=D;j>=0;j--)
f[x][j]=min(f[x][j]+g[y][j],g[x][j+1]+f[y][j+1]);
for(j=D;j>=0;j--) f[x][j]=min(f[x][j],f[x][j+1]);
g[x][0]=f[x][0];
for(j=1;j<=D;j++) g[x][j]+=g[y][j-1];
for(j=1;j<=D;j++) g[x][j]=min(g[x][j],g[x][j-1]);
}
}
int main()
{
n=rd(),D=rd();
int i,a,b;
for(i=1;i<=n;i++) w[i]=rd();
m=rd();
for(i=1;i<=m;i++) a=rd(),vis[a]=1;
memset(head,-1,sizeof(head));
for(i=1;i<n;i++) a=rd(),b=rd(),add(a,b),add(b,a);
memset(s,0x3f,sizeof(s));
dfs(1);
printf("%d",f[1][0]);
return 0;
}
【BZOJ4557】[JLoi2016]侦察守卫 树形DP的更多相关文章
- [BZOJ4557][JLOI2016]侦察守卫(树形DP)
首先可以确定是树形DP,但这里存在跨子树的信息传递问题,这里就需要“借”的思想. f[i][j]表示i子树内所有点都被覆盖到,且i以外j层内的点都能被覆盖到 的方案数. g[i][j]表示i子树内离i ...
- BZOJ4557 JLoi2016 侦察守卫 【树形DP】*
BZOJ4557 JLoi2016 侦察守卫 Description 小R和B神正在玩一款游戏.这款游戏的地图由N个点和N-1条无向边组成,每条无向边连接两个点,且地图是连通的.换句话说,游戏的地图是 ...
- 洛谷 P3267 [JLOI2016/SHOI2016]侦察守卫(树形dp)
题面 luogu 题解 树形\(dp\) \(f[x][y]表示x的y层以下的所有点都已经覆盖完,还需要覆盖上面的y层的最小代价.\) \(g[x][y]表示x子树中所有点都已经覆盖完,并且x还能向上 ...
- BZOJ4557 JLOI2016侦察守卫(树形dp)
下称放置守卫的点为监控点.设f[i][j]为i子树中深度最大的未被监视点与i的距离不超过j时的最小代价,g[i][j]为i子树中距离i最近的监控点与i的距离不超过j且i子树内点全部被监视时的最小代价. ...
- bzoj4557 [JLoi2016]侦察守卫——DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4557 见这位的博客:https://www.cnblogs.com/Narh/p/91403 ...
- bzoj千题计划272:bzoj4557: [JLoi2016]侦察守卫
http://www.lydsy.com/JudgeOnline/problem.php?id=4557 假设当前到了x的子树,现在是合并 x的第k个子树 f[x][j] 表示x的前k-1个子树该覆盖 ...
- 洛谷 P3267 - [JLOI2016/SHOI2016]侦察守卫(树形 dp)
洛谷题面传送门 经典题一道,下次就称这种"覆盖距离不超过 xxx 的树形 dp"为<侦察守卫模型> 我们考虑树形 \(dp\),设 \(f_{x,j}\) 表示钦定了 ...
- Luogu3267 [JLOI2016/SHOI2016]侦察守卫 (树形DP)
树形DP,一脸蒙蔽.看了题解才发现它转移状态与方程真不愧神题! \(f[x][y]\)表示\(x\)的\(y\)层以下的所有点都已经覆盖完,还需要覆盖上面的\(y\)层的最小代价. \(g[x][y] ...
- 动态规划(树形DP):LNOI 2016 侦察守卫
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 ...
随机推荐
- Python-Mac OS X EI Capitan下安装Scrapy
sudo pip install scrapy --ignore-installed six #sudo pip install scrapy --upgrade --ignore-installed ...
- Python-文件修改器
#-*- coding: utf-8 -*- import os import sys import glob from PyQt4.QtGui import * from PyQt4.QtCore ...
- PHP中根据IP地址判断所在城市等信息
本篇文章由:http://xinpure.com/php-based-on-information-such-as-the-ip-address-in-your-city/ 获得IP地址 在 PHP ...
- Apache服务器配置https协议/SSL证书的方法
转载于:http://www.server110.com/apache/201309/1542.html
- leetcode——Lowest Common Ancestor of a Binary Tree
题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 思路 这一次 ...
- 统一修改 UINavigationBar backItem
{ UINavigationBar * navigationBar = [UINavigationBar appearance]; //返回按钮的箭头颜色 [navigationBar setTint ...
- 关于Animator获取当前剪辑长度
通常下意识的肯定用这个接口 GetCurrentAnimatorStateInfo().length 但是存在一个过渡动画的问题,具体看这篇:过渡动画的测试 所以当播新的状态时直接取动画时间,取到的就 ...
- 第一个EJB示例
FirstEJB2.0.zip Eclipse + JBoss 5.1 Ejb3Example.zip Eclipse + JBoss 7.1 注意点: 1. jboss 增加用户: D:\DevPr ...
- Java接口的异常设计
一.问题的提出 疑惑1:在设计接口的时,对于接口方法何时需要声明抛出受检异常或者说所有的接口方法最后都声明抛出受检异常? 以下是代码片段: public interface xx{ public ...
- What is Web Application Architecture? How It Works, Trends, Best Practices and More
At Stackify, we understand the amount of effort that goes into creating great applications. That’s w ...