HDU 4044 GeoDefense (tree DP + grouping backpack)

Source: Internet
Author: User
Tags pc world
GeoDefense

Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 347 Accepted Submission (s): 138

Problem DescriptionTower defense is a kind of real-time strategy computer games. the goal of tower defense games is to try to stop enemies from reaching your bases by building towers which shoot at them as they pass.

The choice and positioning of the towers is the essential strategy of the game. when games, such as Flash Element Tower Defense, feature enemies that run through a "maze", which allows the player to strategically place towers for optimal upgrade tiveness. however, some versions of the genre force the user to create the maze out of their own towers, such as Desktop Tower Defense. some versions are a hybrid of these two types, with preset paths that can be modified to some extent by tower placement, or towers that can be modified by path placement.

GeoDefense is a Thinking Man's Action Tower Defense. it has become one of "PC World's 10 iPhone Games You CANNOT Live ". using exciting vectorized graphics, this highly kinetic game brings a whole new dimension to the defense genre. devastate creeps with blasters, lasers and missiles and watch their energy debris swirl through the gravity wells of your vortex towers.

There is a geoDefense maze of n points numbered from 1 and connected by passageways. there are at least two dead ends among these n points, and there is always one and only one path between any pair of points. point 1 is a dead end, and it's the base of enemies, and all the other dead ends are your bases.

To prevent the enemy reaching your bases, you have to construct towers to attack the enemy. you can build tower on any point and you can only build one tower on one point. A tower can only shot the enemy when it passes the tower. you are given ki choices to build tower on point I, and each choice is given in the format of (price, power) which means that you can build a tower with attack power value equals power in the cost of price. you can also build nothing on a point so it will not cost your money. A tower will reduce the enemy's HP by its attack power. when the HP is less or equal to zero, the enemy dies immediately.

The base of enemies will release only one enemy. it moves very fast that you cannot do anything such as building towers while it is running. it runs all the way until it dies or reaches one of your bases. however, you cannot predict the route it will go through. to win the game, you must kill the enemy before it reaches your bases. you have to strategically place towers for optimal upgrade tiveness so that the forications ications are steady enough to protect the bold and powerful enemy with high HP. you are troubling your head on figuring out the highest HP of the enemy you are able to kill on the way certainly. you have money m when the game begins.
Please note that the towers build in the enemy's base or your bases are all valid tive and if the enemy is shot to death in your bases, you still win.

 

InputThe input consists of several test cases. The first line is an integer T (1 <=t <= 20), which shows the number of the cases.
For each test case, the first line contains only one integer n (2 <= n <= 1000) meaning the number of points.
The following n-1 lines describe the passageways. Each line contains two integers u and v, which are the endpoints of a passageway.
The following line contains only one integer m (1 <= m <= 200) meaning the amount of your money when the game begins.
Then n lines follow. the ith line describes the construction choices of the ith point. it starts with an integer ki (0 <= ki <= 50) and ki is followed by ki pairs of integers separated by spaces. the jth pair is (pricei, j, poweri, j), 0 <= pricei, j <= 200, 0 <= poweri, j <= 50000. ki being zero means that you can't build a tower on the ith point.

 

OutputFor each test case, output a line containing the highest HP value of your enemy that you can deal. it means that if your enemy's HP is larger than that highest value, you can't guarantee your vicur.

 

Sample Input2 2 1 2 30 3 10 20 40 30 50 3 10 30 20 40 45 4 2 1 1 1 4 60 3 10 20 20 40 30 50 3 10 30 20 40 30 45 3 10 30 20 40 30 35 3 10 30 20 40 35

 

Sample Output70 80

 

SourceThe 36th ACM/ICPC Asia Regional Beijing Site -- Online Contest

 

