Highways
InterviewTime limit1sMemory limit128 MB
Find a spanning tree of a connected weighted graph that minimizes the maximum edge weight, and report that weight.
- Level
Medium5 of 10
- Topics
- Minimum spanning tree, Graph, Greedy, Union-find
- Solved
- No attempts yet
Problem
The roads of the Far Country are in very poor condition, yet they still let you travel from any city to any other (possibly by passing through several cities along the way). To fix this, the government of the Far Country decided to build highways. Because money is tight, they chose to build the smallest possible number of highways that still allow travel between any two cities. Each highway is built in place of one selected existing road, and the cost of rebuilding each road into a highway is known in advance.
The highways are built by private companies with public money. The government knows that public opinion cares less about the total cost of all highways and more about how much each individual company earns. For that reason they decided to build the highway network so that the cost of the single most expensive highway is as small as possible. Under this rule, what is the cost of the most expensive highway? All roads and highways are two-way.
Write a program that:
- reads the description of the Far Country's road system and the rebuild cost of each road from standard input,
- computes the cost of the most expensive highway, given that as few highways as possible are built while still allowing travel by highway between any two cities and while keeping the cost of the most expensive one as small as possible,
- writes the result to standard output.
Input
The first line contains two integers , (, ), separated by a single space. is the number of cities in the Far Country; cities are numbered from to . is the number of roads. Each road directly connects two cities.
Each of the next lines contains three integers, separated by single spaces, describing one road and its rebuild cost. The first two are the numbers of the two cities that the road connects, and the third is the cost of rebuilding that road into a highway. The rebuild cost of a single road is a positive integer no greater than .
Output
Output a single line containing the cost of the most expensive highway when the conditions above are satisfied.