Interesting Outing
InterviewTime limit10sMemory limit1024 MB
Given a weighted tree, find the minimum total edge cost of a walk that visits every node, starting and ending at any nodes.
- Level
Medium4 of 10
- Topics
- Tree, DFS, Dynamic programming
- Solved
- No attempts yet
Problem
You are hosting visitors from out of town and want to take them out to see the most interesting places in town.
There are interesting sights you want to tour. You have identified interesting methods of transportation. Each method connects a pair of sights in both directions. There is exactly one way to get from any sight to any other without using any transportation method more than once.
You know the cost for the group to use each transportation method one time (you pay once per use). You choose the starting and ending sights, which can be the same or different. You do not count the cost of getting to the starting sight or coming back from the ending sight, only the transportation cost during the tour. What is the cheapest way to see all sights at least once each?
Input
The first line of the input gives the number of test cases, . test cases follow. Each test case starts with a line containing a single integer , the number of sights. Then, lines follow. The -th of these lines contains three integers , , and , meaning the -th transportation method connects sight and sight in both directions at a cost of coins per usage.
Output
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the minimum cost of a tour that visits each sight at least once.
Constraints
.
, for all .
It is possible to travel between any pair of sights using only the given transportation methods. (This and the previous limit imply the sights and transportation methods form an unrooted tree.)
for all .
Hint
In Sample Case #1 (see the picture), an optimal route, marked with a red line, visits sights .

In Sample Case #2, the only change from Sample Case #1 is that the transportation between sights and got more expensive, rising from to . The route now costs and is not optimal. The optimal route is instead, also marked in red in the picture.

In Sample Case #3, note that the answer may be larger than .