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的更多相关文章

  1. ZOJ 3626 Treasure Hunt I(树形dp)

    Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since ...

  2. ZOJ 3626 Treasure Hunt I (树形DP,常规)

    题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include ...

  3. zoj 3629 Treasure Hunt IV 打表找规律

    H - Treasure Hunt IV Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  4. zoj 3627 Treasure Hunt II (贪心)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:zoj-3627 题意 直线上有n个城市, 第i个城市和i+1个城市是相邻的.  每个城市都有vi的金币.   ...

  5. ZOJ 3627 Treasure Hunt II (贪心,模拟)

    题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集 ...

  6. 【树形dp】Treasure Hunt I

    [ZOJ3626]Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous coun ...

  7. ZOJ 3626(树形DP+背包+边cost)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...

  8. zoj Treasure Hunt IV

    Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland ...

  9. ZOJ3629 Treasure Hunt IV(找到规律,按公式)

    Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland ...

随机推荐

  1. 通过PDB文件实现非嵌入式的c++反射

    上一篇blog我阐述了一种实现非嵌入式的反射的基本思路.相比于通过宏和模板实现,这种非嵌入的反射的优点是不需要写额外的代码来记录meta信息. 首先,为了在c++中实现反射系统,我认为需要解决以下两个 ...

  2. unity 2d 游戏优化之路 遇坑记录

    情景说明:  unity 出的Android包,在目前一些主流机型跑都没有问题,但是在 小米3 这种比较老的机器上跑,报如下错误 GLSL compilation failed, no infolog ...

  3. tf.sequence_mask

    tf.sequence_mask >>> x=[1,2,3]>>> z=tf.sequence_mask(x)>>> sess.run(z)arr ...

  4. HTML网页自动跳转

    <meta http-equiv="refresh" content="3;URL=res.html">

  5. const 和 #define区别_fenglovel_新浪博客

    const 和 #define区别 (2012-12-11 14:14:07) 转载▼ 标签: 杂谈   (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使 ...

  6. 20165333 2016-2017-2 《Java程序设计》第1周学习总结

    20165333 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 java 的地位 Java 的特点 安装JDK 系统环境的设置 Java程序的编写,编译和运 ...

  7. 一步一步学习IdentityServer4 (6) Connect-OpenId Cookies SignIn SignOut 那些事

    先来看下下面的配置: JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthentication( o ...

  8. GridLayout 计算器

    <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android=" ...

  9. deploy.sh

    备份一下之前的一个脚本吧 #!bin/bash adb uninstall org.cocos2d.fishingjoy4 for apk in `find . -name '*.apk' | xar ...

  10. CCF CSP 201604-2 俄罗斯方块

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201604-2 俄罗斯方块 问题描述 俄罗斯方块是俄罗斯人阿列克谢·帕基特诺夫发明的一款休闲游 ...