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]])$
 
 
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = ;
  4. struct arc {
  5. int to,next;
  6. arc(int x = ,int y = -) {
  7. to = x;
  8. next = y;
  9. }
  10. } e[maxn<<];
  11. int head[maxn],d[maxn],n,tot;
  12. void add(int u,int v) {
  13. e[tot] = arc(v,head[u]);
  14. head[u] = tot++;
  15. e[tot] = arc(u,head[v]);
  16. head[v] = tot++;
  17. }
  18. int q[maxn],p[maxn],de[maxn],hd,tl;
  19. int F[maxn][],G[maxn][],A[maxn][],B[maxn][];
  20. int main() {
  21. int u,v,a,b;
  22. while(~scanf("%d",&n)) {
  23. for(int i = ; i <= n; ++i)
  24. scanf("%d",d+i);
  25. tot = ;
  26. memset(head,-,sizeof head);
  27. memset(G,-,sizeof G);
  28. memset(F,-,sizeof F);
  29. memset(A,-,sizeof A);
  30. memset(B,-,sizeof B);
  31. for(int i = ; i < n; ++i) {
  32. scanf("%d%d",&u,&v);
  33. add(u,v);
  34. }
  35. p[q[hd = tl = ] = ] = -;
  36. while(hd <= tl) {
  37. de[u = q[hd++]] = ;
  38. for(int i = head[u]; ~i; i = e[i].next) {
  39. if(e[i].to != p[u]) {
  40. p[e[i].to] = u;
  41. q[++tl] = e[i].to;
  42. }
  43. }
  44. }
  45. while(tl >= ) {
  46. v = q[tl--];
  47. if(p[v] >= ) de[p[v]] = max(de[p[v]],de[v]+);
  48. if(!de[v]) {
  49. if(d[v] >= ) {
  50. F[v][d[v]] = ;
  51. for(int i = ; i < d[v]; ++i) A[v][i] = -;
  52. for(int i = d[v]; i < ; ++i) A[v][i] = ;
  53. }
  54. G[v][] = ;
  55. for(int i = ; i <= ; ++i) B[v][i] = ;
  56. continue;
  57. }
  58.  
  59. for(int i = ; i <= min(,de[v]); ++i) {
  60. G[v][i] = ;
  61. for(int j = head[v]; ~j; j = e[j].next) {
  62. u = e[j].to;
  63. if(u == p[v]) continue;
  64. a = B[u][i-];
  65. b = A[u][];
  66. if(a == - && b == -) {
  67. G[v][i] = -;
  68. break;
  69. }
  70. if(a == -) a = maxn;
  71. if(b == -) b = maxn;
  72. G[v][i] += min(a,b);
  73. }
  74. if(G[v][i] == -) break;
  75. }
  76.  
  77. if(d[v] >= ) {
  78. F[v][d[v]] = ;
  79. for(int i = head[v]; ~i; i = e[i].next) {
  80. u = e[i].to;
  81. if(u == p[v]) continue;
  82. a = A[u][];
  83. b = -;
  84. if(d[v] > ) b = B[u][d[v]-];
  85. if(a == - && b == -) {
  86. F[v][d[v]] = -;
  87. break;
  88. }
  89. if(a == -) a = maxn;
  90. if(b == -) b = maxn;
  91. F[v][d[v]] += min(a,b);
  92. }
  93. }
  94.  
  95. for(int i = head[v]; ~i; i = e[i].next) {
  96. u = e[i].to;
  97. if(u == p[v]) continue;
  98. for(int j = ; j <= ; ++j)
  99. if(F[u][j] != -) {
  100. int tmp = ;
  101. for(int k = head[v]; ~k; k = e[k].next) {
  102. if(e[k].to != u && e[k].to != p[v]) {
  103. a = A[e[k].to][];
  104. b = -;
  105. if(j - >= ) b = B[e[k].to][j-];
  106. if(a == - && b == -) {
  107. tmp = -;
  108. break;
  109. }
  110. if(a == -) a = maxn;
  111. if(b == -) b = maxn;
  112. tmp += min(a,b);
  113. }
  114. }
  115. if(tmp != - && (F[v][j-] == - || F[v][j-] > F[u][j] + tmp))
  116. F[v][j-] = F[u][j] + tmp;
  117. }
  118. }
  119. A[v][] = F[v][];
  120. B[v][] = G[v][];
  121. for(int i = ; i <= ; ++i) {
  122. A[v][i] = A[v][i-];
  123. if(F[v][i] != - && (A[v][i] == - || A[v][i] > F[v][i]))
  124. A[v][i] = F[v][i];
  125. B[v][i] = B[v][i-];
  126. if(G[v][i] != - && (B[v][i] == - || B[v][i] > G[v][i]))
  127. B[v][i] = G[v][i];
  128. }
  129. }
  130. int ret = -;
  131. for(int i = ; i <= ; ++i)
  132. if(F[][i] != - && (ret == - || ret > F[][i]))
  133. ret = F[][i];
  134. printf("%d\n",ret);
  135. }
  136. return ;
  137. }
  138. /*
  139. 5
  140. 1 1 1 1 1
  141. 1 2
  142. 2 3
  143. 3 4
  144. 4 5
  145. */

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. 三、Git 分支

    使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线.有人把 Git 的分支模型称为它的`‘必杀技特性’',也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出. 1.分支简介 ...

  2. C/C++ 名正则言顺

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50532701 名称所表达的含义极其丰富 ...

  3. urlEncoder和urlDecoder的作用和使用

    1.URLEncoder.encode(String s, String enc) 使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式 URLD ...

  4. C#一个托付的样例

    C#中的函数能够被声明的一个托付所调用. 函数为静态方法.和托付声明的參数返回值要一致.   class Program { delegate float MathOperationDelegate( ...

  5. Dalvik虚拟机垃圾收集(GC)过程分析

    前面我们分析了Dalvik虚拟机堆的创建过程,以及Java对象在堆上的分配过程. 这些知识都是理解Dalvik虚拟机垃圾收集过程的基础.垃圾收集是一个复杂的过程,它要将那些不再被引用的对象进行回收.一 ...

  6. Oracle 后台进程介绍

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

  7. Partition(hdu4651)2013 Multi-University Training Contest 5----(整数拆分一)

    Partition Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. oracle 时间戳TIMESTAMP

    //数据库 UPDATETIMESTAMP TIMESTAMP(6) //dto /** 更新时间戳 **/ private String updatetimestamp; //dao //插入操作 ...

  9. Java -- XML解析工具dom4j

    前言 XML现已成为一种通用的数据交流方式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便,对于XML的解析有四种方式:DOM生成和解析XML文档,SAX生成和解析XML文件 ...

  10. js mudules.js

    var InsertRow={ isMoveRow:false, // 是否存在动态移动行 curSelRowIndex:"", // 当前选中行序号 prevSelRowInde ...