题意:给定一棵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. git add . 的时候遇到warning: LF will be replaced by CRLF inXXX 解决办法

    $ git add . warning: LF will be replaced by CRLF in shop/Runtime/Cache/86bbc820c9ec1 d314a9c71cf5651 ...

  2. 【Data structure & Algorithm】把二元查找树转变成排序的双向链表

    把二元查找树转变成排序的双向链表 题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表,要求不能创建任何新节点,只调整指针指向. 比如将二元查找树 10 /       \ 6       ...

  3. 转载 关于启用HTTPS的一些经验分享

    本文转载自  https://imququ.com/post/sth-about-switch-to-https.html 随着国内网络环境的持续恶化,各种篡改和劫持层出不穷,越来越多的网站选择了全站 ...

  4. 洛谷 - UVA11424 - GCD - Extreme (I) - 莫比乌斯反演 - 整除分块

    https://www.luogu.org/problemnew/show/UVA11424 原本以为是一道四倍经验题来的. 因为输入的n很多导致像之前那样 \(O(n)\) 计算变得非常荒谬. 那么 ...

  5. 3DMAX 4角色蒙皮

    1 角色建模 略,以后补充 2 骨骼绑定 一般不用骨骼直接拉,Biped足够,以后适当补充骨骼直接拉的操作 1 将Biped骨骼和模型对齐 1 创建biped之后,第一步一要先选择,然后再对位骨骼到模 ...

  6. bzoj 3559: [Ctsc2014]图的分割【最小生成树+并查集】

    读题两小时系列-- 在读懂题意之后,发现M(c)就是c这块最大权割边也就是的最小生成树的最大权边的权值,所以整个问题都可以在MST的过程中解决(M和c都是跟着并查集变的) 不过不是真的最小生成树,是合 ...

  7. IT兄弟连 JavaWeb教程 JSP经典面试题

    1.JSP标准提供了三种独立的向JSP添加Java代码的技术,请列举. <% %>JSP程序代码块,内部可以直接嵌入Java代码. <%! %>JSP声明区,内部可以声明变量和 ...

  8. Django框架简介,wsgiref 与 jinja2 模块

    目录 框架简介 wsgiref模块 jinja2 模块 框架简介 Django是一个web开发框架,用来开发web应用,本质就是, web框架+socket服务端 MVC框架和MTV框架 MVC,全名 ...

  9. 新建Podfile命令

    接下来,你需要建立一个主工程.建立成功以后,再次启动终端, 利用cd命令进入到工程文件夹内,此时需要创建一个特殊的文本文件,命令如下: 命令: touch Podfile 创建 命令: open -e ...

  10. UITableView以及cell属性

    在ios的UI中UITableView是个常用且强大的控件 基本使用: 1>设置代理,一般把控制器设为代理:self.tableView.delegate = self; 2>遵守代理的协 ...