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>(0$ \le$n$ \le$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>(1$ \le$d$ \le$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>(1$ \le$q$ \le$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>(0$ \le$x$ \le$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])

posted @ 2014-07-18 15:21  hyx1  阅读(650)  评论(0编辑  收藏  举报