Hdu1011 Starship Troopers tree DP

Source: Internet
Author: User
This is a tree-like DP backpack problem. After a long time of struggle, WA finally fell down today and burst into tears ...... It turns out that it is too bad to handle the details. I found a one-day error and found a small condition.

The general meaning of this question is that you have multiple players, each of which can destroy 20 buckets, each room has a certain number of buckets, and there is a benefit value, in the end, you need to get the maximum benefit.

Note that the consumption of intermediate nodes is 0. You can pass through without leaving a team member to directly obtain the profit. For leaf nodes, you must consume the team member to obtain the profit. If no team member is on the leaf, it means no team member has been there, and no benefit can be obtained. It is important to think about it here.

 

For each node, the greatest benefit is equivalent to a group backpack. Each sub-node is a group with multiple solutions, which are mutually exclusive.

 

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 descriptionyou, 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 specified 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 your 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 trow.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 shoshould maximize the possibility of capturing a brain. to simplify
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.

 

Inputthe 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.

 

Outputfor 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

 

# 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]; // used to store the int dp2 [110] [110] combination of items with I as the node; // temporary int n, m; int C [1100]; // int W [1100] used to store the consumption of each room; // the possibility that each room has a brain 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); // initialize the consumption of each node visited [root] = true; // dp2 [root] [0] = DP [root] [0] = 0; For (INT I = 0; I <node [root]. size (); I ++) // For a node, the combination of various schemes for each of his sub-nodes is equivalent to multiple mutually exclusive schemes. Therefore, it is a grouping backpack problem {int temp = node [root] [I]; If (! Visited [temp]) DFS (temp); elsecontinue; For (Int J = m; j> = 0; j --) {for (int K = 1; k <= m; k ++) // here, the cycle starts from 1, because the following path requires only a few excess players to continue to earn benefits. How can we continue to earn the following benefits if no players are there? {// If the cycle starts from 0, that is to say, even if the following branch does not pass by the players, it gains benefits. Apparently, it violates the meaning of the question. The key here is the key // It has been entangled for a long time, and the final result is that 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; elsecout <DP [1] [m] <Endl;} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.