#BS0000441. Sắp xếp bò qua lỗ sâu (Wormhole Sort)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp bò qua lỗ sâu (Wormhole Sort)
Wormhole Sort
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
Farmer John's barn has cows and locations, each numbered from to . Cow starts at location , with exactly one cow at each location.
There are bidirectional wormholes. Wormhole links locations and and has width . Cows at its two endpoints may exchange locations. Exchanges may be repeated in any order.
Place cow at location for every . Among all sorting sequences, maximize the minimum width of the wormholes used. If no exchange is necessary, the result is -1. A sorting sequence always exists.
Input
- The first line contains integers , the numbers of cows and wormholes.
- The second line contains integers , where is cow 's initial location.
- Each of the next lines contains integers , the endpoints and width of wormhole .
Output
Print one integer: the maximum achievable minimum width, or -1 if for every .
Subtasks
- Subtask 1 (40 points): ; is a permutation of ; , , for every . Sorting is possible.
- Subtask 2 (60 points): ; is a permutation of ; , , for every . Sorting is possible.
Examples
Example 1
Input
4 4
3 2 1 4
1 2 9
1 3 7
2 3 10
2 4 3
Output
9
Explanation
Cow 1 is at location 3 and cow 3 at location 1. Exchange locations (2,3), (1,2), (2,3), using widths 10, 9, 10. All cows reach their destinations. With widths above 9, location 1 is isolated, making sorting impossible.
Example 2
Input
4 1
1 2 3 4
4 2 13
Output
-1
Explanation
All four cows are already correctly placed; no wormhole is needed.