题意:

n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发)

分析:

该题关键是最后要到达m,没有这个条件,就是基础的树形背包,哎,一开始没思路,放了一段时间,看看题解才明白,该题突破口,就是,你先想怎么判断不能到到达m的情况,自然想到最短路,对树先求一次最短路,在最短路上的点只能,过一次,其他得点过两次,把最短路上的点花费置零,剩下的就是基础的树形背包。

#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>p;
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= <<;
const int mod = ;
int d[],dp[][],head[],n,t,len;
int par[];
struct edge{
int st,en,c,next;
}e[];
void add(int a,int b,int c)
{
e[len].st=a;
e[len].en=b;
e[len].c=c;
e[len].next=head[a];
head[a]=len++;
}
//求最短路
int dijkstra(int s){
priority_queue<p,vector<p>,greater<p> >q;
for(int i=;i<=n;++i)
d[i]=INF;
q.push(p(,s));
d[s]=;
memset(par,-,sizeof(par));
edge a,b;
while(!q.empty()){
p a=q.top();
q.pop();
int j=a.second;
int cost=a.first;
if(d[j]<cost)continue;
for(int i=head[j];i!=-;i=e[i].next){
edge g=e[i];
if(d[g.en]>cost+g.c){
par[g.en]=i;
d[g.en]=cost+g.c;
q.push(p(d[g.en],g.en));
}
}
}
//最短路上的点置零
for(int i=n;i!=s;i=e[par[i]].st){
e[par[i]].c=;
e[par[i]^].c=;
}
return d[n];
}
int used[];
void dfs(int root){
used[root]=;
for(int i=head[root];i!=-;i=e[i].next){
int son=e[i].en;
int cost=e[i].c*;//走两次
if(used[son])continue;
dfs(son);
for(int j=t;j>=cost;--j){
for(int k=;k+cost<=j;++k){
dp[root][j]=max(dp[root][j],dp[root][j-k-cost]+dp[son][k]);
}
}
}
}
int main()
{
int a,b,c;
while(~scanf("%d%d",&n,&t)){
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head));
memset(used,,sizeof(used));
len=;
for(int i=;i<n-;++i){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
for(int i=;i<=n;++i){
scanf("%d",&dp[i][]);
}
t-=dijkstra();
int maxv=;
if(t<)printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
else{
dfs();
for(int i=;i<=t;++i)
maxv=max(maxv,dp[][i]);
printf("%d\n",maxv);
}
}
return ;

HDU 4276-The Ghost Blows Light(树状背包)的更多相关文章

  1. HDU 4276 The Ghost Blows Light

    K - The Ghost Blows Light Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  2. HDU 4276 The Ghost Blows Light(树形)

    题意:给出一棵n个节点的树,起点1,终点n,相连的两个节点之间有距离,每个节点有个价值,给出一个时间T.问从1到达n在给定时间T内取得的最大价值? 思路:先从1走到n,如果总的时间不够走完,直接退出, ...

  3. HDU 4276 The Ghost Blows Light (树形DP,变形)

    题意:给定一棵n个节点的树,起点是1,终点是n,每经过一条边需要消耗Ti天,每个点上有一定量的珠宝,要求必须在t天内到达终点才可以将珠宝带出去,问至多能带多少珠宝? 思路: 注意Ti可以为0,而且有可 ...

  4. HDOJ 4276 The Ghost Blows Light(树形DP)

    Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The tomb consists of N room ...

  5. HDOJ 4276 The Ghost Blows Light

    题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...

  6. HDU 1561-The more, The Better(树状背包)

    题意: n个城堡,每个有一定的财富,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡,求应攻克哪m个城堡,获得最大财富. 分析:dp[i][j],以i为根的子树,攻克j个城堡,获得的 ...

  7. 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)

    The Ghost Blows Light Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The t ...

  8. BNUOJ 26283 The Ghost Blows Light

    The Ghost Blows Light Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...

  9. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  10. hdu 6203 ping ping ping(LCA+树状数组)

    hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...

随机推荐

  1. java和php实现RSA加密互通-b

    java和PHP RSA加密实现互通 1:通过openssl 生成公钥和密钥文件(linux) (1)  生产私钥文件命令 openssl genrsa -out rsa_private_key.pe ...

  2. 微软Hololens学院教程-Hologram 230-空间场景建模(Spatial mapping )【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  3. git/ TortoiseGit 在bitbucket.org 使用证书登陆

    背景:使用https协议在bitbucket中进行pull,push 时每次都要输入密码,比较麻烦还耽误时间,在网上找了下保存密码的方式 使用在用户环境变量中配置_netrc 文件的方式(http:/ ...

  4. centos7 更新yum安装源

    系统自带的yum安装源有些软件找不到  这里我们使用阿里云的源 1.加源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/re ...

  5. DLL搜索路径和DLL劫持

    DLL搜索路径和DLL劫持 环境:XP SP3 VS2005 作者:magictong 为什么要把DLL搜索路径(DLL ORDER)和DLL劫持(DLL Hajack)拿到一起讲呢?呵呵,其实没啥深 ...

  6. linux 使用kill命令杀死进程的几个办法

    常规篇: 首先,用ps查看进程,方法如下: $ ps -ef ……smx       1822     1  0 11:38 ?        00:00:49 gnome-terminalsmx   ...

  7. C++中的INL

    inl文件介绍 inl文件是内联函数的源文件.内联函数通常在C++头文件中实现,但是当C++头文件中内联函数过多的情况下,我们想使头文件看起来简洁点,能不能像普通函数那样将内联函数声明和函数定义放在头 ...

  8. SQL 2008 R2 启动失败 提示 请求失败或服务未及时响应

    为什么启动sql server 配置管理器出现请求失败或服务未及时响应_百度知道 http://zhidao.baidu.com/link?url=ElemzIan6I2CqJsd7-7uk5TV25 ...

  9. UML各种图画法总结

    <UML 2.4.1 教程> http://www.sparxsystems.cn/resources/uml2_tutorial/ <UML总结(对九种图的认识和如何使用Ratio ...

  10. WINCE6.0+ILI9806E休眠唤醒显示异常问题

    我们的系统WINCE6.0,它支持睡眠和唤醒,目的是想在不使用的时候让设备进入睡眠状态,降低功耗,我们遇到的问题就是设备正常启动后正常显示,但睡眠然后唤醒后要么显示白屏要么是条纹状白屏,如下图: 图1 ...