Loading...
Loading...
You are given an integer n, a list of roads roads and an integer k. There are n cities numbered from 0 to n - 1. Each element in roads contains [a, b, w] meaning there is a road of length w between city a and city b. The roads form a tree, so there is exactly one path between any two cities. The distance between two cities is the sum of the road lengths on that path.
You may choose at most k cities to host a station. For a chosen set of stations, the cost of a city is its distance to the nearest station. The cost of the set is the largest cost among all cities.
Return the smallest cost that a nonempty set of at most k cities can achieve. You can assume that 1 <= k <= n, so at least one station can always be placed.
Input: n = 1
roads = []
k = 1
Output: 0
Explanation: The only city hosts a station, so its distance to the nearest station is `0`.
Input: n = 4
roads = [[0, 1, 1],
[1, 2, 1],
[2, 3, 1]]
k = 1
Output: 2
Explanation: With a single station at city `1` or city `2`, the farthest city is at distance `2`, and no single station can do better.
Input: n = 4
roads = [[0, 1, 5],
[0, 2, 7],
[0, 3, 2]]
k = 2
Output: 5
Explanation: With stations at city `0` and city `2`, every city is within distance `5` of a station, and city `1` is at distance `5`. No set of at most `2` cities can achieve a smaller largest distance.
n ≤105roads.length =n−1roads[i].length =3a ≤n−1b ≤n−1a = bw ≤104k ≤nroads describes a tree: it is connected and has n−1 roads