HDU 4276-The Ghost Blows Light(树状背包)
题意:
n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发)
分析:
该题关键是最后要到达m,没有这个条件,就是基础的树形背包,哎,一开始没思路,放了一段时间,看看题解才明白,该题突破口,就是,你先想怎么判断不能到到达m的情况,自然想到最短路,对树先求一次最短路,在最短路上的点只能,过一次,其他得点过两次,把最短路上的点花费置零,剩下的就是基础的树形背包。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int>p;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= <<;
const int mod = ;
int d[],dp[][],head[],n,t,len;
int par[];
struct edge{
int st,en,c,next;
}e[];
void add(int a,int b,int c)
{
e[len].st=a;
e[len].en=b;
e[len].c=c;
e[len].next=head[a];
head[a]=len++;
}
//求最短路
int dijkstra(int s){
priority_queue<p,vector<p>,greater<p> >q;
for(int i=;i<=n;++i)
d[i]=INF;
q.push(p(,s));
d[s]=;
memset(par,-,sizeof(par));
edge a,b;
while(!q.empty()){
p a=q.top();
q.pop();
int j=a.second;
int cost=a.first;
if(d[j]<cost)continue;
for(int i=head[j];i!=-;i=e[i].next){
edge g=e[i];
if(d[g.en]>cost+g.c){
par[g.en]=i;
d[g.en]=cost+g.c;
q.push(p(d[g.en],g.en));
}
}
}
//最短路上的点置零
for(int i=n;i!=s;i=e[par[i]].st){
e[par[i]].c=;
e[par[i]^].c=;
}
return d[n];
}
int used[];
void dfs(int root){
used[root]=;
for(int i=head[root];i!=-;i=e[i].next){
int son=e[i].en;
int cost=e[i].c*;//走两次
if(used[son])continue;
dfs(son);
for(int j=t;j>=cost;--j){
for(int k=;k+cost<=j;++k){
dp[root][j]=max(dp[root][j],dp[root][j-k-cost]+dp[son][k]);
}
}
}
}
int main()
{
int a,b,c;
while(~scanf("%d%d",&n,&t)){
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head));
memset(used,,sizeof(used));
len=;
for(int i=;i<n-;++i){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
for(int i=;i<=n;++i){
scanf("%d",&dp[i][]);
}
t-=dijkstra();
int maxv=;
if(t<)printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
else{
dfs();
for(int i=;i<=t;++i)
maxv=max(maxv,dp[][i]);
printf("%d\n",maxv);
}
}
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(树形)
题意:给出一棵n个节点的树,起点1,终点n,相连的两个节点之间有距离,每个节点有个价值,给出一个时间T.问从1到达n在给定时间T内取得的最大价值? 思路:先从1走到n,如果总的时间不够走完,直接退出, ...
- 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. 题目描述中有两句话, ...
- HDU 1561-The more, The Better(树状背包)
题意: n个城堡,每个有一定的财富,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡,求应攻克哪m个城堡,获得最大财富. 分析:dp[i][j],以i为根的子树,攻克j个城堡,获得的 ...
- 【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 5869 区间不同GCD个数(树状数组)
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
随机推荐
- 【BZOJ2199】 [Usaco2011 Jan]奶牛议会
Description 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要的”为原则,建立了下面的投票系统: M只到场的奶牛 (1 & ...
- JQuery 知识点
1:animate 动画效果 $(function () { $(".sidebar-nav a").mouseover(function () { $(this).animate ...
- 在openwrt上初体验PostgreSQL数据库
要求 请确保在你的路由器shell 中有以下这些命令 adduser, deluser, addgroup, delgroup, su . 还需要熟悉su,chown ,opkg,mkdir,服务操作 ...
- NOI考前乱写
还有13天NOI,把各种乱七八糟的算法都重新过一遍还是比较有必要的... //HDU 5046 Airport //DancingLink #include<iostream> #incl ...
- Recommender Systems引言
我想站在大神肩膀上...貌似是计算所的一个小伙伴... 总结的很好,看得出来有一定的功底.... 不过对于自己看过了的东西,就不愿意再翻看第二遍了.恰好这本书和项亮那本很像,就直接看这本书了.顺便记记 ...
- javascript中跨源资源共享
来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(十) 通过XHR实现Ajax通信的一个主要限制,来源于跨域安全策略.默认情况下,XHR对 ...
- 团体程序设计天梯赛-练习集L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- spoj 379
题是水题 但丫的题目意思太难懂 ....... 英语水平 ...... #include <cstdio> #include <cstring> #include &l ...
- fiddler插件开发step by step 1
Fiddler 是优秀的抓包工具,有着众多的优秀插件.Fiddler 软件是由C#语言开发的,运行在.net Framework 框架之上,所以我们也可以使用vs来开发自己的Fiddler插件,下面就 ...
- 如何提高多线程程序的cpu利用率
正如大家所知道的那样,多核多cpu越来越普遍了,而且编写多线程程序也是件很简单的事情.在Windows下面,调用CreateThread函数一次就能够以你想要的函数地址新建一个子线程运行.然后,事情确 ...