hdoj 1011 Starship Troopers 【树形DP + 01背包】

来源:互联网 发布:android图形编程 编辑:程序博客网 时间:2024/06/05 00:59

Starship Troopers

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


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 1050 1040 1040 2065 3070 301 21 32 42 51 120 7-1 -1
 

Sample Output
507
 

看了一天树形DP  + 背包,这是靠自己AC的 一道树形DP + 01背包。剩下几道理解不是特别好,改天再贴。

因为预处理少了,所以WA了三次。。。

大致题意:有N个洞穴和连接洞穴的N-1条无向通道,它们构成一棵树。 给你M个士兵,每个士兵可以对付20个敌人(但不能重复进入他之前进过的洞穴),当然若某个洞穴敌人数量过多即大于M*20时,你就不能进入,这时只能用毒气来消灭这个洞穴的敌人(当然这个洞穴的头脑就抓不到了)。为了尽可能快的解决战斗,当你先于一些正在战斗的士兵到达某个洞穴时,你不会等待那些士兵,而会选择把那些士兵留在洞穴里面自己继续前进。现已给出 每个洞穴敌人的数量和头脑存在的概率,问你消灭所有敌人的过程中,抓获头脑的最大概率(为了简化问题,求所有可能概率值的累加)。


想了好久,灵光一闪,KO!

分析:这道题很明显要利用树形dp从树的最底部向根递推求最优解,但关键在于找到dp数组所代表的状态,继而写出状态转移方程。因为只有M个士兵,所以我们可以把这个信息当作一个限制(这时估计思路比较好的就想到了01背包中质量的限制,我是到后面才想到的...),又因为对于任一个洞穴,可以选择进或不进 且只能进一次(01背包中对任一件物品的选择)。到这里,就能看出这是01-背包了(若看不出来,建议好好看看背包九讲)。这样根据原版01-背包就推出了dp数组表示的状态(我是这样摸索出来的,勿喷...)。


信息的提炼
一:对每个洞穴的选择这是个01背包问题;
二:dp[ i ][ j ]表示的状态是  到第 i 个洞穴且已经使用了j 个士兵的前提下 所能抓获头脑的最大概率。


这样状态转移方程就好写了
用num[]数组存储每个洞穴所需要的士兵的数目,用val[]数组存储每个洞穴能抓获头脑的概率值。

对<u,v>边:j 表示进入u洞穴使用的士兵数目,k表示进入v洞穴使用的士兵数目
dp[ u ][ j ]  = max(dp[ u ][ j ], dp[ u ][ j-k ] + dp[ v ][ k ])。(num[u] <= j <= M, 1 <=  k + num[u] <= j )。

注意:
1, 这里dp[ v ][ k ]必须是有意义的 ,也就是说v 洞穴要能够进入。
2,括号里面的限制是一定要有的,因为只有满足这个限制才能够进入洞穴u!!!


思路:从根节点开始DFS,在每个回溯过程进行01背包最优值的选择。


详看代码:小心对dp数组的预处理 和 M 为0时的判断

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#define MAXN 100+10using namespace std;vector<int> G[MAXN];int dp[MAXN][MAXN];//dp[i][j]表示在房间i时 用过j个飞船 得到的抓获头目的最大概率int val[MAXN];//每个房间能抓获头目的概率值int num[MAXN];//记录每个房间 消耗的飞船数目int N, M;void DFS(int u, int fa){    for(int i = num[u]; i <= M; i++)//预处理 WA了三次。。。        dp[u][i] = val[u];    for(int i = 0; i < G[u].size(); i++)    {        int v = G[u][i];        if(v == fa) continue;        DFS(v, u);        //0-1 背包 求使用M个飞船 所能得到的抓获头目的最大概率        for(int j = M; j >= num[u]; j--)        {            for(int k = 1; k + num[u] <= j; k++)//枚举 到v房间时 可能消耗的飞船数目            {                if(dp[v][k])                    dp[u][j] = max(dp[u][j], dp[u][j-k] + dp[v][k]);            }        }    }}int main(){    while(scanf("%d%d", &N, &M), N!=-1||M!=-1)    {        int a, b;        memset(dp, 0, sizeof(dp));        for(int i = 1; i <= N; i++)        {            G[i].clear();            scanf("%d%d", &num[i], &val[i]);            if(num[i] % 20 == 0)//求出每个洞穴需要士兵的数目                num[i] /= 20;            else                num[i] = num[i] / 20 + 1;        }        for(int i = 1; i < N; i++)        {            scanf("%d%d", &a, &b);            G[a].push_back(b);            G[b].push_back(a);        }        if(M == 0)//单独判断        {            printf("0\n");            continue;        }        DFS(1, -1);        printf("%d\n", dp[1][M]);    }    return 0;}










0 0
原创粉丝点击