ZOJ 3626 Treasure Hunt I 树上DP
E - Treasure Hunt I
Time Limit:2000MS
Memory Limit:65536KB
Description
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. "V1V2 ... 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. "ijTi" 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
Sample 1: CC can go to town 2 and return at day 2.
Sample 2: CC can't come back within 1 day. So he can only take the treasure in his hometown.
Sample 3: CC only need 2 days to collect all the treasure.
题意
简单来说,这道题是给你一颗树,然后每个点有一个价值,每个边有一个代价,然后问你,从k点出发,花费最多m/2的代价,能够取得最多的价值是多少。
题解
这道题实际上是一个树上背包问题,dp[i][j]表示从i点出发,花费j的代价所能取得的最大价值是多少。
转移方程为 dp[i][j]=max(dp[i][j],dp[i][m-k-t[i][v]]+dp[v][k])
跑一发就好!
吐槽
markdown这个编辑器真TM难用!
#define N 105
int val[N];
vector<int> adj[N*2];
int w[N][N];
int dp[N][N],vis[N],m;
void dfs(int u)
{
vis[u]=1;
dp[u][0]=val[u];
for(int i=0;i<adj[u].size();i++)
{
int v=adj[u][i];
if(vis[v]==0)
{
dfs(v);
for(int j=m;j>=0;j--)
{
for(int k=0;k<=j-w[u][v];k++)
{
dp[u][j]=max(dp[u][j],dp[u][j-k-w[u][v]]+dp[v][k]);
}
}
}
}
}
int main()
{
int n,a,b,c,k;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
adj[i].clear();
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&val[i]);
dp[i][0]=val[i];
}
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
adj[a].push_back(b);
adj[b].push_back(a);
w[a][b]=w[b][a]=c;
}
scanf("%d%d",&k,&m);
m/=2;
memset(vis,0,sizeof(vis));
dfs(k);
int ans=-1;
for(int i=0;i<=m;i++)
{
if(dp[k][i]>ans)
ans=dp[k][i];
}
printf("%d\n",ans);
}
return 0;
}
ZOJ 3626 Treasure Hunt I 树上DP的更多相关文章
- ZOJ 3626 Treasure Hunt I(树形dp)
Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous country since ...
- 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】Treasure Hunt I
[ZOJ3626]Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous coun ...
- ZOJ 3626(树形DP+背包+边cost)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...
- zoj Treasure Hunt IV
Treasure Hunt IV Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is exploring the wonderland ...
- ZOJ3629 Treasure Hunt IV(找到规律,按公式)
Treasure Hunt IV Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is exploring the wonderland ...
随机推荐
- MVVM模式用依赖注入的方式配置ViewModel并注册消息
最初的想法 这次主要讨论下给View指定ViewModel的事情.一般来说给View指定ViewModel常用的方式有两种,一种是在View的后台代码中写DataContext = new ViewM ...
- Runtime - 消息发送原理
Runtime - 消息发送原理. Objective-C运行时的核心就在于消息分派器objc_msgSend,消息分派器把选择器映射为函数指针,并调用被引用的函数. 要想理解objc_msgSend ...
- pip安装遇到问题
安装pip之后,在cmd下输入 pip --version始终提示: Unknown option:versionDid not provide a command自己安装步骤没错,怎么想也不明白,无 ...
- linux nat网络配置
1. 2 . 3. BOOTPROTO = static ONBOOT=yes #开启自动启用网络连接 IPADDR0=192.168.21.128 #设置IP地址 PREFIXO0=24 #设 ...
- SQLServer系统变量使用
1.@@IDENTITY返回最后插入的标识值.这个变量很有用,当你插入一行数据时,想同时获得该行的的ID(标示列),就可以用@@IDENTITY示例:下面的示例向带有标识列的表中插入一行,并用 @@I ...
- SourceTree 3.0.17如何跳过注册进行安装? — git图形化工具(一)
SourceTree 3.0.17个人版本的尝试跳过注册方式好几次都没成功,于是下载了企业版本https://www.sourcetreeapp.com/enterprise. 安装过程: 1.首次点 ...
- python 库资源大全
偶然的机会翻到这篇文章,很全面,来源: Python 资源大全中文版 哪些 Python 库让你相见恨晚? 环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 pyth ...
- ubuntu18.10安装网易云音乐
1.到网易云官网下载安装包(在18.10双击安装包没能安装成功,于是使用命令行) 2.执行安装命令 sudo dpkg -i 名称,这时会提示缺少依赖gconf-service等,提示执行命令,照做就 ...
- 2017-2018 ACM-ICPC, NEERC, Moscow Subregional Contest B - Byteland Trip dp
B - Byteland Trip 题目大意:给你一个由'<' 和 '>'组成的串, 如果在'<' 只能前往编号比它小的任意点, 反之只能前往比它大的任意点,问你能遍历所有点 并且每 ...
- .NET Runtime version 2.0.50727.xxx 执行引擎错误。 (Fatal Execution Engine Error)
如题问题困扰本人良久. 尝试VS2005.VS2008.VS2010均出现过次问题. 主要现象: 1. Window设计器会崩溃,直接挂掉.(当逐条注释掉一些静态构造函数内的代码是情况好转) 2. 发 ...