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 ...
随机推荐
- TObject、TPersisent 、TComponent、TControl、TGraphicControl、TWinControl 关系图
VCL的类图结构 TObject | TPersisent | ...
- Unix IPC之Posix消息队列(2)
/* Query status and attributes of message queue MQDES. */ extern int mq_getattr (mqd_t __mqdes, stru ...
- 怎么使用T-sql生成两位字母
SQL code select char(cast(rand()*25 as int)+97)+char(cast(rand()*25 as int)+97) select 两 ...
- API文档管理平台
一.应用场景 在公司中,有很多开发,每个人维护的api接口是不一样的.如果有一个统一的api文档管理平台,每个开发,把自己维护的接口录入进去. 之后再开发别的功能时,不需要重复造轮子,直接调用就可以了 ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- 使用Github的gh-pages分支展示一个页面
Github有一个Github pages的功能可以搭建博客或者托管网页,是免费使用的. 首先你的注册Github账号 下载安装git Github官网操作 登录到Github上,创建一个名为 Git ...
- 为GHOST远控添加ROOTKIT功能
原版的ghost远控似乎有一个SSDT HOOK功能的模块,当然已经没有什么用处了.这里在GHOST的基础上添加一些ROOTKIT功能.而且随着x64下主动防御技术的发展,这里不打算使用传统的HOOK ...
- 浅析redux
一 redux 思想 首先,每一个webApp有且只有一个state tree,为方便管理和跟踪state的变化,也为了减少混乱,redux只允许通过发送(dispatch)action的方式来改变s ...
- mysql千万级表关联优化(2)
概述: 交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差,因此SQL优化任务交到了我手上. 这个 ...
- Spark(七)Spark内存调优
一.概述 Spark 作为一个基于内存的分布式计算引擎,其内存管理模块在整个系统中扮演着非常重要的角色.理解 Spark 内存管理的基本原理,有助于更好地开发 Spark 应用程序和进行性能调优.本文 ...