题意:给出一棵n个节点的树,起点1,终点n,相连的两个节点之间有距离,每个节点有个价值,给出一个时间T。问从1到达n在给定时间T内取得的最大价值?

思路:先从1走到n,如果总的时间不够走完,直接退出,否则把时间扣掉,这些边权设置为0,然后做一遍树形DP

 #include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
int n,m;
int tot,go[],first[],next[],val[];
int c[],vis[],pre[],edge[],op[];
int f[][],v[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y,int z){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
val[tot]=z;
}
void add(int x,int y,int z){
insert(x,y,z);op[tot]=tot+;
insert(y,x,z);op[tot]=tot-;
}
void bfs(){
for (int i=;i<=n;i++) vis[i]=pre[i]=edge[i]=;
int h=,t=;
c[]=;vis[]=;
while (h<=t){
h++;
for (int i=first[c[h]];i;i=next[i]){
int pur=go[i];
if (vis[pur]) continue;
pre[pur]=c[h];
edge[pur]=i;
c[++t]=pur;
vis[pur]=;
}
}
}
void prework(){
for (int i=n;i!=;i=pre[i]){
m-=val[edge[i]];
val[edge[i]]=val[op[edge[i]]]=;
}
}
void dfs(int x,int fa){
for (int i=;i<=m;i++) f[x][i]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
dfs(pur,x);
int dis=val[i];
dis*=;
for (int k=m;k>=dis;k--)
for (int j=;j+dis<=k;j++)
f[x][k]=std::max(f[x][k],f[pur][j]+f[x][k-j-dis]);
}
for (int i=;i<=m;i++)
f[x][i]+=v[x];
}
int main(){
while (scanf("%d%d",&n,&m)!=EOF){
if (n==&&m==) return ;
tot=;
for (int i=;i<=n;i++) first[i]=;
for (int i=;i<n;i++){
int x,y,z;
x=read();y=read();z=read();
add(x,y,z);
}
for (int i=;i<=n;i++) v[i]=read();
bfs();
prework();
if (m<){
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
continue;
}
dfs(,);
printf("%d\n",f[][m]);
}
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 (树形DP,变形)

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

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

  4. HDOJ 4276 The Ghost Blows Light

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

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

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

  6. 【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 ...

  7. BNUOJ 26283 The Ghost Blows Light

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

  8. HDU-4276 The Ghost Blows Light (树形DP+背包)

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

  9. HDU 4276-The Ghost Blows Light(树状背包)

    题意: n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发) 分析: 该题关键是最后要到达m,没有这个条件,就是基 ...

随机推荐

  1. Manacher马拉车

    俗话说:摩托再好,不如骡拉啊(好像不是骡) Manacher就是O(N)计算最长回文子串的算法. 其中我们需要在0位置加入字符“$",然后原字符串中每两个字符加入一个"#" ...

  2. Delphi中多线程下使用使用 UniDAC+MSSQL 需要注意的问题(连接前调用CoInitialize)

    一般解决方法是在线程开始启用 CoInitialize(nil),线程结束调用 CoUninitialize .如果你使用多种数据库连接,比如三层中经常切换到MSSQL和Oracle,我们只需在判断 ...

  3. SWF加解密资源索引之加密混淆篇【转】

    ============================ SWF加解密资源索引之加密混淆篇 ============================ [心得] swf加密混淆器(带源码) http:/ ...

  4. poj 2773 利用欧拉函数求互质数

    题意:找到与n互质的第 k个数 开始一看n是1e6 敲了个暴力结果tle了,后来发现k达到了 1e8 所以需要用到欧拉函数. 我们设小于n的 ,与n互质的数为  (a1,a2,a3.......a(p ...

  5. Vericant维立克 | 氪加

    Vericant维立克 | 氪加 Vericant维立克

  6. n%i之和

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1168 题意:给定一个n,注意这里n小于10^12,求 分析:早些时 ...

  7. HTML--控制小人自由移动

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. python高级编程:缓存

    # -*- coding: utf-8 -*-__author__ = 'Administrator'#缓存"""对于运行代价很高的函数和方法结果,可以进行缓存,只要:1 ...

  9. webService返回自定义类型的数据处理

    1.自定义一个Student 数据类型: package com.chnic.webservice; import java.io.Serializable; public class Student ...

  10. Linux命令vi/vim编辑

    一.vi的基本概念基本上vi可以分为三种状态,分别是命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode),各模式的功能区分如下:a) 命令 ...