题目大意:在一个n个节点的树形迷宫中,1为起点,n为出口。每个节点上有一定价值的珠宝,在节点之间移动的时间已知,问在能走出迷宫的前提下并且不超过m的时间内能收集的最多珠宝是多少?

题目分析:在树上,从1到n的路径唯一。从1到n的唯一路径叫做主线路,要想走到出口,一定会经过主线路,也就是必须经过主线路上节点。在脱离主线路之前必须要预留出返回的时间。

代码如下:

# include<iostream>
# include<cstdio>
# include<vector>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; const int N=105;
const int INF=1000000000; int n,m;
int d[N];
int dp[N][N*5];
int g[N][N];
int w[N],pre[N];
vector<int>e[N]; void init()
{
int a,b,c;
for(int i=1;i<=n;++i)
e[i].clear();
for(int i=1;i<n;++i){
scanf("%d%d%d",&a,&b,&c);
g[a][b]=g[b][a]=c;
e[a].push_back(b);
e[b].push_back(a);
}
for(int i=1;i<=n;++i)
scanf("%d",w+i);
} void spfa()
{
fill(pre,pre+n+1,-1);
fill(d+1,d+n+1,INF);
queue<int>q;
q.push(1);
d[1]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=0;i<e[u].size();++i){
int v=e[u][i];
if(d[v]>d[u]+g[u][v]){
d[v]=d[u]+g[u][v];
pre[v]=u;
q.push(v);
}
}
}
} void dfs(int u,int fa)
{
fill(dp[u],dp[u]+m+1,w[u]);
for(int i=0;i<e[u].size();++i){
int v=e[u][i];
if(v==fa) continue;
dfs(v,u); int t=2*g[u][v]; for(int j=m;j>=t;--j)
for(int k=0;k+t<=j;++k)
dp[u][j]=max(dp[u][j],dp[v][k]+dp[u][j-k-t]);
}
} void solve()
{
spfa();
if(d[n]>m){
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
}else{
int u=n;
while(pre[u]!=-1){
g[pre[u]][u]=g[u][pre[u]]=0;
u=pre[u];
}
m-=d[n];
dfs(1,-1);
printf("%d\n",dp[1][m]);
}
} int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
solve();
}
return 0;
}

  

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

  1. 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 ...

  2. HDU 4276 The Ghost Blows Light

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

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

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

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

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

  5. HDU4276 - The Ghost Blows Light(树形DP)

    题目大意 给定一棵n个结点的树,每个结点上有一定数量的treasure,经过每条边需要花一定的时间,要求你从结点1出发,在不超过时间T的情况下,最多能够获得的treasure是多少,并且要求结束于结点 ...

  6. HDOJ 4276 The Ghost Blows Light

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

  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. URAL_1018 Binary Apple Tree 树形DP+背包

    这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...

  10. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. Android之sdcard操作

    private static final String FILENAME = "abc.txt"; private static final String DIR = " ...

  2. xcode开发的6个小技巧

    Xcode是iPhone和iPad开发者用来编码或者开发iOS app的IDE.Xcode有很多小巧但很有用的功能,很多时候我们可能没有注意到它们,也或者我们没有在合适的水平使用这些功能简化我们的iO ...

  3. Android模拟器配置选项说明

    Memory Options是模拟器的运行内存大小,类比电脑内存大小,就是在设置->应用程序中,正在运行标签页下面显示的那个大小Internal storage是模拟器内置存储空间大小,用于存放 ...

  4. Installing Cygwin and Starting the SSH Daemon

    This chapter explains how to install Cygwin and start the SSH daemon on Microsoft Windows hosts. Thi ...

  5. 大嫂的HTML

      <html>   <head>   <style type="text/css">   *{ margin: 0; padding: 0; ...

  6. linux学习笔记2:linux 下java开发的软件安装

    一.java ee开发环境的搭建 1.jdk的安装步骤 (1)首先必须要有安装文件,具体的可以去相关网站上下载,并制作iso文件 (2)将制作的iso文件挂载到linux系统上,并在虚拟机上将iso文 ...

  7. C/C++整数除法以及保留小数位的问题

    题目描述 Given two postive integers A and B,  please calculate the maximum integer C that C*B≤A, and the ...

  8. 保存现场数据和状态:onSaveInstanceState\onRestoreInstanceState\onCreate()

    当某个activity变得“容易”被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK键的时候. 注意上面 ...

  9. Osmocom-BB MOTO C118硬刷

    写在最前面,先知我YY下硬刷最好可能实现的功能: 1.把软件刷入flash,修改loader后,可以实现上电就自动运行程序: 2.硬刷后,程序自动起来,可以修改loader就行加密 3.硬刷后,有可能 ...

  10. HDU 1109

    http://acm.hdu.edu.cn/showproblem.php?pid=1109 一个范围内给一堆点,求到这些点的最短距离最大 模拟退火,温度是步长 #include <iostre ...