The Recommendlcy question looks scary ~~~~ Given a tree composed of n nodes, 1 is an enemy base, and the leaf node is our node. We can place one or more guns on each node, and then hit the gun. Each node has a ki gun, each type of gun has a cost and an energy (the energy corresponds to how much hp the enemy has ). The enemy may run to each branch of a node. Therefore, to maintain the position, it is necessary to ensure that each branch has a bastion host. Finally, I asked how to crack the gun to maximize the number of hp attacks.

Solution: tree-like DP + grouping backpack. to hold our base, we only need to fight from the enemy base and let the blame die on the way to our base, so that we can start status transfer from root 1. To ensure that each of our bases is stopped, we need to find the minimum hp that is destroyed by the son node when the capacity of each node is j, in this case, you can use the son node dp [v] [j] (dp [I] [j] to indicate that the maximum hp of the I node is destroyed by j charges) as an item, each v corresponds to a group of backpacks, and then computes max (t, min (dp [son] [j-k], dp [v] [k]) to obtain a combination, the capacity is j, and the minimum value is the maximum. The value is assigned to dp [I] [j].

 

One problem is that there are items with a price of 0. Therefore, the simple reverse DP cannot ensure that only one item is selected. You need to open an array.

/* HDU 4044 tree-like DP + grouping backpack */# include <stdio. h> # include <iostream> # include <string. h ># include <algorithm> using namespace std; const int MAXN = 1010; const int INF = 0x3fffffff; struct Node {int to; int next;} edge [MAXN * 2]; int tol; int head [MAXN]; int dp [MAXN] [220]; int price [MAXN] [60]; int power [MAXN] [60]; void init () {memset (head,-1, sizeof (head); tol = 0;} void add (int a, int B) {edge [tol]. to = B; edge [tol]. next = head [a]; head [A] = tol ++; edge [tol]. to = a; edge [tol]. next = head [B]; head [B] = tol ++;} int n, m; int tmp [MAXN]; void dfs (int u, int pre) {if (head [u] =-1 | (edge [head [u]. to = pre & edge [head [u]. next =-1) // leaf node {// The condition of the leaf node should not be wrong !!!! For (int I = 0; I <= m; I ++) dp [u] [I] = 0; for (int I = 0; I <= m; I ++) tmp [I] = dp [u] [I]; // The price is 0. Therefore, the reverse DP cannot guarantee that only one item is taken for (int I = m; I> = 0; I --) {for (int j = 1; j <= price [u] [0]; j ++) if (price [u] [j] <= I) dp [u] [I] = max (dp [u] [I], tmp [I-price [u] [j] + power [u] [j]); tmp [I] = dp [u] [I]; // The tmp array is the previous status of the recorded dp} return;} for (int I = 0; I <= m; I ++) dp [u] [I] = INF; for (int I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. to; if (v = pre) continue; dfs (v, u); for (int j = m; j> = 0; j --) {int t = 0; for (int k = 0; k <= j; k ++) // here k must start from 0. T = max (t, min (dp [u] [j-k], dp [v] [k]); dp [u] [j] = t ;}} for (int I = 0; I <= m; I ++) tmp [I] = dp [u] [I]; for (int I = m; i> = 0; I --) {for (int j = 1; j <= price [u] [0]; j ++) if (price [u] [j] <= I) dp [u] [I] = max (dp [u] [I], tmp [I-price [u] [j] + power [u] [j]); // Add a tmp array tmp [I] = dp [u] [I];} int main () {// freopen ("in.txt ", "r", stdin); // freopen ("out.txt", "w", stdout); int u, v; int T; scanf ("% d ", & T); while (T --) {init (); scanf ("% d", & n); for (int I = 1; I <n; I ++) {scanf ("% d", & u, & v); add (u, v) ;}scanf ("% d", & m ); for (int I = 1; I <= n; I ++) {scanf ("% d", & price [I] [0]); power [I] [0] = price [I] [0]; for (int j = 1; j <= price [I] [0]; j ++) {scanf ("% d", & price [I] [j], & power [I] [j]) ;}} dfs (1, 0 ); printf ("% d \ n", dp [1] [m]);} 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.