HDU 4044 - GeoDefense

GeoDefense

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


Problem Description
Tower 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. Many games, such as Flash Element Tower Defense, feature enemies that run through a "maze", which allows the player to strategically place towers for optimal effectiveness. 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 Without". 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 effectiveness so that the fortifications 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 effective and if the enemy is shot to death in your bases, you still win.
 
Input
The 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.
 
Output
For each test case, output a line containing the highest HP value of your enemy that you can deal with. It means that if your enemy’s HP is larger than that highest value, you can’t guarantee your victory.
 
Sample Input
2
2
1 2
30
3 10 20 20 40 30 50
3 10 30 20 40 30 45
4
2 1
3 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 30 35
 
Sample Output
70
80
 
感觉这题好变态,
题意:
  从根节点出一组敌人,叶子节点是我们的家,在每个节点可以放上大炮,每个节点有k个大炮,最多装一个,有一个花费值和威力值,
你一共有m钱,求最大威力值。敌人攻击的路径是任意的,所以最大威力是每条路值和的最小值。大炮放在叶子节点也是管用的。
思路:
  分组背包,是两个分组背包,一个是树与子树,一个是自己本身的分组背包,解释在代码里
AC代码:
 1 # include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int inf = 1<<30;
 5 const int MAX = 1009;
 6 const int MAX2 = 209;
 7 
 8 int dp[MAX][MAX2]; //dp[i][j] i为根的子树 花费j最大值
 9 int dp2[MAX][MAX2]; //dp2[i][j] 第i节点 花费j最大值
10 int n, m;
11 vector <int> v[MAX];
12 
13 void dfs(int root, int f)
14 {
15     int len = v[root].size();
16     // 结束条件
17     if(len == 1 && root != 1) //叶子节点
18     {
19         for(int i = 0; i <= m; i++) 
20             dp[root][i] = dp2[root][i];
21         return;
22     }
23     // 初始为最大
24     for(int i = 0; i <= m; i++) 
25         dp[root][i] = inf;
26     for(int i = 0; i < len; i++)
27     {
28         int son = v[root][i];
29         if(son == f) 
30             continue;
31         dfs(son, root);
32         // 分组背包
33         for(int j = m; j >= 0; j--) //树与子树之间的分组背包,子树花费和是j
34         {
35             int t = 0;
36             for(int k = 0; k <= j; k++) //son子树花费是k
37                 t = max(t, min(dp[root][j - k], dp[son][k])); //各个子树威力值最小的
38             dp[root][j] = t;
39         }
40     }
41 
42     for(int j = m; j >= 0; j--) //树与该树根节点之间的分组背包
43         for(int k = 0; k <= j; k++)
44             dp[root][j] = max(dp[root][j], dp[root][j - k] + dp2[root][k]);
45 }
46 int main()
47 {
48     int t;
49     scanf("%d", &t);
50     while(t--)
51     {
52         scanf("%d", &n);
53         for(int i = 1; i <= n; i++) 
54             v[i].clear();
55         memset(dp2, 0, sizeof(dp2));
56         
57         for(int i = 1; i < n; i++)
58         {
59             int x, y;
60             scanf("%d%d", &x, &y);
61             v[x].push_back(y);
62             v[y].push_back(x);
63         }
64         scanf("%d", &m);
65         for(int i = 1; i <= n; i++)
66         {
67             int k, x, y;
68             scanf("%d", &k);
69             for(int j = 0; j < k; j++)
70             {
71                 scanf("%d%d", &x, &y);
72                 dp2[i][x] = max(dp2[i][x], y);
73             }
74         }
75         //求i节点花费j所对应的最大值
76         for(int i = 1; i <= n; i++)
77             for(int j = 1; j <= m; j++)
78                 dp2[i][j] = max(dp2[i][j], dp2[i][j - 1]);
79         
80         dfs(1, -1);
81 
82         printf("%d\n", dp[1][m]);
83     }
84     return 0;
85 }
View Code

 

posted @ 2016-08-28 16:16  stort  阅读(214)  评论(0编辑  收藏  举报