Bombing plan

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 416    Accepted Submission(s): 96

Problem Description
Kingdom Y is in the war with kingdom X. Kingdom X consists of N cities,there are N-1 bidirectional roads which are all 1 long ,each of them connect a pair of cities,the N cities are all connect by the N-1 bidirectional.People can travel through the roads.

Now kingdom Y is going to bomb kingdom X. Every city of kingdom X has its own value W. If city i was to be bombed, then all the cities that lie within the distance W(i) from city i would be destroyed as well. The king of kingdom Y wants to know the minimum bombing time that can destroy all the cities in kingdom X. Could you help him?

 
Input
There are multiple test cases. Please process till EOF.
In each test case: 
First line: an integer n(n<=10^5) indicating the number of city
Second line:contain n numbers w[i](0<=w[i]<=100) ,indicating that the value of city[i],
Next n - 1 lines: each contains two numbers ui and vi, (1 ≤ ui,vi<=n), indicates that there’s one road connecting city ui and vi.

 
Output
For each case,output one number, denotes the minimum number of bombing times.
 
Sample Input
5
1 1 1 1 1
1 2
2 3
3 4
4 5
 
Sample Output
2
 
Author
FZUACM
 
Source
 
解题:dp
令F[i][j]为以i为根的子树,能向子树外拓展j个节点最少需要炸毁几个城市。G[i][j]为以i为根的子树,子树内有节点未被炸毁,且距离根为j最少需要炸毁几个城市。 
转移方程: 
不炸毁u点 
 
$F[u][j] = F[v][j+1] + min(F[k][0\dots j+1,G[k][0\dots j])$
$G[u][0] = F[u][0]$
$G[u][j] = G[v][j-1] + min(F[k][0\dots j-1],G[k][0\dots j-1])$
 
炸毁u点
$F[u][w[u]] = 1 + min(F[v][0\dots w[u]+1],G[v][w[u]])$
 
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[maxn<<];
int head[maxn],d[maxn],n,tot;
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
e[tot] = arc(u,head[v]);
head[v] = tot++;
}
int q[maxn],p[maxn],de[maxn],hd,tl;
int F[maxn][],G[maxn][],A[maxn][],B[maxn][];
int main() {
int u,v,a,b;
while(~scanf("%d",&n)) {
for(int i = ; i <= n; ++i)
scanf("%d",d+i);
tot = ;
memset(head,-,sizeof head);
memset(G,-,sizeof G);
memset(F,-,sizeof F);
memset(A,-,sizeof A);
memset(B,-,sizeof B);
for(int i = ; i < n; ++i) {
scanf("%d%d",&u,&v);
add(u,v);
}
p[q[hd = tl = ] = ] = -;
while(hd <= tl) {
de[u = q[hd++]] = ;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to != p[u]) {
p[e[i].to] = u;
q[++tl] = e[i].to;
}
}
}
while(tl >= ) {
v = q[tl--];
if(p[v] >= ) de[p[v]] = max(de[p[v]],de[v]+);
if(!de[v]) {
if(d[v] >= ) {
F[v][d[v]] = ;
for(int i = ; i < d[v]; ++i) A[v][i] = -;
for(int i = d[v]; i < ; ++i) A[v][i] = ;
}
G[v][] = ;
for(int i = ; i <= ; ++i) B[v][i] = ;
continue;
} for(int i = ; i <= min(,de[v]); ++i) {
G[v][i] = ;
for(int j = head[v]; ~j; j = e[j].next) {
u = e[j].to;
if(u == p[v]) continue;
a = B[u][i-];
b = A[u][];
if(a == - && b == -) {
G[v][i] = -;
break;
}
if(a == -) a = maxn;
if(b == -) b = maxn;
G[v][i] += min(a,b);
}
if(G[v][i] == -) break;
} if(d[v] >= ) {
F[v][d[v]] = ;
for(int i = head[v]; ~i; i = e[i].next) {
u = e[i].to;
if(u == p[v]) continue;
a = A[u][];
b = -;
if(d[v] > ) b = B[u][d[v]-];
if(a == - && b == -) {
F[v][d[v]] = -;
break;
}
if(a == -) a = maxn;
if(b == -) b = maxn;
F[v][d[v]] += min(a,b);
}
} for(int i = head[v]; ~i; i = e[i].next) {
u = e[i].to;
if(u == p[v]) continue;
for(int j = ; j <= ; ++j)
if(F[u][j] != -) {
int tmp = ;
for(int k = head[v]; ~k; k = e[k].next) {
if(e[k].to != u && e[k].to != p[v]) {
a = A[e[k].to][];
b = -;
if(j - >= ) b = B[e[k].to][j-];
if(a == - && b == -) {
tmp = -;
break;
}
if(a == -) a = maxn;
if(b == -) b = maxn;
tmp += min(a,b);
}
}
if(tmp != - && (F[v][j-] == - || F[v][j-] > F[u][j] + tmp))
F[v][j-] = F[u][j] + tmp;
}
}
A[v][] = F[v][];
B[v][] = G[v][];
for(int i = ; i <= ; ++i) {
A[v][i] = A[v][i-];
if(F[v][i] != - && (A[v][i] == - || A[v][i] > F[v][i]))
A[v][i] = F[v][i];
B[v][i] = B[v][i-];
if(G[v][i] != - && (B[v][i] == - || B[v][i] > G[v][i]))
B[v][i] = G[v][i];
}
}
int ret = -;
for(int i = ; i <= ; ++i)
if(F[][i] != - && (ret == - || ret > F[][i]))
ret = F[][i];
printf("%d\n",ret);
}
return ;
}
/*
5
1 1 1 1 1
1 2
2 3
3 4
4 5
*/

