#BS0000471. Các hành tinh (Planets)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Các hành tinh (Planets)
Planets
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Jack starts on planet 1 at time zero and must reach planet . Bidirectional gates have positive integer travel durations. Each planet has blocked integer departure times. If departure is blocked, wait one second and repeat until departure is allowed. Minimize the arrival time; no departure is required at the destination.
Input
Read , then triples describing a gate. The next lines each contain followed by strictly increasing blocked times .
Output
Print the minimum arrival time, or -1 if unreachable.
Subtasks
Subtask 1 (20 points)
- , ,
- , , ; no parallel edges; , , each list is strictly increasing; all data are integers.
Subtask 2 (30 points)
- , ,
- , , ; no parallel edges; , , each list is strictly increasing; all data are integers.
Subtask 3 (50 points)
- , ,
- , , ; no parallel edges; , , each list is strictly increasing; all data are integers.
Examples
Example 1
Input
4 6
1 2 2
1 3 3
1 4 8
2 3 4
2 4 5
3 4 3
0
1 3
2 3 4
0
Output
7
Explanation
Via planet 2, depart at time 2 and travel five more seconds. Planet 3 requires waiting until time 5; the direct route costs eight.
Example 2
Input
3 1
1 2 3
0
1 3
0
Output
-1
Explanation
Planet 3 is unreachable.