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,没有这个条件,就是基 ...
随机推荐
- KEIL中的变量相关
例:sfr P0=0x80表示P0口地址为80H:(sfr是字节操作) sfr16 T2=0xCC表示T2口地址的低地址为TL2=0XCC,高地址为TH2=0XCD(sfr16是字操作) 头文件reg ...
- Cmake设置环境变量
references: http://stackoverflow.com/questions/21047399/cmake-set-environment-variables-from-a-scrip ...
- Android 得到当前已连接的wifi的信号强度
1.得到当前已连接的wifi信息 WifiManager wifi_service = (WifiManager)getSystemService(WIFI_SERVICE); WifiInfo wi ...
- nodejs教程
http://www.yiibai.com/nodejs/ http://www.runoob.com/nodejs/nodejs-tutorial.html http://www.runoob.co ...
- puppet cert maintain
- JS监听组合按键
有些时候,我们需要在网页上,增加一些快捷按键,方便用户使用一些常用的操作,比如:保存,撤销,复制.粘贴等等. 下面简单梳理一下思路: 我们所熟悉的按键有这么集中类型: 单独的按键操作,如:delete ...
- 传阿里整合资源,进军O2O市场
阿里巴巴对于本地生活市场,以及O2O领域始终虎视眈眈.从最早的融合口碑网,到最近阶段推出淘宝点点.收购高德地图等一系列app产品,其整合线上线下消费市场的野心已十分明显. 今年年初,阿里巴巴集团重新进 ...
- Android 体系结构
Anroid是在Linux基础开发出的一个移动设备开发平台.它自上而下包含四个部分: Application(应用程序) Applicaton Framework(应用程序框架) Libraries& ...
- 解决vsftpd 530 Permission denied报错
虚拟机装好RedHat后,准备使用filezilla连接,输入IP地址,root用户,密码,快速连接,报错: 故障排除: 1.首先检查系统是否开启了vsftp服务,如果没有开启,先开启该服务. 方法1 ...
- c#基础: NetWorkStream类的主要属性
一.网络流 1. 最常用的方法 Read() Write() Flush() NetworkStream netStream = new NetworkStream(mesock); ...