ZOJ 3626 Treasure Hunt I(树形dp)
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)的更多相关文章
- ZOJ 3626 Treasure Hunt I 树上DP
E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...
- ZOJ 3626 Treasure Hunt I (树形DP,常规)
题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include ...
- zoj 3629 Treasure Hunt IV 打表找规律
H - Treasure Hunt IV Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- zoj 3627 Treasure Hunt II (贪心)
本文出自 http://blog.csdn.net/shuangde800 题目链接:zoj-3627 题意 直线上有n个城市, 第i个城市和i+1个城市是相邻的. 每个城市都有vi的金币. ...
- ZOJ 3627 Treasure Hunt II (贪心,模拟)
题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集 ...
- 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...
- 【DP_树形DP专题】题单总结
转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...
- ZOJ 3626(树形DP+背包+边cost)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...
- 【树形dp】Treasure Hunt I
[ZOJ3626]Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous coun ...
随机推荐
- day01_14.遍历数组
<?php $a = array('a','b','c'); print_r($a); ?> 输出结果:Array ( [0] => a [1] => b [2] => ...
- Python的内存管理、命名规则、3个特性讲解
理解变量: 变:现实世界中的状态是会发生改变的 量:衡量/记录现实世界中的状态,让计算机能够像人一样去识别世间万物(例如:一个人的身高.体重等这些信息) 为什么要变量: 程序执行的本质就是一系列状态的 ...
- ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined)
靠这把上了蓝 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 megabyte ...
- 浏览器提示ERR_CONTENT_DECODING_FAILED,Gzip压缩数据无法解压
最近在页面上有个显示数据表格的功能,数据由后台传给前台JS表格插件.数据格式为JSON 由于数据量很大,就想到用GZIP压缩以后传给前台.压缩前,某个表格的数据量达到3M多,用GZIP压缩后就200K ...
- Scrum基础知识图谱
啰嗦一下 最近在学习scrum项目管理的知识,书上知识点分散,很难有整体的视角来看scrum有哪些核心知识,故制作了思维导图,望给和我一样容易迷失的人一样,起到一个指引作用,废话不多说,直接上图 图谱
- 【Luogu】P1251餐巾计划(上下界费用流)
题目链接 学了一下上下界费用流,似乎很nb.但是我说得不好,所以这里给出博客链接. 某dalao的博客 然后这道题的解法就是先用上下界费用流的建图方式连早上和晚上之间的那条边,保证当天一定会有r条或以 ...
- P1382 楼房 (扫描线,线段树)
题目描述 地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i],0),右上角为(r[i],h[i]).地平线高度为0.在轮廓 ...
- UVA 10003 Cutting Sticks(区间dp)
Description Cutting Sticks You have to cut a wood stick into pieces. The most affordable company ...
- MySQL 5.7.17绿色版安装
下载地址 :https://dev.mysql.com/downloads/mysql/ ,需要oracle帐号 下载 Windows (x86, 64-bit), ZIP Archive 是个 ...
- 【webstrom】webstrom打开多个项目,webstrom常用快捷键
1.webstrom打开多个项目 默认情况下一次只能打开一个项目,如果需要打开多个就按照下面的方法 File -> settings -> Directories -> Add Co ...