This page is still under construction.

Parts of this page are still being built. What you see may change.

Reducing Network Diameter

Time limit2sMemory limit256 MB

Summary
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 w(j,k)w(j,k) of an edge (j,k)(j,k) is the time a message needs to travel from node jj to node kk, and w(j,k)=w(k,j)w(j,k) = w(k,j). The length of the path between two nodes ss and tt 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 cc for an edge (j,k)(j,k) reduces w(j,k)w(j,k) to max⁡{w(j,k)−c, 0}\max\{w(j,k)-c,\ 0\}, and cc can be any non-negative real number. Find the minimum total cost that makes the diameter of the tree network at most DD. For the network in Figure 1 with a target diameter D=26D = 26, the minimum cost is 4: pay 4 for the edge (5,6)(5,6) and its weight drops from 9 to 5. For D=0D = 0 the minimum cost is 40, because every edge weight has to become 0. For D=19D = 19 the minimum cost is 11.5, paying 3.5 for the edge (2,3)(2,3), 0.5 for the edge (3,4)(3,4), and 7.5 for the edge (3,5)(3,5). Write a program that computes the minimum cost for a given tree network and a target diameter DD.

Input

Your program reads from standard input. The first line holds the number of test cases TT. Each test case starts with a line holding the number of nodes nn (1≤n≤40,0001 \le n \le 40{,}000) and the target diameter DD (0≤D≤50,0000 \le D \le 50{,}000). Each of the next n−1n-1 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 11 and 50,00050{,}000, and node numbers are integers between 11 and nn. 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 DD. Print the value with one digit after the decimal point, rounded off from the second digit.

Examples3

  1. Example 1

    Input
    3
    6 26
    1 2 7
    6 5 9
    5 3 8
    3 2 6
    4 3 10
    6 0
    1 2 7
    6 5 9
    5 3 8
    3 2 6
    4 3 10
    6 19
    1 2 7
    6 5 9
    5 3 8
    3 2 6
    4 3 10
    
    Expected output
    4.0
    40.0
    11.5
    
  2. Example 2

    Input
    4
    1 0
    2 0
    1 2 5
    2 5
    1 2 10
    2 11
    1 2 10
    
    Expected output
    0.0
    5.0
    5.0
    0.0
    
  3. Example 3

    Input
    3
    4 10
    1 2 10
    1 3 10
    1 4 10
    4 9
    1 2 10
    1 3 10
    1 4 10
    4 0
    1 2 10
    1 3 10
    1 4 10
    
    Expected output
    15.0
    16.5
    30.0