HDU Starship Troopers (树形DP)
Starship Troopers
Time Limit : 10000/5000ms (Java/Other)???Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1???Accepted Submission(s) : 1
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 -1Sample Output
50 7Author
XU, ChuanSource
ZJCPC2004題目大意:給出一張樹形的地圖,然后每個節(jié)點就是一個洞,每個洞里面有bugs和brain,現(xiàn)在給出我方有m個騎兵,每個騎兵可以消滅20個bugs,經(jīng)過的洞穴不能再回頭,要收服每個洞穴里面的brain就必須消滅每個洞穴里面的全部bugs,求最大的brain
?
思路:樹形DP.
對于x個節(jié)點來說.dp[x][i]表示在x洞穴用i個騎兵可以收服的brain的數(shù)目.其實就是01背包的稍加變形.
for(i=1;i<=n;i++)
? for(int j=m;j>=r;j--)
????? for(int k=1;k<=j-r;j++)??????????????????????????????????? //k<=j-r 即是:k+r<=j,即等會用于:對于當(dāng)前節(jié)點使用剩下的騎兵k在子節(jié)點能夠收服的brain值.
????????????? dp[x][j]=max(dp[x][j],dp[x][j-k]+dp[i][k]);// i是x的子節(jié)點.這就是說:x節(jié)點收服brain的最大值等于:? max(本身初始化的值 ,本身+剩下騎兵的情況下子節(jié)點收服的brain值)
整個過程DFS下去再回朔,不用標(biāo)記,因為這是樹,只需要標(biāo)記下前綴就可以了.因為數(shù)據(jù)有可能是:反樹的方向給出,
?
#include<iostream> #include<cstdio> #include<cstring>using namespace std;const int VM=110;struct Edge{int to,nxt; }edge[VM<<1];int n,m,cnt,head[VM]; int vis[VM],bug[VM],brain[VM],dp[VM][VM]; //dp[x][j]表示在i洞派出j個騎兵獲取的brain值 void addedge(int cu,int cv){ //樹建立雙向邊,是為了保證兩點的連通edge[cnt].to=cv; edge[cnt].nxt=head[cu];head[cu]=cnt++;edge[cnt].to=cu; edge[cnt].nxt=head[cv];head[cv]=cnt++; }void DFS(int u){if(vis[u])return ;vis[u]=1;int num=(bug[u]+19)/20;for(int j=m;j>=num;j--) //初始化很重要,因為是邊界條件dp[u][j]=brain[u]; //進(jìn)入這個洞,只要派出的人數(shù)不小于這個洞的bug的 ,都可以至少獲得這個洞brain值這么大的brainfor(int i=head[u];i!=-1;i=edge[i].nxt){int v=edge[i].to;if(!vis[v]){DFS(v);for(int j=m;j>=num;j--)for(int k=1;k<=j-num;k++)dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);}} }int main(){freopen("input.txt","r",stdin);while(~scanf("%d%d",&n,&m)){if(n==-1 && m==-1)break;cnt=0;memset(head,-1,sizeof(head));for(int i=1;i<=n;i++)scanf("%d%d",&bug[i],&brain[i]);int u,v;for(int i=1;i<n;i++){scanf("%d%d",&u,&v);addedge(u,v);}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; }?
?
總結(jié)
以上是生活随笔為你收集整理的HDU Starship Troopers (树形DP)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 2152 选课时间(题目已修
- 下一篇: 用vim 配置javascript