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

题意:给一棵有n个结点的树。每一个点有点权表示在这个点上的价值。每条边有边权表示走这条路所须要的时候,给一个时间m。问在时间m从点k出发再回到点k所能得到的最大的价值和。

题解:dp[i][j]:表示以i为根的子树花费j能达到的最大值。

#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#define ll long long
#define N 110 using namespace std; struct Edge {
int to,cost;
} ; vector<Edge>G[N];
int val[N],dp[N][N];
int n,m,k; void Addedge(int u,int v,int c) {
Edge it;
it.to=v;
it.cost=c;
G[u].push_back(it);
} void dfs(int fa,int u) {
dp[u][0]=val[u];
for(int i=0; i<G[u].size(); i++) {
int v=G[u][i].to;
int w=G[u][i].cost;
if(v==fa)continue;
dfs(u,v);
for(int j=m; j>=w; j--) {
for(int p=0; p<=j-w; p++) {
dp[u][j]=max(dp[u][j],dp[v][p]+dp[u][j-w-p]);
}
}
}
} int main() {
//freopen("test.in","r",stdin);
while(cin>>n) {
for(int i=0; i<N; i++)G[i].clear();
for(int i=1; i<=n; i++) {
scanf("%d",val+i);
}
for(int i=1; i<n; i++) {
int u,v,c;
scanf("%d %d %d",&u,&v,&c);
Addedge(u,v,c);
Addedge(v,u,c);
}
scanf("%d %d",&k,&m);
m/=2;
memset(dp,0,sizeof dp);
dfs(-1,k);
int ans=0;
for(int i=0; i<=m; i++)
ans=max(ans,dp[k][i]);
cout<<ans<<endl;
}
return 0;
}

ZOJ 3626 Treasure Hunt I(树形dp)的更多相关文章

  1. ZOJ 3626 Treasure Hunt I 树上DP

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

  2. ZOJ 3626 Treasure Hunt I (树形DP,常规)

    题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include ...

  3. zoj 3629 Treasure Hunt IV 打表找规律

    H - Treasure Hunt IV Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  4. zoj 3627 Treasure Hunt II (贪心)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:zoj-3627 题意 直线上有n个城市, 第i个城市和i+1个城市是相邻的.  每个城市都有vi的金币.   ...

  5. ZOJ 3627 Treasure Hunt II (贪心,模拟)

    题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集 ...

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

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

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

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

  8. ZOJ 3626(树形DP+背包+边cost)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...

  9. 【树形dp】Treasure Hunt I

    [ZOJ3626]Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous coun ...

随机推荐

  1. 静态方法中不能使用 $this

    忽略了一个问题,$this 代表当前对象!! 静态方法中应该使用 类名 . self 或者 static 关键字来代替! static public function get_info($id) { ...

  2. PHP mac xdebug配置

    PHP实现断点调试的条件 1. 需要PHP安装xdebug扩展 2. 修改PHP配置文件,开启xdebug扩展,并且对xdebug进行一些配置 3. 重启服务器如apach或nginx 4. 编译器配 ...

  3. ASP.NET MVC下使用SWFUpload完成剪切头像功能

    首先介绍SWFUpload组件 SWFUpload是一个客户端文件上传工具,最初由Vinterwebb.se开发,它通过整合Flash与JavaScript技术 为WEB开发者提供了一个具有丰富功能继 ...

  4. hdu5441

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  5. Android强制更新

    代码改变世界 Android版本强制更新 package com.lianpos.util; import android.content.Context; import android.conten ...

  6. JSON之解析

    JSON之解析通过TouchJSON\SBJSON\JSONKit\NSJSONSerialization JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 ...

  7. java程序实现视频格式的转换

    http://blog.sina.com.cn/s/blog_96b60b0c01013mi5.html 原文地址:java程序实现视频格式的转换作者:笑看风云 flv格式转换--第一步 不定期更新. ...

  8. java面试题之如何实现处理线程的返回值?

    有三种实现方式: 主线程等待法: 使用Thread类的join方法阻塞当前线程以等待子线程处理完毕: 通过Callable接口实现,通过FutureTask 或者线程池:

  9. phpMyAdmin操作之改管理员密码

    1.登录phpMyAdmin 默认地址:http://localhost/phpmyadmin 2.点击用户按钮 3.往下拉找到修改密码 点执行就修改了 注意: 如果再次登录时报错提示: #1045 ...

  10. linux监控平台搭建-磁盘

    linux监控平台搭建-磁盘 磁盘:随着大数据快速发展.人工智能.自动化.云平台.数据量指数的增长.磁盘的使用量也在增长.目前的机器基本上采用SSD或者SATA盘,一直有人比较那个好.会不会使用时间短 ...