2015 Multi-University Training Contest 1 hdu 5290 Bombing plan的更多相关文章

  1. hdu 5290 Bombing plan

    http://acm.hdu.edu.cn/showproblem.php?pid=5290 题意: 一棵树,每个点有一个权值wi,选择点i即可破坏所有距离点i<=wi的点,问破坏所有点 最少需 ...

  2. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  3. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  4. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  5. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  6. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  7. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  8. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  9. 2015 Multi-University Training Contest 6 hdu 5362 Just A String

    Just A String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. 利用请求的JSON数据创建图形图层

    先看效果图: 包含三个部分:信息窗口(标题是要素的某个属性信息,其余是感兴趣的属性信息).图上图形按照某一属性大小不一显示,图例 1.创建底图用于存放以上三部分: "esri/Map&quo ...

  2. debian 9 安装无线网卡

    #添加源 echo "deb http://httpredir.debian.org/debian/ stretch main contrib non-free" >> ...

  3. Eigen下载安装

    首先提供Eigen的两个重要网站 官方网站 下载地址 1.下载 wget http://bitbucket.org/eigen/eigen/get/3.3.5.tar.gz 2.解压缩 tar -zx ...

  4. 线性回归(regression)

    简介 回归分析只涉及到两个变量的,称一元回归分析.一元回归的主要任务是从两个相关变量中的一个变量去估计另一个变量,被估计的变量,称因变量,可设为Y:估计出的变量,称自变量,设为X. 回归分析就是要找出 ...

  5. [terry笔记]Python字符串

    如下学习python的字符串用法. print(dir(str)) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', ...

  6. java中的hachcode方法

    哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...

  7. maven规定的目录

    Maven规定的目录结构 若要使用Maven,那么项目的目录结构必须符合Maven的规范 ,如写一个使用Spring的Web项目就需要引入大量的jar包.一个项目Jar包的数量之多往往让我们瞠目结舌, ...

  8. nodejs是一个平台,是平台

    node.js是用javascript来写服务器代码的平台

  9. 怎样制作C#安装程序

    近期须要制作一个C#安装.在网上找了一些资料发现都不是非常完整,最后自己综合了一些资料,而且通过亲自检測,最后成功完毕C#打包成安装程序(打包成最简单的一种安装程序.假设须要更高的功能请自己在开发). ...

  10. Unity3d修炼之路:用Mesh绘制一个Cube

    #pragma strict function Awake(){ var pMeshFilter : MeshFilter = gameObject.AddComponent(typeof(MeshF ...