• 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])

  • 相关阅读:
    移动项目开发笔记(禁止一个按钮在服务器事件处理完成前连续点击按钮)
    网页设计div+css之id与class使用原则
    Windows Media Encode 9简介及SDK
    酒桌上的规矩,社会的潜规则
    实现最小宽度的几种方法及CSS Expression
    Css的zindex属性与Flash动画层叠需注意
    asp.net很有用的字符串操作类
    生活中的几种心态
    Silverlight监测工具:Silverlight Spy
    silverlight为控件注册属性
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3853599.html
Copyright © 2020-2023  润新知