hdu1011----Starship Troopers

Source: Internet
Author: User

Starship Troopers Time limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 11797 Accepted Submission (s): 3256


Problem Descriptionyou, the leader of Starship Troopers, is 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 are occupied by some bugs, and their brains hide in some of the rooms. Scientists has 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 are always easier than to capture their brains. A map is drawn for your, 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-a-path, there is one unique paths leading to each of the entrance. To-finish the battle as soon as possible, you don't want to wait for the troopers to clear a class before advancing to th e Next one, instead you has to leave some troopers at each of the passed to fight all the bugs inside. The troopers never re-enter a, where they has visited before.

A Starship Trooper can fight against bugs. Since you don't 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 for the brains taken. Making Such a plan is a difficult job. You need the a computer.

Inputthe input contains several test cases. The first line of all test case contains integers n (0 < n <=) and M (0 <= M <=), which is the n Umber 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 non-negative integers-the amount of bugs inside and the possibility of containing a brain, respe Ctively. The next N-1 lines give the description of tunnels. Each tunnel was described by and integers, which is the indices of the-rooms it connects. Rooms is numbered from 1 and the other is the cavern of the entrance to the.

The last test case was 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 Tak En rooms.

Sample Input
5 1050 1040 1040 2065 3070 301 21 32 42 51 120 7-1-1

Sample Output
507

Authorxu, Chuan
SourceZJCPC2004
recommendjgshining | We have carefully selected several similar problems for you:2955 2415 1059 1712 3033

Statistic | Submit | Discuss | Note


Tree DP, Dp[u][i] represents the optimal solution that a sub-tree can get by sending I soldiers to attack u


Dp[u][i] = max (Dp[v][j] + dp[u][i-j])


/************************************************************************* > File Name:hdu1011.cpp > Author: ALex > Mail: [email protected] > Created time:2015 January 03 Saturday 14:41 19 Seconds ******************************* /#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int N = 150;int Dp[n][n]; int Pro[n];int bug[n];int N, m;vector <int> edge[n];void dfs (int u, int fa) {int tmp = Bug[u]/20;if (Bug[u]% &A mp;& Bug[u]) {++tmp;} for (int i = tmp; I <= m; ++i) {dp[u][i] = Pro[u];} int size = Edge[u].size (); for (int i = 0; i < size; ++i) {int v = edge[u][i];if (v = = FA) {continue;} DFS (v, u); for (int j = m; j >= tmp;--j)//bug[u] used to subdue U node {for (int k = 1; k <= j-tmp; ++k)//Note K starts from 1,Because every room has at least one soldier {dp[u][j] = max (Dp[u][j], dp[v][k] + dp[u][j-k]);}}} int main () {int u, v;while (~scanf ("%d%d", &n, &m)) {if (n = =-1 && m = =-1) {break;} memset (DP, 0, sizeof (DP)), for (int i = 1; I <= n; ++i) {edge[i].clear (); scanf ("%d%d", &bug[i], &pro[i]);}  for (int i = 1; I <= n-1; ++i) {scanf ("%d%d", &u, &v); Edge[u].push_back (v); Edge[v].push_back (u);} if (M = = 0) {printf ("0\n"); continue;} DFS (1,-1);p rintf ("%d\n", Dp[1][m]);} return 0;}


hdu1011----Starship Troopers

Related Keywords:

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.