首页 > 代码库 > HDU 1011 Starship Troopers(树形DP)

HDU 1011 Starship Troopers(树形DP)

Starship Troopers

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 62   Accepted Submission(s) : 12

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Author

XU, Chuan

Source

ZJCPC2004
 
题解:

这个题目是在树形DP的基础上在每个节点的一个分组背包问题

首先根据题目条件建立一颗树,然后DFS整棵树,每个节点的dp[i][j]

表示以这个i节点为根,放j个士兵最多能得到brains的个数!

注意:只有根节点的bug全被消灭,才能访问子节点

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
int dp[105][105],bug[105],brain[105]; //dp[i][j]表示第i个节点放置j个士兵
bool vis[105];
int n,m;
vector<int> mp[105];
void dfs(int k)
{
    vis[k]=1;
    for(int i=bug[k];i<=m;i++) dp[k][i]=brain[k];
    for(int i=0;i<mp[k].size();i++)
    {
        if (vis[mp[k][i]]) continue;
        dfs(mp[k][i]);
        for(int j=m;j>=bug[k];j--)//一定要倒序,类似01背包
         for(int q=1;q+j<=m;q++) //只要父节点和子节点的士兵总和不超过m的情况都要看看这种可能
          dp[k][j+q]=max(dp[k][j+q],dp[k][j]+dp[mp[k][i]][q]);
          /*
          之前写成
          for(int j=m;j>=bug[k];j--)
                for(int q=1;q<=j-bug[k];q++)
                    dp[u][j]=max(dp[u][j],dp[u][j-q]+dp[v][q]);
          这是错误的,一直wa还找不到错误在哪,网上的这个代码放到hdu里也是错的
          */
    }
    return;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if (n==-1 && m==-1) break;
        for(int i=1;i<=n;i++)
         {
             scanf("%d%d",&bug[i],&brain[i]);
             if (bug[i]%20!=0) bug[i]=bug[i]/20+1;//处理成要放的士兵个数
                   else bug[i]=bug[i]/20;
             mp[i].clear();
         }
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        if (m==0) {printf("0\n"); continue;}
        memset(vis,0,sizeof(vis));
        memset(dp,0,sizeof(dp));
        dfs(1);
        printf("%d\n",dp[1][m]);
    }
    return 0;
}

 


HDU 1011 Starship Troopers(树形DP)