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.
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.
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
一开始题目意思都没搞懂。。。
塔防游戏,有一棵树,怪物从节点1出发,随机走到一个子节点,再往下走,一直走到叶子节点。每个节点可以装一些炮,有各自的花费和打怪的血。问最坏情况下最多能打掉多少血。
解法是先对每个子节点做分组背包,对根节点可能的选择再做一次分组背包(只有一组)。
用dp[u][c]表示节点u有c的钱,它和它的子树最坏情况下最多能能打掉的血。做分组背包的时候,若在当前组选择花费j,则当前树中打掉血的最坏情况是min(dp[u][c-j],dp[v][j]),对于每个dp[u][c],它等于所有min(dp[u][c-j],dp[v][j])里的最大值。分组背包后再对根节点做分组背包,这里要注意的是可能会有花费为0的武器,为了避免这种可能在同一组选了多个情况,要先用一个temp数组保存初始dp值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
#define MAXN 1010
#define MAXM 210
#define MAXNODE 4*MAXN
#define pii pair<int,int>
using namespace std;
int T,N,W,K,dp[MAXN][MAXM],vis[MAXN],temp[MAXN];
vector<pii> tower[MAXN];
vector<int> V[MAXN];
void DFS(int u){
vis[u]=1;
int flag=0;
for(int i=0;i<V[u].size();i++){
int v=V[u][i];
if(vis[v]) continue;
flag=1;
DFS(v);
for(int c=W;c>=0;c--){
int t=0;
for(int j=0;j<=c;j++) t=max(t,min(dp[u][c-j],dp[v][j]));
dp[u][c]=t;
}
}
if(!flag) memset(dp[u],0,sizeof(dp[u]));
for(int i=0;i<=W;i++) temp[i]=dp[u][i];
for(int c=W;c>=0;c--)
for(int i=0;i<tower[u].size();i++) if(tower[u][i].first<=c) dp[u][c]=max(dp[u][c],temp[c-tower[u][i].first]+tower[u][i].second);
}
int main(){
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(int i=1;i<=N;i++){
V[i].clear();
tower[i].clear();
}
for(int i=0;i<N-1;i++){
int u,v;
scanf("%d%d",&u,&v);
V[u].push_back(v);
V[v].push_back(u);
}
scanf("%d",&W);
for(int i=1;i<=N;i++){
scanf("%d",&K);
while(K--){
int a,b;
scanf("%d%d",&a,&b);
tower[i].push_back(make_pair(a,b));
}
}
memset(vis,0,sizeof(vis));
memset(dp,INF,sizeof(dp));
DFS(1);
printf("%d\n",dp[1][W]);
}
return 0;
}