题目大意:在一个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. scanf和scanfs的区别

    scanf()函数是标准C中提供的标准输入函数,用以用户输入数据 scanf_s()函数是Microsoft公司VS开发工具提供的一个功能相同的安全标准输入函数,从vc++2005开始,VS系统提供了 ...

  2. String创建对象的个数--西北狼

    public class StringDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto ...

  3. [Python陷阱]os.system调用shell脚本获取返回值

    当前有shell个脚本/tmp/test.sh,内容如下: #!/bin/bashexit 11 使用Python的os.system调用,获取返回值是: >>> ret=os.sy ...

  4. WPF 中动态创建、删除控件,注册控件名字,根据名字查找控件

    动态创建控件 1.容器控件.RegisterName("Name",要注册的控件)   //注册控件 2.容器控件.FindName("Name") as  控 ...

  5. JS监听关闭浏览器事件

    Onunload与Onbeforeunload Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或 ...

  6. BZOJ 4027 兔子与樱花

    原来想的是给所有点排序....但是要修改啊...然后发现对于儿子排序就可以了. #include<iostream> #include<cstdio> #include< ...

  7. MVC学习IIS的不同版本(一)

    一:IIS5.0运行在进程InetInfo.exe中,该进程寄宿着一个名为World Wide Publishing Service(W3VC)的window服务. W3VC的主要功能:包括HTTP请 ...

  8. App Store审核指南:WatchKit、HealthKit、ApplePay以及HomeKit部分

    将此前App Store审核指南中的WatchKit.HealthKit.ApplePay以及HomeKit部分进行了整理和摘取. 10. 用户界面 10.1 应用程序必须遵守苹果的<Apple ...

  9. nginx+lua项目学习

    1.启动nginx命令 /usr/local/ngx_openresty/nginx/sbin/nginx -p /usr/local/ngx_openresty/nginx/ -c /usr/loc ...

  10. 《JS高程》基本类型和引用类型的值学习笔记

    ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值. 创建方式类似:创建一个变量并为其赋值. (1)基本类型值和引用类型值比较 基本类型值 引用类型值 简单的数据段 可能由 ...