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 ...
随机推荐
- CI 分页类的使用
分页本身很简单,无非就是一个 [limit $offset, $length] 的过程. $length 是每页显示的数据量,这个是固定的.要确定的就只有 $offset了. 在CI中的分页类同样要依 ...
- SDOJ 2605 闲荡
描述 L 饭后无聊,便在 BugTown 里闲荡. BugTown 共有 N 栋房屋和 M 条有向道路.每栋房屋都有一个非负整数 vi 作为标识. BugTown 有一个特性十分神奇:从任意一个房屋离 ...
- [python学习篇] [os模块] [2]删除文件夹
def deleteDirectory(self,current_path): if not os.path.exists(current_path): self.logger.info(curren ...
- 将json的文本文件转换为csv文件
import pandas as pd import fire import glob import json def text_to_csv(file_name): json_data = json ...
- JS数组的下标如果是字符串的排序
var test = []; test['0'] = 0; test['1'] = 1; test['2'] = 2; 这样一个数组的排序方式是字符为‘1’的数组元素排第一,为‘0’的排在最后
- list 类
题外:len = sizeof(a)/sizeof(a[0]); 求出数组长度 1.list是一种以双向链表方式实现的一种顺序容器.list容器中,存放元素的存储单元可以是连续的也可以是不连续的. 2 ...
- deque 类
题外: 'A' +1='B' 1.deque被称为双端队列,它也是一种顺序容器.可通过迭代器存取元素 ,也可以通过下标顺序 存取元素 for(i=0;i<d1.size();i++) { cou ...
- POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28399 Accepted: 9684 De ...
- BZOJ3555 [Ctsc2014]企鹅QQ 【hash】
题目 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大的互联网功能体验, ...
- 刷题总结——生日礼物(bzoj1293单调队列)
题目: Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠, ...