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. [NOIP补坑计划]NOIP2016 题解&做题心得

    感觉16年好难啊QAQ,两天的T2T3是不是都放反了啊…… 场上预计得分:100+80+100+100+65+100=545(省一分数线280) ps:loj没有部分分,部分分见洛咕 题解: D1T1 ...

  2. HDU 2095 find your present (2)( 位运算 )

    链接:传送门 题意:给出n个数,这n个数中只有一种数出现奇数次,其他全部出现偶数次,让你找到奇数次这个数 思路:简单异或运算题 /*********************************** ...

  3. [读书笔记] Python 数据分析 (八)画图和数据可视化

    ipython3 --pyplot pyplot: matplotlib 画图的交互使用环境

  4. typedef和define混用产生的错误

    最近在写代码过程中,发现一个问题,编译总是过不去,报错如下: stdint.h::: error: duplicate 'unsigned' stdint.h::: error: 'long long ...

  5. webpack实战---安装操作

    什么是webpack? 他有什么优点?     首先对于很多刚接触webpack人来说,肯定会问webpack是什么?它有什么优点?我们为什么要使用它?     Webpack是前端一个工具,可以让各 ...

  6. 机器学习关于AUC的理解整理

    AUC 几何意义:ROC曲线与X轴的面积 https://blog.csdn.net/luo3300612/article/details/80367901 AUC物理意义:随机给定一个正样本和一个负 ...

  7. SQL SERVER-union

    UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每个 SELECT 语句中 ...

  8. 上机题目(0基础)-计算两个正整数的最大公约数和最小公倍数(Java)

    题目例如以下:

  9. unity3d进程通信利用WM_COPYDATE和HOOK

    hello,近期用unity做了进程通信,应该是和c++的PC端实现通信,才開始一头雾水,后来实现了才知道好繁杂......先感谢对我提供帮助的百度,谷歌以及游戏圈的大大们. 在进程通信中非常多方法, ...

  10. 开源TT框架上的日志类

    public class Logger { /** * log tag */ private String tagName = "MoGuLogger";// tag name / ...