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 ...
随机推荐
- webdriver高级应用- 启动FireFox的同时打开Firebug
1. 首先本机Firefox浏览器需要安装一下firebug插件,具体怎么安装这里不赘述,网上教程很多. 2. 具体自动化实现的代码如下: #encoding=utf-8 from selenium ...
- python - unittest - testsuite and runner
前置条件: 测试用例部分或全部编写完成 一. 生成测试集 1. 方法1 - 通过加载函数来加载测试用例 import unittest from TestCase.test_login import ...
- selenium2.53用45以下的火狐别太高
selenium2.53用45以下的火狐别太高在高的火狐需要selenium3
- mysql-学习链接
http://www.cnblogs.com/lyhabc/p/3691555.html
- jQuery 遍历函数 ,javascript中的each遍历
jQuery 遍历函数 jQuery 遍历函数包括了用于筛选.查找和串联元素的方法. 函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集合 ...
- 聊聊、Java Keytool P12 转 JKS
最近公司合作机构需要更改服务证书,总共给了 3 个文件过来.openapi-cert.p12.openapi-cert.key.openapi-cert.crt. openapi-cert.crt - ...
- 换一种思维看待PHP VS Node.js
php和javascript都是非常流行的编程语言,刚刚开始一个服务于服务端,一个服务于前端,长久以来,它们都能够和睦相处,直到有一天,一个叫做node.js的JavaScript运行环境诞生后,再加 ...
- Spring 4.3.11.RELEASE文档阅读(二):Core Technologies_IOC
在看这部分内容的时候,想了一些问题: 容器: 1,什么是容器 用来包装或装载物品的贮存器 2,容器能做什么 包装或装载物品 3,为什么需要容器 为什么要使用集装箱?如果没有容器会是什么样? 4,常见的 ...
- python操redis
Python操作redis python连接方式:点击 下面介绍详细使用 1.String 操作 redis中的String在在内存中按照一个name对应一个value来存储 set() #在Redi ...
- 【bzoj2085】[Poi2010]Hamsters Hash+倍增Floyd
题目描述 Tz养了一群仓鼠,他们都有英文小写的名字,现在Tz想用一个字母序列来表示他们的名字,只要他们的名字是字母序列中的一个子串就算,出现多次可以重复计算.现在Tz想好了要出现多少个名字,请你求出最 ...