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 < ...
随机推荐
- 微软职位内部推荐-Sr Development Lead-OSG-IPX
微软近期Open的职位: Job Summary:Be part of Microsoft's strategy to deliver a great input experience across ...
- linux内核分析之内存管理
1.struct page /* Each physical page in the system has a struct page associated with * it to keep tra ...
- VS 2005部署应用程序提示“应用程序无法正常启动( 0x0150002)” 解决方案
遇到这个问题,一定是缺少了CRT.MFC.ATL的DLL,不同版本的VS是不一样的.系统自带这些库的Release版,如果没有自带,打补丁就有了:系统不自带这些库的Debug版,所以Debug版的程序 ...
- integer和double的比较.
Integer douVal=20; double parseDouble = Double.parseDouble(douVal.toString()); System.out.println(pa ...
- EasyUI datagrid 分页Json字符串格式
//EasyUI datagrid 分页Json字符串格式 //{"total":xx,"rows":[{...},{...}]} total:总数 rows: ...
- Eclipse 调整代码颜色的地方
Editors - Text Editors General-Apperance-Colors and Fonts 各工作区里面的Editor和Syntax Coloring
- AlphaGo 已经战胜了李世石,而你还不知道什么是机器学习?
谷歌人工智能 AlphaGo 与韩国棋手李世石 3 月 15 日进行了最后一场较量,最终比赛结果为 AlphaGo 4:1 胜李世石,人机围棋大战巅峰对决至此落幕.我不知道大家有没有被震撼到,反正我的 ...
- POJ1002487-3279(map)
http://poj.org/problem?id=1002 题意:是说很多公司用了容易记住的电话号码,例如有英文字母的或者是用了很多连字符或没有连字符的.每个电话号码都有标准模式,而为了统计有没有重 ...
- CVE漏洞爬虫java代码依赖-TestNG
TestNG是Java中的一个测试框架,而该CVE漏洞爬虫示例中所涉及到的java代码中, \Crawler\src\com\***\ThreaderRun.java文件在导入import org.t ...
- dup和dup2函数以及管道的实现
疑问:管道应该不是这样实现的,因为这要求修改程序的代码 dup和dup2也是两个非常有用的调用,它们的作用都是用来复制一个文件的描述符.它们经常用来重定向进程的stdin.stdout和stderr. ...