Ferries
Time limit2sMemory limit512 MB
Captains at each island reassign fixed ferry fares among destinations to maximize the cheapest fare from island 1 to island N.
- Level
Hard8 of 10
- Topics
- Shortest path, Greedy, Sorting, Game theory
- Solved
- No attempts yet
Problem
Kang the penguin lives on a group of Antarctic islands numbered to . His house is on island . He has a cold today, so he wants to visit the veterinarian who works on island .
He would normally swim, but with the cold he plans to take ferries instead. There are ferries, numbered to . Ferry carries passengers from island to island for dollars, and it runs in that direction only. At most one ferry runs from one island to another island, and a ferry may charge dollars. Kang wants to reach island as cheaply as possible.
Unfortunately for the penguin, the captains have started a money making scheme today. They know Kang plans to ride from island to island , so they agreed to make his trip as expensive as they can. Captains whose ferries depart from the same island may swap destinations among themselves. Their contracts fix the fare of each ferry, so a fare stays with its ferry even after its destination changes. Suppose ferries , and all depart from island , they go to islands , and , and they charge , and dollars. The captains of ferry and ferry may trade destinations, after which ferry goes to island and still charges dollars, while ferry goes to island and still charges dollars.
The captains announce the final destinations before Kang boards any ferry, and they cannot change them afterwards. Kang knows what the captains are planning, but he does not know the destinations before he leaves his house. He wants the smallest amount of money that is certain to be enough for the trip. In other words, find the cost of Kang's cheapest route to island when the captains arrange the destinations so that this cheapest route costs as much as possible.
Input
Your program reads from standard input. The first line contains two integers and . Each of the next lines contains three integers , and , describing one ferry. A route from island to island always exists.
Output
Your program writes one integer to standard output, the minimum number of dollars Kang needs to reach his doctor.