HDU 4276 The Ghost Blows Light(树形)
题意:给出一棵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(树形)的更多相关文章
- HDU 4276 The Ghost Blows Light
K - The Ghost Blows Light Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- HDU 4276 The Ghost Blows Light (树形DP,变形)
题意:给定一棵n个节点的树,起点是1,终点是n,每经过一条边需要消耗Ti天,每个点上有一定量的珠宝,要求必须在t天内到达终点才可以将珠宝带出去,问至多能带多少珠宝? 思路: 注意Ti可以为0,而且有可 ...
- 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 ...
- HDOJ 4276 The Ghost Blows Light
题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...
- HDU4276 - The Ghost Blows Light(树形DP)
题目大意 给定一棵n个结点的树,每个结点上有一定数量的treasure,经过每条边需要花一定的时间,要求你从结点1出发,在不超过时间T的情况下,最多能够获得的treasure是多少,并且要求结束于结点 ...
- 【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 ...
- BNUOJ 26283 The Ghost Blows Light
The Ghost Blows Light Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...
- HDU-4276 The Ghost Blows Light (树形DP+背包)
题目大意:在一个n个节点的树形迷宫中,1为起点,n为出口.每个节点上有一定价值的珠宝,在节点之间移动的时间已知,问在能走出迷宫的前提下并且不超过m的时间内能收集的最多珠宝是多少? 题目分析:在树上,从 ...
- HDU 4276-The Ghost Blows Light(树状背包)
题意: n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发) 分析: 该题关键是最后要到达m,没有这个条件,就是基 ...
随机推荐
- 关于overload和override
override 覆盖,表示在子类中一个函数覆盖基类中的同名函数,或者局部的某个函数覆盖了全局的某个同名函数.被覆盖的函数通常不能直接被调用,必须借助一些显式的强制手段. overload 重载,表示 ...
- tab切换jquery代码
http://immmmm.com/jquery-tab-switch-code-improved.html html <div id="sidebar-tab"> ...
- WebService-使用JDK开发WebService
一.使用JDK开发WebService 2.1.开发WebService服务器端 1.定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所 ...
- AngularJs学习笔记1——入门知识
1.什么是AngularJs AngularJs 诞生于2009年,由Misko Hevery 等人创建,后被Google收购,是一个优秀的Js框架,用于SPA(single pag ...
- 给那些因为Firebug而舍不得FireFox的朋友
Google Chrome浏览器调试 作为Web开发人员,我为什么喜欢Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/20 ...
- Android : 如何在WebView显示的页面中查找内容
Android : 如何在WebView显示的页面中查找内容 Author : Aoyousatuo Zhao http://blog.sina.com.cn/aoyousatuo WebView是A ...
- Makefile学习(三)执行make
9 执行make 一般方法:make. 某些情况:1.可能需要使用make更新一部分过时文件而不是全部 2.需要使用另外的编译器或者重新定义编译选项 3.只需要查看哪些文件被修改,不需要重新编译 所以 ...
- openMP的一点使用经验
最近在看多核编程.简单来说,由于现在电脑CPU一般都有两个核,4核与8核的CPU也逐渐走入了寻常百姓家,传统的单线程编程方式难以发挥多核CPU的强大功能,于是多核编程应运而生.按照我的理解,多核编程可 ...
- C#获取时间戳的方法
获取时间戳的方法 /// <summary> /// 获取时间戳 /// </summary> /// <param name= ...
- Stm32高级定时器(二)
Stm32高级定时器(二) 1 主从模式:主?从? 谈论主从,可知至少有两个以上的触发或者驱动信号,stm32内部有多个定时器,可以相互之间驱动或者控制. 主模式:定时器使能只受驱动时钟控制或者输出控 ...