首页 > 其他分享 >树形背包 hdu1011Starship Troopers

树形背包 hdu1011Starship Troopers

时间:2023-02-07 17:03:02浏览次数:61  
标签:cost room int bugs 20 树形 hdu1011Starship include Troopers


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

​ZJCPC2004 ​

 

 

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

相关文章

  • 树形背包 POJ1155TELE
    题目描述某收费有线电视网计划转播一场重要的足球比赛。他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内......
  • (树形DP+背包)POJ1947Rebuilding Roads
    RebuildingRoadsTimeLimit: 1000MS MemoryLimit: 30000KTotalSubmissions: 13307 Accepted: 6171DescriptionThecowshavereconstructedFarmerJohn'sfarm,w......
  • 洛谷 P2014 选课 树形依赖背包
    题目描述在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习。现在有N门功......
  • 树形DP依赖背包 洛谷 P2015 二叉苹果树
    题目描述有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点)这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1。我们用一根树枝两端连接的......
  • 树形DP (cf 219D Choosing Capital for Treeland)
    题意翻译题目描述Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市。每条道路只能单向通行。现在政府需要决定选择哪个城市为首都。假如城市i成为了首都......
  • 树形DP+概率DP
    动态规划报告树形dp树形DP,即在树上进行的DP。由于树固有的递归性质,树形DP一般都是递归进行的。一般需要在遍历树的同时维护所需的信息以一道题目为例2022CCPC桂林......
  • vue3 + ts 封装树形控件
    vue3+ts封装树形控件父组件调用 <TreeFilter label="name" title="部门列表(单选)" :requestApi="getUserDepartment" :defaultValue="treeFilterValue.depar......
  • 数组数据转化树形结构数据
    functiontranslateDataToTree(data){​letparents=data.filter(value=>value.parentId=='undefined'||value.parentId==null)​letchildren=data.filt......
  • 自增树形组件
    <viewclass="tree_Box"><!--有多个--><viewclass="tree"a:for="{{datas}}"a:key="{{item.id}}"><viewclass="tree_childern_item"a:if......
  • 树形dp
    大佬博客由于树形dp种类繁多,而且大佬博客中总结的很好,这里我就只记录下我写到的树形dp《F-Components》  简单的来说就是:给一颗有n个节点的树,因......