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 ...
随机推荐
- 用qemu+gdb tcp server+CDT调试linux内核启动-起步
用qemu+gdb tcp server+CDT调试linux内核启动-起步 说明: 环境信息与 用virtualbox+模拟串口+CDT调试linux内核 TCP IP协议栈-起步 提到的一样,并且 ...
- [错误处理]python大小写敏感,关键字不要写错
今天调试程序,发现了一个极为隐蔽的bug. True False关键字大小写写错了,然后半天没找出问题所在.
- curl 设置头部
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
- CSS:IE,Chrome,Firefox兼容性和CSS Hack(转载)
原作者:微米博客 以前写过一篇关于CSS hack的文章,但近期回头看了看发现理解的不够深刻,总结的也不凝练,于是今天重新测试从新写一篇.常用的CSS hack如下(笔者只对IE&FF& ...
- BZOJ2527 [Poi2011]Meteors 【整体二分 + 树状数组】
题目 Byteotian Interstellar Union有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下 ...
- 在 IBM RAD 平台上基于 JAX-WS 开发 Web Services服务器端,客户端
原文地址:https://www.ibm.com/developerworks/cn/websphere/library/techarticles/1305_jiangpl_rad/1305_jian ...
- 关于在IE下面promise兼容的解决办法
下载es6-promise就可以解决这个问题 import ES6Promise from "es6-promise" if(!window.Promise) { console. ...
- bzoj 2797 [Poi2012]Squarks 枚举一个,推出所有
题目大意 设有n个互不相同的正整数{X1,X2,...Xn},任取两个Xi,Xj(i≠j),能算出Xi+Xj. 现在所有取法共n*(n-1)/2个和,要你求出X1,X2,...Xn. 输出所有满足条件 ...
- Gcd(bzoj 2818)
Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...
- tyvj 2002 扑克牌
期望DP 本题递推比较麻烦,可以记忆化搜索 注意搜索的边界条件 以及每一次转移 #include <iostream> #include <cstdio> #include & ...