LA 4015

It is said that the people of Menggol lived in caves. A tribe‘s caves were connected to each other with paths. The paths were so designed that there was one and only one path to each cave. So the caves and the paths formed a tree. There was a main cave, which connected the world outside. The Menggolian always carved a map of the tribe‘s caves on the wall of the main cave.

Scientists have just discovered Menggolian‘s tribe. What a heart-stirring discovery! They are eager to explore it. Since the terrain is very complex and dangerous, they decide to send out a robot.

The robot will be landed into the main cave, where he will begin his adventure. It doesn‘t have to return to the main cave, because the messages of his exploration will be sent immediately to the scientists while he is on the way.

A robot can only walk x <tex2html_verbatim_mark>meters before it runs out of energy. So the problem arises: given the map of the tribe‘s caves and a set of x <tex2html_verbatim_mark>, how many caves can be explored at most?

Input

There are multiple test cases in the input file. Each test case starts with a single number n <tex2html_verbatim_mark>(0n500) <tex2html_verbatim_mark>, which is the number of caves, followed by n - 1 <tex2html_verbatim_mark>lines describing the map. Each of the n - 1 <tex2html_verbatim_mark>lines contains three integers separated by blanks: i <tex2html_verbatim_mark>, j <tex2html_verbatim_mark>, and d <tex2html_verbatim_mark>(1d10000) <tex2html_verbatim_mark>. It means that the i <tex2html_verbatim_mark>-th cave‘s parent caveis the j <tex2html_verbatim_mark>-th cave and the distance is d <tex2html_verbatim_mark>meters. A parent cave of cave i <tex2html_verbatim_mark>is the first cave to enter on the path from i <tex2html_verbatim_mark>to the main cave. Caves are numbered from 0 to n - 1 <tex2html_verbatim_mark>. Then there is an integer q <tex2html_verbatim_mark>(1q1000) <tex2html_verbatim_mark>, which is the number of queries, followed by q <tex2html_verbatim_mark>lines. For one query, there is one integer x <tex2html_verbatim_mark>(0x5000000) <tex2html_verbatim_mark>, the maximum distance that the robot can travel. n = 0 <tex2html_verbatim_mark>indicates the end of input file.

Output

For each test case, output q <tex2html_verbatim_mark>lines in the format as indicated in the sample output, each line contains one integer, the maximum number of caves the robot is able to visit.

Sample Input

3
1 0 5
2 0 3
3
3
10
11
0

Sample Output

Case 1:
2
2
3

设dp[u][j][0]为以u为根,访问j个结点不返回u所需要的最小距离
设dp[u][j][1]为以u为根,访问j个结点返回u所需要的最小距离则 dp[u][j][0] = min(dp[v][k][0] + dp[u][j - k][1] + f[u][v], dp[v][k][1] + dp[u][j - k][1] + 2 * f[u][v])dp[u][j][1] = min(dp[v][k][1] + dp[u][j - k][1] + 2 * f[u][v])

LA 4015

时间: 2025-01-07 15:28:19

LA 4015的相关文章

LA 4015 Cave

树上dp 入度为零的点即没有父亲的点就是根节点 由于距离很大,不能加入到状态里,则考虑如何求出访问了 i (1 <= i <= n) 个点最小的距离 对与树的根节点而言,从它开始游历它的子树,要么在子树内结束,要么返回根节点 那么: 记 f[u][j] 表示从 u 出发访问其子树,访问了 j 个点,并且回到 u 的最小花费 记 g[u][j] 表示从 u 出发访问其子树,访问了 j 个点,最后停留在其子树内中的最小花费 讨论 u 的最终是否停留在子节点 v 的子树中,得到状态转移方程: g[u

动态规划(四)

\(1.String\) \(Painter,Chengdu\) \(2008,LA\) \(4394\) 题意: 给定两个长度相等,只有小写字母组成的字符串\(s\)和\(t\),每步可以把\(s\)的一个连续子串"刷"称同一个字母,问至少需要多少不才能把\(s\)变成\(t\).比如,\(s=bbbbbbb\),\(t=aaabccb\),最少需要两步可实现将\(s\)变成\(t\):\(bbbbbbb->aaabbbb->aaabccb\). 分析: 一个套路的区间\

hdu 5745 la vie en rose

这道题的官方题解是dp,但是可以暴力出来.改天再研究怎么dp. 暴力的时候,如果计算sum的时候,调用strlen函数会超时,可见这个函数并不是十分的好.以后能不用尽量不用. La Vie en rose Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 861    Accepted Submission(s): 461 Problem

让MAC OS也能使用LL LA L等LS的别名

linux下默认ll是ls -l的别名.OS X下默认不支持.习惯了linux下使用ll,我们同样也可以将习惯搬到os x下的shell中. 再当前用户家目录下新建.bash_profile文件.根据你的习惯,添加下面格式内容即可. 1 2 3 alias ll='ls -l' alias la='ls -a' alias l='ls -la' 然后执行:source .bash_profile你还可以添加你喜欢的其他别名.

LA 3942 Remember the Word (Trie)

Remember the Word 题目:链接 题意:给出一个有S个不同单词组成的字典和一个长字符串.把这个字符串分解成若干个单词的连接(单词可以重复使用),有多少种方法? 思路:令d[i]表示从字符i开始的字符串(后缀s[i..L])的分解数,这d[i] = sum{d(i+len(x)) | 单词x是其前缀}.然后将所有单词建成一个Trie树,就可以将搜索单词的复杂度降低. 代码: #include<map> #include<set> #include<queue>

LA 2678 Subsequence

有一个正整数序列,求最短的子序列使得其和大于等于S,并输出最短的长度. 用数组b[i]存放序列的前i项和,所以b[i]是递增的. 遍历终点j,然后在区间[0, j)里二分查找满足b[j]-b[i]≥S的最大的i,时间复杂度为O(nlongn). 这里二分查找用到库函数lower_bound() 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #inclu

LA 4127 - The Sky is the Limit (离散化 扫描线 几何模板)

题目链接 非原创 原创地址:http://blog.csdn.net/jingqi814/article/details/26117241 题意:输入n座山的信息(山的横坐标,高度,山底宽度),计算他们的轮廓线, 即露出来的表面边长,有些山是重叠的不计.空白地带不计,每座山都是等腰三角形. 分析:大白书P414页. 求小山的总长度,用一些虚线将其离散化,分成一段一段的,特征点:山脚,山顶,交点.这样就能保 证相邻两个扫描点之间再无交点.然后一最上面的点就是分割点,维护上一个点lastp即可. 1

LA 2031

Mr. White, a fat man, now is crazy about a game named ``Dance, Dance, Revolution". But his dance skill is so poor that he could not dance a dance, even if he dances arduously every time. Does ``DDR" just mean him a perfect method to squander his

Mac下ll、l、la、等简写命令不能使用

Mac默认用的也是Unix系统,Unix系统本身是没有这些简写命令的,可以通过给命令设置别名来使得可以使用这些简写命令 查看本机所有已经设置的命令别名:alias 设置命令别名:alias ll='ls -alF' 执行命令只在当前shell有效,要长期有效可以设置在用户的.bash_profile里面,这样每次登陆就都可以用这些简写命令了. 步骤: 1.vim ~/.bash_profile 2.将以下内容写入文件并保存 alias ll='ls -alF' alias la='ls -A'