Karosai
InterviewTime limit1sMemory limit1024 MB
Given ponds joined by partitions with fixed heights, find the minimum water level at pond 1 that allows a path to pond N.
- Level
Medium4 of 10
- Topics
- Minimum spanning tree, Union-find, Graph
- Solved
- No attempts yet
Problem
Karosas Rosas sails on a pond system made of ponds. Some of the ponds are connected, so you can cross from one to another. Each connection has a partition with a fixed height, written , where . Karosas can cross from pond to pond only when the water level in pond is at least . For example, there are three ponds (). Ponds 1 and 2 are joined by a partition with , and ponds 2 and 3 are joined by a partition with . If the water level in pond 1 is at least 5000, Karosas can cross from pond 1 to pond 2. To reach pond 3 from pond 1, the level must be at least 7000.
In the picture, Karosas can get from pond 1 to pond 2 but not to pond 3. Karosas Rosas is staying in pond 1, and his friend is in pond . Rosas wants to know how high the water level in pond 1 must be so that he can visit his friend. Given the pond setup, find the minimum level that pond 1 must be raised to so that pond can be reached from pond 1.
Input
The first line contains two integers and , the number of ponds and the number of connected pairs. Each of the next lines contains three integers , , and , meaning ponds and are joined by a partition of height . (, and .)
Output
Print one integer, the minimum water level in pond 1 needed to reach pond from pond 1. The input guarantees that a path from pond 1 to pond always exists.
Constraints
, ,