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. WINSERVER-IIS-无法启动

    报错信息:无法启动计算机上的服务W3SVC 开始百度,多数教程是这样写的 修复错误 运行命令提示符 fsutil resource setautoreset true c:\ 打开运行输入 servi ...

  2. 一步一步跟我学习hadoop(7)----hadoop连接mysql数据库运行数据读写数据库操作

        为了方便 MapReduce 直接訪问关系型数据库(Mysql,Oracle).Hadoop提供了DBInputFormat和DBOutputFormat两个类.通过DBInputFormat ...

  3. Oracle字符乱码、数据越界訪问典型Bug分析

    Oracle字符乱码.数据越界訪问典型Bug分析 前言:           作为乙方,在甲方客户那里验收阶段发现两个诡异Bug. 下面就问题来源.问题根因.解决方式.怎样避免做具体描写叙述. .且两 ...

  4. Oracle 后台进程介绍

    一 进程分类: 1.服务器进程(server process): 依据客户请求完毕工作.如接收和处理应用发送的SQL语句 2.后台进程(background process): 随数据库而启动,用于完 ...

  5. Asp.net动态页面静态化之初始NVelocity模板引擎

    Asp.net动态页面静态化之初始NVelocity模板引擎 静态页面是网页的代码都在页面中,不须要运行asp,php,jsp,.net等程序生成client网页代码的网页,静态页面网址中一般不含&q ...

  6. 剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到)

    剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到) 1.新建Converter public class BorderClipConverter : IM ...

  7. 如何获取Assets的路径

    有两种方法可以获取assets的绝对路径: 第一种方法: String path = file:///android_asset/文件名; 第二种方法: InputStream abpath = ge ...

  8. gson的安装和使用

    gson的安装和使用 1.安装 2.布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  9. [JZOJ 5437] [NOIP2017提高A组集训10.31] Sequence 解题报告 (KMP)

    题目链接: http://172.16.0.132/senior/#main/show/5437 题目: 题解: 发现满足上述性质并且仅当A序列的子序列的差分序列与B序列的差分序列相同 于是我们把A变 ...

  10. winform控件命名规范对照表

    WinForm Control 命名规范 数据类型 数据类型简写 标准命名举例 Label lbl lblMessage LinkLabel llbl llblToday Button btn btn ...