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. pupload上传插件问题整理

    前些日子公司网站需要开发一个类似与百度文库上传文档的功能,实现文档的批量上传.展示以及继续上传的功能.开发完成后,通过在多版浏览器下的使用,发现了一系列问题,特总结于下,以免来者在这些问题上耗费太多时 ...

  2. CF1027F Session in BSU (并查集+树上构造)

    题目大意:你可以在第$ai$天或者第$bi$天进行第$i$场考试,每天最多进行一场考试,求把所有考试都考完的最早结束时间 由于天数可能很大,需要离散 把问题抽象成一棵树,每个点最多被"分配& ...

  3. Tensorflow 读写 tfrecord 文件(Python3)

    TensorFlow笔记博客:https://blog.csdn.net/xierhacker/article/category/6511974 写入tfrecord文件 import tensorf ...

  4. iText操作pdf(生成,导入图片等)

    生成pdf有很多种方法,用pdfbox也很方便,今天我要写的是用iText 主要在pom.xml中配置的jar包如下 <dependency> <groupId>com.low ...

  5. PatentTips - Object-oriented processor architecture and operating method

    BACKGROUND OF THE INVENTION The present invention relates to processors and computer systems. More s ...

  6. Qt之qInstallMessageHandler(输出详细日志)

    简述 安装之前已定义的消息处理程序,返回一个指向前一个消息处理程序. 消息处理程序是一个函数,用于打印调试信息.警告信息.严重错误和致命的错误的消息.Qt库(debug模式)包含成百上千的警告信息打印 ...

  7. 关于nodejs的线程模型可以看这篇文章

    虽然还是有一些没有讲全,但是整体还是讲的很不错的. http://www.ruanyifeng.com/blog/2014/10/event-loop.html

  8. 【转载】SQLITE3 使用总结

    转载自网友董淳光的文章. 前序: 这里要注明,我是一个跨平台专注者,并不喜欢只用 windows 平台.我以前的工作就是为 unix 平台写代码.下面我所写的东西,虽然没有验证,但是我已尽量不使用任何 ...

  9. POJ 3664 Election Time 题解

    这道题网上非常多人都会说easy,水题之类的话,只是我看了下说这种话的人的程序,能够说他们的程序都不及格! 为什么呢?由于他们的程序都是使用简单的二次排序水过(大概你能搜索到的多是这种程序).那样自然 ...

  10. yii自己定义CLinkPager分页

    在components中自己定义LinkPager.并继承CLinkPager 代码例如以下: <? php /** * CLinkPager class file. * * @author l ...