题意:

有n个洞,连接像一棵树,每个包含一定数量的怪和价值,给你m个士兵,每个士兵能打20个怪,杀完一个洞的怪可得该洞的价值才可继续打相连的下面的洞(每个士兵只能打一个洞),求获得的最大价值。

分析:把士兵数看做容量,就是背包问题,dp[i][j]以i为根节点子树士兵数j时获得的最大价值(根节点有一定的士兵,要先打通根节点)1是根节点

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
vector<int>e[];
int used[],dp[][],p[],b[];
int n,m;
void dfs(int root){
int tmp=(b[root]+)/;//打通根节点需士兵数
for(int k=tmp;k<=m;++k)
dp[root][k]=p[root];
used[root]=;
for(int i=;i<e[root].size();++i){
int son=e[root][i];
if(used[son])continue;
dfs(son);
for(int j=m;j>=tmp;--j)
for(int l=;l<=j-tmp;++l)
dp[root][j]=max(dp[root][j],dp[root][j-l]+dp[son][l]);
}
}
int main()
{
while(~scanf("%d%d",&n,&m)){
if(n==-&&m==-)break;
for(int i=;i<=n;++i){
scanf("%d%d",&b[i],&p[i]);
e[i].clear();
}
memset(dp,,sizeof(dp));
memset(used,,sizeof(used));
int u,v;
for(int i=;i<n-;++i){
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
if(m==)printf("0\n");
else{
dfs();
printf("%d\n",dp[][m]);
}
}
return ;
}

HDU 1011-Starship Troopers(树形背包)的更多相关文章

  1. hdu 1011 Starship Troopers(树形背包)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. hdu 1011 Starship Troopers 树形背包dp

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. HDU 1011 Starship Troopers 树形+背包dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1011   题意:每个节点有两个值bug和brain,当清扫该节点的所有bug时就得到brain值,只有当父节点被 ...

  4. hdu 1011 Starship Troopers(树形DP入门)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. hdu 1011 Starship Troopers(树上背包)

    Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...

  6. [HDU 1011] Starship Troopers (树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 dp[u][i]为以u为根节点的,花了不超过i元钱能够得到的最大价值 因为题目里说要访问子节点必 ...

  7. HDU 1011 Starship Troopers 树形DP 有坑点

    本来是一道很水的树形DP题 设dp[i][j]表示,带着j个人去攻打以节点i为根的子树的最大收益 结果wa了一整晚 原因: 坑点1: 即使这个节点里面没有守卫,你如果想获得这个节点的收益,你还是必须派 ...

  8. HDU 1011 Starship Troopers【树形DP/有依赖的01背包】

    You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built unde ...

  9. hdu 1011(Starship Troopers,树形dp)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  10. hdu 1011 Starship Troopers 经典的树形DP ****

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. fzu 2105 Digits Count ( 线段树 ) from 第三届福建省大学生程序设计竞赛

    http://acm.fzu.edu.cn/problem.php?pid=2105 Problem Description Given N integers A={A[0],A[1],...,A[N ...

  2. query specified join fetching, but the owner of the fetched association was not present in the select list

    报标题的错误,是因为在 select count的时候,不需要fetch 所以在取完count以后,再把fetch加进去,变成left join  fetch /** * 请把jhql以o为返回对象, ...

  3. 3111: [Zjoi2013]蚂蚁寻路 - BZOJ

    题目描述 Description在一个 n*m 的棋盘上,每个格子有一个权值,初始时,在某个格子的顶点处一只面朝北的蚂蚁,我们只知道它的行走路线是如何转弯,却不知道每次转弯前走了多长.蚂蚁转弯是有一定 ...

  4. eclipse中切换jre后报错:Java compiler level does not match the version of the installed Java project facet.

    项目移除原来的jre环境lib后,添加本地的jre,报错如下: Java compiler level does not match the version of the installed Java ...

  5. C#取中间文本

    /// <summary> /// 取中间文本 + static string GetMiddleStr(string oldStr,string preStr,string nextSt ...

  6. easyui 使特定tab处于选中状态

    <div id="tabs" class="easyui-tabs"> <div title="Tab1" style=& ...

  7. A geometric interpretation of the covariance matrix

    A geometric interpretation of the covariance matrix Contents [hide] 1 Introduction 2 Eigendecomposit ...

  8. spoj 346

    当一个数大于等于12  那分别处以2, 3, 4之后的和一定大于本身    但是直接递归会超时    然后发现有人用map存了   膜拜..... #include <cstdio> #i ...

  9. spoj 24

    大数  #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> ...

  10. 用Vue.js和Webpack开发Web在线钢琴

    缘起 由于童心未泯,之前在手机上玩过钢琴模拟App,但是手机屏幕太小,始终觉得不过瘾.其实对于我这个连基本乐理都不懂的"乐盲"来说,就算给我一台真正的钢琴,我也玩不转.不过是图个新 ...