首页 > ACM题库 > HDU-杭电 > HDU 1011 Starship Troopers-动态规划-[解题报告] C++
2013
11-26

HDU 1011 Starship Troopers-动态规划-[解题报告] C++

Starship Troopers

问题描述 :

You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with tunnels. Each room is occupied by some bugs, and their brains hide in some of the rooms. Scientists have just developed a new weapon and want to experiment it on some brains. Your task is to destroy the whole base, and capture as many brains as possible.

To kill all the bugs is always easier than to capture their brains. A map is drawn for you, with all the rooms marked by the amount of bugs inside, and the possibility of containing a brain. The cavern’s structure is like a tree in such a way that there is one unique path leading to each room from the entrance. To finish the battle as soon as possible, you do not want to wait for the troopers to clear a room before advancing to the next one, instead you have to leave some troopers at each room passed to fight all the bugs inside. The troopers never re-enter a room where they have visited before.

A starship trooper can fight against 20 bugs. Since you do not have enough troopers, you can only take some of the rooms and let the nerve gas do the rest of the job. At the mean time, you should maximize the possibility of capturing a brain. To simplify the problem, just maximize the sum of all the possibilities of containing brains for the taken rooms. Making such a plan is a difficult job. You need the help of a computer.

输入:

The input contains several test cases. The first line of each test case contains two integers N (0 < N <= 100) and M (0 <= M <= 100), which are the number of rooms in the cavern and the number of starship troopers you have, respectively. The following N lines give the description of the rooms. Each line contains two non-negative integers — the amount of bugs inside and the possibility of containing a brain, respectively. The next N – 1 lines give the description of tunnels. Each tunnel is described by two integers, which are the indices of the two rooms it connects. Rooms are numbered from 1 and room 1 is the entrance to the cavern.

The last test case is followed by two -1′s.

输出:

For each test case, print on a single line the maximum sum of all the possibilities of containing brains for the taken rooms.

样例输入:

5 10
50 10
40 10
40 20
65 30
70 30
1 2
1 3
2 4
2 5
1 1
20 7
-1 -1

样例输出:

50
7

貌似是很经典的树形dp问题,应该说是树形dp的入门!!

感觉树形dp比背包多了一个辅助数组,背包直接一个数组循环下去,而树形dp因为有分支,不是线性的,所以就需要用一个辅助数组来进行转化最优情况!

1011 题目大意:一棵树,有n个结点,每个结点有v个bug,有w的brain。我从1号结点开始走,带着m个战士。
1个战士可以消灭20个bugs,如果我把某个结点的所有bug都消灭了我就能得到那个结点的brain。
如果想攻击当前结点,那么必须先攻击了它的父结点(1号点除外)。
其中当你攻占了当前结点,你可以分派人手,走向几个不同的子结点,去攻占更多。也就是说,不是单一的路径。
代码:
1 # include<stdio.h>
 2 # include<string.h>
 3 # define N 105
 4 struct node{
 5     int from,to,next;
 6 }edge[2*N];
 7 int head[N],tol,visit[N],ans[N],bug[N],n,m,dp[N][N],f[N][N];
 8 void add(int a,int b)
 9 {
10     edge[tol].from=a;edge[tol].to=b;edge[tol].next=head[a];head[a]=tol++;
11 }
12 int max(int a,int b)
13 {
14     return a>b?a:b;
15 }
16 void dfs(int u)
17 {
18     int i,j,r,tt,k;
19     visit[u]=1;
20     for(i=head[u];i!=-1;i=edge[i].next)
21     {
22         r=edge[i].to;
23         if(!visit[r])
24         {
25             dfs(r);
26             for(k=m;k>=1;k--)
27             {
28                 for(j=1;j<=k;j++)
29                 {
30                     f[u][k]=max(f[u][k],f[u][k-j]+dp[r][j]);
31                 }
32             }
33                 /*for(j=0;j<=m;j++)
34                 {
35                 if(j*20>=ans[u]) 
36                     dp[u][j]=max(dp[u][j],dp[r][(j*20-ans[u])/20]+bug[u]);
37                 }*/
38         }
39     }
40     tt=(ans[u]+19)/20;
41     for(j=tt;j<=m;j++)
42         dp[u][j]=f[u][j-tt]+bug[u];
43 }
44 int main()
45 {
46     int i,a,b;
47     while(scanf("%d%d",&n,&m)!=EOF)
48     {
49         if(n==-1 && m==-1) break;
50         for(i=1;i<=n;i++)
51             scanf("%d%d",&ans[i],&bug[i]);
52         tol=0;
53         memset(head,-1,sizeof(head));
54         for(i=1;i<n;i++)
55         {
56             scanf("%d%d",&a,&b);
57             add(a,b);
58             add(b,a);
59         }
60         memset(visit,0,sizeof(visit));
61         memset(dp,0,sizeof(dp));
62             memset(f,0,sizeof(f));
63                     if(m==0)
64         {
65             printf("0\n");
66             continue;
67         }
68 
69         dfs(1);
70         printf("%d\n",dp[1][m]);
71     }
72     return 0;
73 }

  1. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