码迷,mamicode.com
首页 > 其他好文 > 详细

LA 4015

时间:2014-07-19 19:03:48      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   width   

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>(0bubuko.com,布布扣nbubuko.com,布布扣500) <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>(1bubuko.com,布布扣dbubuko.com,布布扣10000) <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>(1bubuko.com,布布扣qbubuko.com,布布扣1000) <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>(0bubuko.com,布布扣xbubuko.com,布布扣5000000) <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,布布扣,bubuko.com

LA 4015

标签:des   style   http   color   os   width   

原文地址:http://www.cnblogs.com/hyxsolitude/p/3853599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!