Reducing Network Diameter
Time limit2sMemory limit256 MB
Pay per unit of reduction on tree edge weights so the longest path between any two nodes is at most D at minimum total cost.
- Level
Hard8 of 10
- Topics
- Greedy, Tree, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
The computer network of a country forms a tree, so there is exactly one path between any two nodes. The weight of an edge is the time a message needs to travel from node to node , and . The length of the path between two nodes and is the sum of the weights of the edges on that path. The diameter of a tree network is the length of the longest path between two nodes. For example, the network in Figure 1 has diameter 30, on the path between node 1 and node 6. The diameter is the largest delay any pair of nodes can suffer, so it is an important parameter of a computer network.

Figure 1. A tree network
You can shorten the communication time of an edge by paying a cost for it. Paying a cost for an edge reduces to , and can be any non-negative real number. Find the minimum total cost that makes the diameter of the tree network at most . For the network in Figure 1 with a target diameter , the minimum cost is 4: pay 4 for the edge and its weight drops from 9 to 5. For the minimum cost is 40, because every edge weight has to become 0. For the minimum cost is 11.5, paying 3.5 for the edge , 0.5 for the edge , and 7.5 for the edge . Write a program that computes the minimum cost for a given tree network and a target diameter .
Input
Your program reads from standard input. The first line holds the number of test cases . Each test case starts with a line holding the number of nodes () and the target diameter (). Each of the next lines holds three integers that describe one edge: the two end nodes of the edge and its weight. Every edge weight is an integer between and , and node numbers are integers between and . The initial weights are integers, but a cost of real value can reduce a weight to a real number.
Output
Your program writes to standard output. For each test case, print on one line the minimum cost that makes the diameter of the tree network at most . Print the value with one digit after the decimal point, rounded off from the second digit.