题意:给定一棵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. TwinCAT3的c++和标准c++(c++11)特性区别

    1.vector不能使用花括号初始化 2.不支持cmath,需要使用TcMath.h

  2. HDOJ-2047

    阿牛的EOF牛肉串 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  3. python 网络编程(网络基础之网络协议篇)

    前言 C/S  B/S 架构 client<----->server browser<--->server 学习socket编程就是要编写一个客户端软件和服务端软件,然后实现服 ...

  4. Eclipse SVN 图标解释

    [转]http://blog.sina.com.cn/s/blog_64941c8101018dno.html - 已忽略版本控制的文件.可以通过Window → Preferences → Team ...

  5. 快速打开和关闭SQL服务

    将下面文本复制到txt中,然后将文本文件另存为bat @echo off for /f "skip=3 tokens=4" %%i in ('sc query MSSQLSERVE ...

  6. IT兄弟连 JavaWeb教程 JSP动作指令

    JSP动作指令语法 JSP动作指令语法格式如下: <jsp:动作名 属性名1="属性值1" 属性名2="属性值2" ... %> JSP具体动作指令 ...

  7. c#file类读写

    private void button4_Click(object sender, EventArgs e) { FileStream fs = File.OpenRead(textBox1.Text ...

  8. Nginx实现负载均衡(nginx + tomcat应用分布式)

    一.       工具   nginx-1.8.0 apache-tomcat-6.0.33 二.    目标   实现高性能负载均衡的Tomcat集群: 三.    步骤   1.首先下载Nginx ...

  9. python自建模块显示说明与详情

    通常我们自建模块放在/usr/lib/python2.7/site-packages下面,这样可以python就可以进行调用. 但是,自建模块也要有详细的说明情况,例如查help,可以看出来模块的作用 ...

  10. Python-4-设置字符串的格式字符串

    字符串是不可变的,所有元素赋值和切片赋值都是非法的   1.替换字段名 可以按顺序和名称匹配 >>> "{foo} {} {bar} {}".format(1, ...