HDU 4276 The Ghost Blows Light (树形DP,变形)
题意:给定一棵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,变形)的更多相关文章
- 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 ...
- 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,如果总的时间不够走完,直接退出, ...
- HDU4276 - The Ghost Blows Light(树形DP)
题目大意 给定一棵n个结点的树,每个结点上有一定数量的treasure,经过每条边需要花一定的时间,要求你从结点1出发,在不超过时间T的情况下,最多能够获得的treasure是多少,并且要求结束于结点 ...
- HDOJ 4276 The Ghost Blows Light
题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...
- 【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 1520.Anniversary party 基础的树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 3586 Information Disturbing(二分+树形dp)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...
随机推荐
- 华为codecraft2018总结
华为codecraft2018总结 想来也是参加了第二次了,自己还是那么的菜.总结下今年的比赛,得奖是不存在的了,但是收获还是有的. 代码相关的都在这里了:https://github.com/hui ...
- 1.3-1.4 hive环境部署
一. 官网:http://hive.apache.org/ 下载:http://archive.apache.org/dist/hive/ GitHub:https://github.com/apac ...
- UVa 11520 Fill the Square (水题,暴力)
题意:给n*n的格子里填上A-Z的字符,保证相邻字符不同,并且字典序最小. 析:直接从第一个格子开始暴力即可,每次判断上下左是不是相同即可. 代码如下: #pragma comment(linker, ...
- E20181029-hm
cardinality 基数 entity n. 实体; 实际存在物; 本质; distribute vt. 分配,散布; 散发,分发; 把…分类; [电] 配电; replica n. 复制品; ...
- 关于 T[] 的反射问题
1. T[] 类型不适应以下代码 Dictionary<string, Test> d = new Dictionary<string, Test>(); // Get a T ...
- Integrative Analysis of MicroRNAome, Transcriptome, and Proteome during the Limb Regeneration of Cynops orientalis (文献分享一组-翁海玉)
文献名:Integrative Analysis of MicroRNAome, Transcriptome, and Proteome during the Limb Regeneration of ...
- VRTK3.3.0-004传送
直线传送: 一.无高度变换传送(VRTK_BasicTeleport) 1丶继续在VRScripts下创建空物体PlayArea,用来挂在传送相关脚本:创建Plane作为传送地面 2丶在PlayAre ...
- PostgreSQL - 怎么转换数据类型
前言 对于select 233;这个sql,得到的结果是int4类型,如果我们希望将结果转换成其他的数据类型,有以下方法(下边的{数据类型}表示占位符,要替换成数据库中的某一种数据类型): 方法一:使 ...
- jmeter diff测试(调用JAR包处理)
1.准备接口数据(对比字段,即json数据中需要提取的key对应的值进行对比) 2.配置获取EXCEL数据 3.新建线程,并建两个http请求,分别用于请求新旧接口 4.提取需要对比的内容 5.赋值变 ...
- Day2课后作业:sed替换程序
#!/usr/bin/env python #_*_conding:utf-8_*_ import sys,os old_file = sys.argv[1] new_file = sys.argv[ ...