Starship Troopers
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23205 Accepted Submission(s): 6168
Problem Description
You, 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 many 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 many 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 trooper 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 should maximize the possibility of capturing a brain. To simplify the 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.
Input
The 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.
Output
For 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 10
50 10
40 10
40 20
65 30
70 30
1 2
1 3
2 4
2 5
1 1
20 7
-1 -1
Sample Output
50
7
Author
XU, Chuan
Source
Recommend
JGShining
Statistic | Submit | Discuss | Note
算法实现:
题意:
有n个洞组成一棵树,你有m个士兵,你从1号房间开始攻打,每个洞有a个"bugs"和b的价值。你的一个士兵可以打20个"bugs",为了拿到这个洞的价值b你必须留下k个士兵消灭这个洞的所有"bugs"(k*20>="bugs"的数量,且留下的士兵不可以再去攻打其他的洞,且必须攻打了前面的洞才可以攻打后面的洞)。问你花费这m个士兵可以得到的最大价值是多少。
分析:
我们可以知道每个节点的花费cost(i) = (x(i)+19)/20。
那么,
f(i, j):表示i子树,用j个战士最多可以获得的价值。
如果i有s个儿子节点,那么就形成了s组的物品,对每组物品进行分组背包。
每一组可以选择派1,2...j-cost(i)个战士去,为什么最多是j-cost(i)?因为还要留下cost(i)个战士消灭当前洞穴的虫子。
这样就可以得到状态转移了:
f(i, j) = max { max{ f(i, j-k) + f(v, k) | 1<=k<=j-cost(i) } | v是i的儿子节点 }
这题要特别注意的是,如果是叶子节点,并且叶子节点的花费为0,那么要让他的花费变为1,因为必须派一个战士走向叶子节点才可以获得金子.
链式前向星建树代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=310;
struct node
{
int v;///终端点
int next;///下一条同样起点的边号
int w;///权值
}edge[N*2];///无向边,2倍
int head[N];///head[u]=i表示以u为起点的所有边中的第一条边是 i号边
int tot; ///总边数
int minn;
void add(int u,int v)
{
edge[tot].v=v;
//edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
struct Node {
int num,cost, val;
}room[N];
int n,m;
int dp[N][N];
void dfs(int u,int fa)
{
for (int i=room[u].cost; i<=m; ++i) dp[u][i] = room[u].val;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(fa==v) continue; ///如果下一个相邻节点就是父节点,则证明到底层了,开始递归父节点的兄弟节点
dfs(v,u);
for (int s = m; s >= room[u].cost; --s)
for (int j = 1; s-j >= room[u].cost; ++j)
dp[u][s] = max(dp[u][s], dp[u][s-j] + dp[v][j]);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(head,-1,sizeof(head));
memset(dp,0,sizeof(dp));
tot=0;
if(n==-1&&m==-1) break;
for (int i = 1; i <= n; ++i)
{
int x;
scanf("%d%d", &x, &room[i].val);
room[i].cost = x/20 + (x%20!=0);
}
for(int i=1;i<n;i++)
{
int u,w,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);///注意题目给出第一个不一定是父亲结点
}
if (m==0)
{ // 别忘了特判,有这样的数据!!!
printf("0\n");continue;
}
dfs(1,-1);
printf("%d\n",dp[1][m]);
}
return 0;
}
vector建树代码分析:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = 110;
int n, m;
struct Node {
int num,cost, val;
}room[MAXN];
int f[MAXN][MAXN];
bool vis[MAXN];
vector<int>a[MAXN];
void dfs(int u) {
vis[u] = true;
memset(f[u], 0, sizeof(f[u]));
for (int i = room[u].cost; i <= m; ++i)
f[u][i] = room[u].val;
for (int i = 0; i < a[u].size(); ++i) {
int v = a[u][i];
if (vis[v]) continue;
dfs(v);
for (int s = m; s >= room[u].cost; --s) {
for (int j = 1; s-j >= room[u].cost; ++j)
f[u][s] = max(f[u][s], f[u][s-j] + f[v][j]);
}
}
}
int main(){
while (~scanf("%d %d", &n, &m) ) {
if(n==-1&&m==-1) break;
for (int i = 1; i <= n; ++i)
a[i].clear();
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d%d", &x, &room[i].val);
room[i].cost = x/20 + (x%20!=0);///cost为0时也需要一名战士
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d %d", &u, &v);
a[u].push_back(v);
a[v].push_back(u);
}
if (m==0) {
cout<<0<<endl;
continue;
}
memset(vis, 0, sizeof(vis));
vis[1] = true;
dfs(1);
printf("%d\n", f[1][m]);
}
return 0;
}
标签:cost,room,int,bugs,20,树形,hdu1011Starship,include,Troopers From: https://blog.51cto.com/u_14932227/6042473