[ZOJ3626]Treasure Hunt I


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn't at his hometown. One day, a brave person named CC finds a treasure map, and he wants to get as much as possible.

Akiba consists of n towns and n-1 roads. There is a way from each town to any other. Each town contains some treasure values Vi. CC starts from town k(his hometown), at day 0. After m days, the bloodsucker will appear and CC would be killed if he hasn't been back yet, it means CC has m days for hunting the treasure at most. It takes CC Ti days to move from one town to another neighbour town.(Two towns called neighbour if they are the endpoint of one road.) You can assume CC will get the treasure immediately as he arrives at that town. CC wants to obtain as much value as possible, keeping him alive at the same time.

Input

There are multiple cases, about 50 cases.
The first line of each case contains an integer n, indicating there are n towns.
The following line describe the treasure's value in each town. "V1 V2 ... Vn". Vi is the value of the treasure in ith town. Each value is separated by one blank.
The next n-1 lines describe the n-1 roads in Akiba. "i j Ti" Means the ith town and the jth town are endpoints of that road. It takes Ti days to get through this road.
The last line has two integer k and m as described above.

1<=n<=100, 0<=Vi<=1000 , 1<=Ti<=10
1<=k<=n, 1<=m<=200
All the inputs are integers.

Output

Just output the max value CC can get, and you should keep CC alive after m days.

Sample Input

2
1 3
1 2 1
1 2
2
1 3
2 1 1
2 1
2
3 3
1 2 1
2 5

Sample Output

4
3
6

Hint

Sample 1: CC can go to town 2 and return at day 2.
Sample 2: CC can't come back within 1 day. So he can only take the treasure in his hometown.
Sample 3: CC only need 2 days to collect all the treasure.


Author: LI, Chao
Contest: ZOJ Monthly, July 2012

试题分析:设dp[i][j]表示在i号节点走j步能获得的最大宝藏数。

     就像泛化背包那样,由于还要回去,所以得出转移方程:

     dp[i][j]=max(dp[i][j-t-2*Cost[x]]+dp[i->son][t],dp[x][j])

     为什么不是减去2*t呢,因为之前的已经在dp[i->son][t]中算过了。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,M,K;
int val[101],dp[101][201];
int Node[301],Root[301],Cost[301],Next[301];
int cnt; void addedge(int u,int v,int w){
cnt++;
Node[cnt]=v;
Cost[cnt]=w;
Next[cnt]=Root[u];
Root[u]=cnt;
return ;
}
void init(){
for(int i=1;i<=N;i++)
for(int j=0;j<=M;j++) dp[i][j]=val[i];
return ;
}
void dfs(int x,int fa){
for(int i=Root[x];i;i=Next[i]){
int son=Node[i];
if(son==fa) continue;
dfs(son,x);
}
for(int i=Root[x];i;i=Next[i]){
int son=Node[i];
if(son==fa) continue;
for(int j=M;j>=1;j--){
for(int t=0;t<=M;t++){
if(j<t+2*Cost[i]) break;
dp[x][j]=max(dp[x][j-t-2*Cost[i]]+dp[son][t],dp[x][j]);
}
}
}
return ;
} int main(){
while(scanf("%d",&N)!=EOF){
cnt=0;
memset(Next,0,sizeof(Next));
memset(Node,0,sizeof(Node));
memset(Root,0,sizeof(Root));
memset(Cost,0,sizeof(Cost));
for(int i=1;i<=N;i++) val[i]=read();
for(int i=1;i<N;i++){
int u=read(),v=read(),w=read();
addedge(u,v,w);
addedge(v,u,w);
}
K=read(),M=read();
init();
dfs(K,-1);
printf("%d\n",dp[K][M]);
}
}

【树形dp】Treasure Hunt I的更多相关文章

  1. ZOJ 3626 Treasure Hunt I(树形dp)

    Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since ...

  2. zoj-3626 Treasure Hunt I (树形dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: zoj-3626 题意 给一棵n个节点的树, 节点编号1~n, 每个节点有权值val[i],经过这个节点就可 ...

  3. ZOJ 3626 Treasure Hunt I 树上DP

    E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...

  4. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

  5. 【DP_树形DP专题】题单总结

    转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...

  6. HDU5834 Magic boy Bi Luo with his excited tree(树形DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...

  7. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

  8. 宝藏(树形DP)

      这道题目是十分考验思维的,n^2应该还是比较好想的,主要是如何转移根的问题.转移根,在我看来应该是树形dp最难的一部分了, 一般学会如何转移根,也就差不多考验通吃树形dp了. 下面转一转大佬链接: ...

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

随机推荐

  1. SQLserver 字符串分割函数

    CREATE function Get_StrArrayStrOfIndex ( @str varchar(), --要分割的字符串 @split varchar(), --分隔符号 @index i ...

  2. vue_router添加点击事件

    1.在vue学习中遇到给router-link 标签添加事件@click .@mouseover等无效的情况 原来的代码: <router-link to='/SelectPage' @clic ...

  3. 【Python学习笔记】Pandas库之DataFrame

    1 简介 DataFrame是Python中Pandas库中的一种数据结构,它类似excel,是一种二维表. 或许说它可能有点像matlab的矩阵,但是matlab的矩阵只能放数值型值(当然matla ...

  4. vue-实现倒计时功能

    JavaScript 创建一个 countdown 方法,用于计算并在控制台打印距目标时间的日.时.分.秒数,每隔一秒递归执行一次. msec 是当前时间距目标时间的毫秒数,由时间戳相减得到,我们将以 ...

  5. Linux 入门记录:十五、Linux 网络基本配置

    一.以太网(Ethernet) 以太网(Ethernet)是一种计算机局域网技术.IEEE 组织的 IEEE 802.3 标准制定了以太网的技术标准,它规定了包括物理层的连线.电子信号和介质访问层协议 ...

  6. 【bzoj4765】普通计算姬

    一道奇奇怪怪的数据结构题? 把树线性化,然后分块维护吧. 为了加速,求和用树状数组维护每个块的值. #include<bits/stdc++.h> #define N 100010 #de ...

  7. 辨别苹果数据线真伪 苹果计算器 Dashboard 知识

    辨别苹果数据线真伪 苹果计算器 Dashboard 知识  苹果数据线真伪的最简单的辨别: 线质柔软 用数据线连接适配器(苹果自带的适配器)充电 连接手机 如果该手机数据线是假的, 在手机上会提示”该 ...

  8. mybatis-plus的学习

    1.mybatisplus 提供了比较齐全的crud即增删改查,不需要在mapper.xml里写sql可以直接调用 原文链接:http://blog.csdn.net/u014519194/artic ...

  9. Fastcgi协议定义解释与说明

    1 响应格式如(十六进制方式显示) 序列 0 1 2 3 4 5 6 7 ... 数值 01 06 00 01 01 1D 03 00... 序列0(值01)为version,固定取1即可序列1(值0 ...

  10. SEO:查找网站的百度收录情况和如何让百度快速收录

    查询收录的工具地址: http://tool.chinaz.com/baidu/entry/ 如何让百度快速收录: 一.大家都熟知的百度网站提交,只需要提交网站的首页即可.以前做完这一步就被百度收录的 ...