Subway Stations
Time limit1.5sMemory limit1024 MB
Given a weighted tree, choose a path of at most K nodes that minimizes the maximum distance from any node to the path.
- Level
Hard8 of 10
- Topics
- Tree, Binary search, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Seoul City Mayor Hanbyeol plans to build a subway system to ease traffic congestion on the city's crowded roads. The budget allows at most stations, and all stations must be connected by a single path of roads.
Seoul is modeled as a connected graph with nodes and bidirectional edges, where each node is a building and each edge is a road. Hanbyeol must choose a simple path of nodes, where , and build a station on every node along that path.
Hanbyeol wants to minimize the maximum distance from any building to its nearest station. Write a program that finds the station placement that minimizes this value.
Input
The first line contains two space-separated integers , the number of nodes in the city, and , the maximum number of subway stations Hanbyeol can build. ()
Each of the next lines contains three space-separated integers , , and . These mean that the -th road connects nodes and , and its length is kilometers. (; ; if , then )
Output
Print the maximum distance from any building to its nearest station when this value is minimized, in kilometers.
Hint
(For Sogang students.) This problem is an adapted version that matches the format of a problem in a general programming contest. In the exam, the original scoring was:
- 55 points for Subtask 1.
- 40 points for Subtask 2.
- 3 points for Subtask 3.
- 2 points for Subtask 4.