hdu 1011树形dp

来源:互联网 发布:xmind8 for mac序列号 编辑:程序博客网 时间:2024/05/16 23:51

Starship Troopers

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


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.
 


题意:

是有n个洞组成一棵树,你有m个士兵,你从1号房间开始攻打,每个洞有a个"bugs"和b的价值。你的一个士兵可以打20个"bugs",为了拿到这个洞的价值b你必须留下k个士兵消灭这个洞的所有"bugs"(k*20>="bugs"的数量,且留下的士兵不可以再去攻打其他的洞,且必须攻打了前面的洞才可以攻打后面的洞)。问你花费这m个士兵可以得到的最大价值是多少。 (cost[i]=(bugs+19)/20; value[i]=b[i];)
dp[i][j]表示以i节点为根j个士兵的最大价值;
那么dp[i][j]=max(dp[i][j],dp[i][j-k]+dp[v][k]),

其中v是i的每个儿子节点,k<=j-cost[i];(必须留下cost[i]去消除i节点的bugs);

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;int visit[111];int cost[111];int value[111];int dp[111][111];int n,m;vector < int >v[111];inline int max(int a,int b){return a>b? a:b;}void dfs(int n){    int i,j,k,l,o;    k=(cost[n]+19)/20;    l=v[n].size();    visit[n]=1;    for(i=k; i<=m; i++)    {        dp[n][i]=value[n];    }    for(i=0; i<l; i++)    {        int t=v[n][i];        if(visit[t])            continue;        dfs(t);        for(j=m;j>=k;j--)        {            for(o=1;o<=j-k;o++)            {                dp[n][j]=max(dp[n][j],dp[n][j-o]+dp[t][o]);            }        }    }}int main(){    int i,j,k,l;    while(cin>>n>>m)    {        if(n==-1&&m==-1)            break;        for(i=1; i<=n; i++)            v[i].clear();        memset(dp,0,sizeof(dp));        memset(visit,0,sizeof(visit));        for(i=1; i<=n; i++)        {            cin>>cost[i];            cin>>value[i];        }        for(i=1; i<n; i++)        {            cin>>k>>l;            v[k].push_back(l);            v[l].push_back(k);        }        if(m==0)        {            cout<<0<<endl;            continue;        }        dfs(1);        cout<<dp[1][m]<<endl;    }}


0 0
原创粉丝点击