这是一道树形DP的背包问题。 纠结了好久,一直WA,今天终于A掉了,泪流满面啊。。。。。。从中发现自己对于细节的处理实在是太糟糕了。找了一天的错误,最后发现错了一个小条件。
这道题目大致的意思是,你有多名队员,每一个队员可以消灭20只bugs,每个房间有一定数量的bugs,并且存在一个收益值,最终你要获得最大的收益。
这里要注意的地方是中间节点消耗为0,你可以走过,不留队员直接获得收益,而对于叶子节点必须消耗队员才能获得收益。如果叶子处没有队员那么就等于没有队员去过,就无法获得收益。这里需要好好想想,这很重要。
对于每一个节点而言,求最大收益,相当于一个分组背包。每个子节点为一组,有多种方案,之间互斥。
Starship Troopers
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4965 Accepted Submission(s): 1305
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.
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.
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
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<string> #include<vector> using namespace std; vector<int> node[110]; bool visited[110]; int dp[110][110]; //用于存放以i为跟节点的物品组合 int dp2[110][110]; //临时 int n,m; int c[1100]; //用于存放每个房间的消耗 int w[1100]; //每个房间存在大脑的可能性 int max(int a,int b) { return a>b?a:b; } void dfs(int root) { int cost=(c[root]%20==0?c[root]/20:c[root]/20+1); //初始化每个节点的消耗 visited[root]=true; //dp2[root][0]=dp[root][0]=0; for(int i=0;i<node[root].size();i++) //对于一个节点,他的每个子节点的各种方案组合相当于多个互斥的方案选择 因此是一个分组背包问题 { int temp=node[root][i]; if(!visited[temp]) dfs(temp); else continue; for(int j=m;j>=0;j--) { for(int k=1;k<=m;k++) //此处循环从1开始,是因为下面的路至少要有多余的队员才能继续获得收益,如果没有队员怎么继续获得下面的收益? { //如果从0开始循环,也就是说下面的分支就算没有队员经过,但却获得了收益。显然违背了题意。此处是关键 //在这里纠结了很久,最后才发现 if(j<k) break; dp2[root][j]=max(dp2[root][j],dp2[root][j-k]+dp[temp][k]); } } } for(int i=cost;i<=m;i++) dp[root][i]=dp2[root][i-cost]+w[root]; } void init() { memset(dp,0,sizeof(dp)); memset(dp2,0,sizeof(dp2)); for(int i=1;i<=n;i++) { node[i].clear(); visited[i]=false; } for(int i=1;i<=n;i++) { int a,b; cin>>a>>b; c[i]=a; w[i]=b; } for(int i=2;i<=n;i++) { int a,b; cin>>a>>b; node[a].push_back(b); node[b].push_back(a); } } int main() { while(cin>>n>>m ) { if(n==-1&&m==-1) break; init(); dfs(1); int ans=0; if(m==0) cout<<"0"<<endl; else cout<<dp[1][m]<<endl; } return 0; }