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

思路:

  注意Ti可以为0,而且有可能t太小以至于不能到达n,则输出不可达。这样想会简单点,将"1->n"路径上的每条边断开,变成一片森林,而且路径上的这些点是根。我们需要计算每棵树在j天内最多能获得多少珠宝,这只需要一次DFS就可以完成了。然后除了森林中的根(即1->n路径上的点),其他都可以不用考虑了,按照"1->n"路径的方向,逐个点进行转移,更新dp值,到最后dp[n][t]就是答案了。最后这步是“至少带走1件物品”分组背包模型,只需要先将1件物品丢进背包,有好的就更新,没有的话也保证了至少带1件物品。

  

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <deque>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
int n, edge_cnt, head[N], w[N], dp[N][], cap[];
struct node
{
int from, to, cost, next, tag;
node(){};
node(int from,int to,int cost,int next):from(from),to(to),cost(cost),next(next){tag=;};
}edge[N*]; deque<pii> que;
void add_node(int from,int to,int cost)
{
edge[edge_cnt]=node(from,to,cost,head[from]);
head[from]=edge_cnt++;
} bool dfs(int t,int far,int m)
{
int flag=false;
if(m<) return false; //不可到达 for(int i=; i<=m; i++) dp[t][i]=w[t]; //到达本点至少获得w[t]
node e;
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if(e.to==far) continue;
if(dfs(e.to, t, m-e.cost)==true)
{
que.push_back(make_pair(e.to, e.cost));
flag=true;
edge[i].tag=;
}
else
{
for(int j=m; j>=; j--)
for(int k=*e.cost; k<=j; k++) //枚举给此孩子k-2*cost天的时间
dp[t][j]=max(dp[t][j], dp[t][j-k]+dp[e.to][k-*e.cost] );
}
}
if(t==n) return true; //终点站
else return flag;
} int main()
{
freopen("input.txt", "r", stdin);
int m, a, b, c;
while(~scanf("%d%d",&n,&m))
{
que.clear();
edge_cnt=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
memset(cap,,sizeof(cap)); for(int i=; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
add_node(a,b,c);
add_node(b,a,c);
}
for(int i=; i<=n; i++) scanf("%d",&w[i]);
if(dfs(,-,m)==true)
{
reverse(que.begin(),que.end()); //变成1条链,逐个节点进行“至少带1件物品的分组背包”
que.push_front(make_pair(,));
int t, c, st=;
while(!que.empty())
{
t=que.front().first; //当前点已经在t
c=que.front().second; //转移到t的花费
que.pop_front();
st+=c; for(int j=m; j>=st; j--)
{
cap[j]=cap[j-c]+dp[t][]; //从上个点转移到t至少需先消耗c
for(int k=; k+st<=j; k++)
cap[j]=max(cap[j], cap[j-k-c]+dp[t][k]);
}
}
printf("%d\n", cap[m]);
}
else puts("Human beings die in pursuit of wealth, and birds die in pursuit of food!");//不可达
}
return ;
}

AC代码

HDU 4276 The Ghost Blows Light (树形DP,变形)的更多相关文章

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

  2. HDU 4276 The Ghost Blows Light

    K - The Ghost Blows Light Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  3. HDU 4276 The Ghost Blows Light(树形)

    题意:给出一棵n个节点的树,起点1,终点n,相连的两个节点之间有距离,每个节点有个价值,给出一个时间T.问从1到达n在给定时间T内取得的最大价值? 思路:先从1走到n,如果总的时间不够走完,直接退出, ...

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

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

  5. HDOJ 4276 The Ghost Blows Light

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

  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 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. HDU 3586 Information Disturbing(二分+树形dp)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...

随机推荐

  1. Bayesian 网络分类算法

    1:贝叶斯网络的定义和性质 一个贝叶斯网络定义包括一个有向无环图(DAG)和一个条件概率表集合.DAG中每一个节点表示一个随机变量,可以是可直接观测变量或隐藏变量,而有向边表示随机变量间的条件依赖:条 ...

  2. STL中的vector实现邻接表

    /* STL中的vector实现邻接表 2014-4-2 08:28:45 */ #include <iostream> #include <vector> #include  ...

  3. [ACM] hdu 1285 确定比赛名次 (拓扑排序)

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. HTML学习笔记(一)HTML的一些概念区别

    HTML HTML 指超文本标记语言.在 HTML 4 中,有若干的标签和属性是被废弃的,替换成style对应的属性 应该避免使用下面这些标签和属性: 标签 描述 style <center&g ...

  5. Linux下新建一个站点

    Apache+nagix使用Lnmpa创建一个新的站点 我们在部署服务器的时候通常会遇到需要分域名和分应用部署,那么如何通过Apache+nagix创建一个新的站点服务呢 LNMPA这种架构有什么优势 ...

  6. CodeForces 689B【最短路】

    题意: 给你一副图,给出的点两两之间的距离是abs(pos1-pos2),然后给你n个数是表示该pos到x的距离是1. 思路: 直接建边,跑spfa就好了.虽然说似乎题意说边很多,其实只要建一下相邻的 ...

  7. laravel 数据库连接Mysql

    找到 config/database.php 'mysql' => [ 'driver' => 'mysql', //数据库的类型 'host' => env('DB_HOST', ...

  8. 浅谈C++中内存泄漏的检测

    首先我们需要知道程序有没有内存泄露,然后定位到底是哪行代码出现内存泄露了,这样才能将其修复.最简单的方法当然是借助于专业的检测工具,比较有名如BoundsCheck,功能非常强大,相信做C++开发的人 ...

  9. Codevs 1293 送给圣诞夜的极光

    1293 送给圣诞夜的极光  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 圣诞老人回到了北极圣 ...

  10. pytest入门学习(2)

    pytest的hello world pyt1.py def func(x): print (x+1); return x+1; def test_answer(): assert func(3) = ...