This page is still under construction.

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

Delta Quadrant

Time limit5sMemory limit128 MB

Summary
Starting anywhere on a weighted tree, find the shortest closed tour that visits all but k planets and returns to the start.
Level

Medium7 of 10

Topics
Dynamic programming, Tree
Solved
No attempts yet

Problem

Much of the Delta Quadrant is unexplored, and warring races hold many dangerous regions. A few neutral zones run from planet to planet, and travel along those is safe.

A critical summit is scheduled, but it stays on hold until a quorum of its members is present. Reaching that quorum means gathering almost every delegate in the Delta Quadrant at one location, where the Enterprise picks them up.

There are NN planets, and N−1N-1 safe paths through neutral zones connect them. Each path takes a known amount of time to traverse, and every planet is reachable from every other planet.

A single ship leaves from any planet, visits N−kN-k distinct planets counting the one it started from, and returns to that same planet. It may traverse a path more than once. Find the shortest total time.

Input

The first line has TT, the number of test cases. (1≤T≤501 \le T \le 50)

Each test case begins with a line holding two integers: NN, the number of planets, and kk, the number of planets that need not be visited. (2≤N≤10 0002 \le N \le 10\,000, 0≤k≤min⁡(N−1, 20)0 \le k \le \min(N-1,\ 20))

The next N−1N-1 lines each hold three integers: the two planet numbers the path connects, then the time needed to traverse that path. Planets are numbered 00 through N−1N-1, and each time is between 00 and 1 000 0001\,000\,000, inclusive. The given graph is always connected.

Output

For each test case, print the minimum travel time on its own line.

Examples1

  1. Example 1

    Input
    3
    2 0
    0 1 3000
    4 1
    0 1 81
    1 2 41
    2 3 59
    9 2
    0 1 1000
    1 2 1200
    0 3 1000
    3 4 1200
    0 5 1000
    5 6 1200
    0 7 1800
    7 8 600
    
    Expected output
    6000
    200
    13200