Treasure Hunt I - ZOJ 3626 树形dp
来源:互联网 发布:四轴单片机 编辑:程序博客网 时间:2024/06/06 14:24
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
21 31 2 11 221 32 1 12 123 31 2 12 5
Sample Output
436
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.
题意:
给一棵n个节点的树, 节点编号1~n, 每个节点有权值val[i],经过这个节点就可以获取这个价值(不能重复获得)每一条边有一个花费值w(i,j), 表示走完i和j点的边要花费w(i,j)
现在要从k点出发,总花费值为m,问总花费不超过m的情况下并且最终要回到出发点,最多可以获取多少价值?
思路:
简单树形dp。
f(i,j)表示子树i, 用花费j最多可以获得的价值对与i的每个儿子,可以选择分配花费2*w, 2*w+1, 2*w+2,...j给它,可以看作是一组物品
对所有儿子做分组背包
f(i, j) = max{ max{ f(i, j-k) + f(v, k-2*w) | 2*w<=k<=i } | v是i的儿子节点}
ans = f(k, m);
AC代码如下:
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>#include<cmath>using namespace std;vector< pair<int,int> >adj[110];int val[110],f[110][210],vis[110],k,m;void dfs(int u){ int i,j,e,v,w; vis[u]=true; for(i=0;i<=m;i++) f[u][i]=val[u]; for(e=0;e<adj[u].size();e++) { v=adj[u][e].first; w=adj[u][e].second; if(vis[v]) continue; dfs(v); for(i=m;i>=0;i--) for(j=w*2;j<=i;j++) f[u][i]=max(f[u][i],f[u][i-j]+f[v][j-2*w]); }}int main(){ int n,i,j,u,v,w; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) adj[i].clear(); for(i=1;i<=n;i++) scanf("%d",&val[i]); for(i=1;i<n;i++) { scanf("%d%d%d",&u,&v,&w); adj[u].push_back(make_pair(v,w)); adj[v].push_back(make_pair(u,w)); } memset(f,0,sizeof(f)); memset(vis,0,sizeof(vis)); scanf("%d%d",&k,&m); dfs(k); printf("%d\n",f[k][m]); }}
- ZOJ 3626 Treasure Hunt I(树形DP)
- ZOJ 3626 Treasure Hunt I / 树形DP
- Treasure Hunt I - ZOJ 3626 树形dp
- ZOJ-3626 Treasure Hunt I 树形DP
- zoj-3626 Treasure Hunt I (树形dp)
- zoj 3626 Treasure Hunt I(树形DP+分组背包)
- zoj 3626 Treasure Hunt I (树形dp)
- ZOJ 3626 Treasure Hunt I(树形dp)
- ZOJ 3626 Treasure Hunt I (树形dp)
- ZOJ 3626 Treasure Hunt I 树形背包
- zoj 3626 Treasure Hunt I(树形背包)
- [zoj 3626]Treasure Hunt I 树DP
- ZOJ 3626 —— Treasure Hunt I(树形DP + 背包)
- zoj 3626 Treasure Hunt I
- ZOJ 3626 Treasure Hunt I
- ZOJ 3626 Treasure Hunt I
- ZOJ 3626 Treasure Hunt I(树型DP+01背包)
- zoj 3626 Treasure Hunt I(树背包)
- linq+映射数据库调用方法
- 考CPA的战友们,你们有这么强悍的另一半吗?
- 13hibernate hibernate关联关系的CRUD(增删改查)
- 不更新也能排名上首页的策略
- 文件属性中的@符号
- Treasure Hunt I - ZOJ 3626 树形dp
- 在给文件输出的时候设置默认了怎么修改
- 未来地图,开启万物互联-华中雄
- 深入理解JavaScript系列 ----(11):执行上下文(Execution Contexts)
- 【考据】“烫烫烫”与“锟斤拷”的原理
- 深入浅出Cocoa之类与对象
- 设计模式之单例模式
- 论文读书笔记-主题-word representations learning & Evaluation
- mongodb shell commands