Delta Quadrant
Time limit5sMemory limit128 MB
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 planets, and 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 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 , the number of test cases. ()
Each test case begins with a line holding two integers: , the number of planets, and , the number of planets that need not be visited. (, )
The next lines each hold three integers: the two planet numbers the path connects, then the time needed to traverse that path. Planets are numbered through , and each time is between and , inclusive. The given graph is always connected.
Output
For each test case, print the minimum travel time on its own line.