码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1011Starship Troopers(树形背包)

时间:2017-11-26 12:41:55      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:with   sim   and   ssi   instead   oms   mount   bat   line   

Starship Troopers

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20833    Accepted Submission(s): 5550

Problem Description

 
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.
 

 

Input

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.
 

 

Output

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

 

Sample Input

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
 

 

Sample Output

50 7
 
 

code

 1 /*
 2 hdu 1011 
 3 dp[i][j]:以i为根的树中,留下j个士兵,最大的收益。 
 4 */
 5 #include<cstdio>
 6 #include<algorithm>
 7 #include<cstring>
 8 
 9 using namespace std;
10 
11 const int MAXN = 210;
12 
13 struct Edge{
14     int to,nxt;
15 }e[10010];
16 int head[10010],tot;
17 int val[MAXN],bg[MAXN],dp[MAXN][MAXN];
18 bool vis[MAXN];
19 int n,m;
20 
21 inline int read() {
22     int x = 0,f = 1;char ch = getchar();
23     for (; ch<0||ch>9; ch = getchar())
24         if (ch==-) f = -1;
25     for (; ch>=0&&ch<=9; ch = getchar())
26         x = x*10+ch-0;
27     return x*f;
28 }
29 inline void add_edge(int u,int v) {
30     e[++tot].to = v,e[tot].nxt = head[u],head[u] = tot;
31 }
32 inline void init() {
33     memset(head,0,sizeof(head));
34     memset(vis,false,sizeof(vis));
35     memset(dp,0,sizeof(dp));
36     tot = 0;
37 }
38 void dfs(int u) {
39     int tmp = (bg[u]+19)/20; // 对于当前点留下多少士兵 
40     for (int i=tmp; i<=m; ++i) dp[u][i] = val[u]; // 留下tmp个以及tmp+1...都是一样的 
41     vis[u] = true;
42     for (int i=head[u]; i; i=e[i].nxt) {
43         int v = e[i].to;
44         if (vis[v]) continue; // 不能搜回去 
45         dfs(v);
46         for (int j=m; j>=tmp; --j) 
47             for (int k=1; k<=j-tmp; ++k) 
48                 dp[u][j] = max(dp[u][j],dp[u][j-k]+dp[v][k]);
49     }
50 }
51 int main() {
52     
53     while (scanf("%d%d",&n,&m)!=EOF && (n!=-1||m!=-1)) {
54         init(); 
55         for (int i=1; i<=n; ++i) {
56             bg[i] = read(), val[i] = read();
57         }
58         for (int u,v,i=1; i<n; ++i) {
59             u = read(),v = read();
60             add_edge(u,v),add_edge(v,u); // 当前不知道谁是谁的父亲儿子 
61         }
62         if (m==0) { // 别忘了特判,有这样的数据!!! 
63             printf("0\n");continue;
64         }
65         dfs(1);
66         printf("%d\n",dp[1][m]);
67     } 
68     return 0;
69 }

 

 

hdu1011Starship Troopers(树形背包)

标签:with   sim   and   ssi   instead   oms   mount   bat   line   

原文地址:http://www.cnblogs.com/mjtcn/p/7898384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!