The festival must go on
Time limit1sMemory limit256 MB
Pick M roads of a weighted tree so the longest path using only picked roads is as short as possible.
- Level
Medium7 of 10
- Topics
- Binary search, Tree, Greedy
- Solved
- No attempts yet
Problem
Cube World has cities and bidirectional roads. Every pair of distinct cities is joined by a route, so the road network is a tree. Cities are numbered from to , roads are numbered from to , and road has length .
The ruler of Cube World is holding a festival. He picks of the roads and runs one event on each picked road, such as a car parade or a sports match. The transport ministry complained that traffic would be paralysed, so the ruler decided to keep the disruption small. Among all ways to pick roads, he takes one that minimizes the maximum length of a simple path that uses only picked roads. The length of a path is the sum of the lengths of the roads on it, and a path that stays in one city has length .
For each test case, find that minimized maximum.
Input
The first line contains an integer (), the number of test cases.
The first line of each test case contains two space separated integers and (, ), where is the number of cities and is the number of roads to pick. The next lines describe the roads. The -th of them contains three space separated integers , , (, , ), meaning that road connects city and city and its length is .
The sum of over all test cases does not exceed .
Output
For each test case, print one line with the maximum length of a simple path over the picked roads, for a choice of roads that makes this value as small as possible.
Note
A simple path is a path that never repeats a city. It is a sequence of distinct cities such that for every with a road connects city and city . In this problem every such road has to be a picked road.