atcoder#ABC254G. [ABC254G] Elevators
[ABC254G] Elevators
Score : points
Problem Statement
There is a complex composed of -story skyscrapers. The skyscrapers are numbered to , and the floors are numbered to .
From any floor of any skyscraper, one can use a skybridge to get to the same floor of any other skyscraper in one minute.
Additionally, there are elevators. The -th elevator runs between Floor and Floor of Skyscraper . With this elevator, one can get from Floor to Floor of Skyscraper in minutes, for every pair of integers such that .
Answer the following queries.
- Determine whether it is possible to get from Floor of Skyscraper to Floor of Skyscraper , and find the shortest time needed to get there if it is possible.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Each query is in the following format:
Output
Print lines. The -th line should contain -1
if, for , the destination is unreachable; otherwise, it should contain the minimum number of minutes needed to get there.
3 4 3
1 2 10
2 3 7
3 9 14
3 1 3
1 3 3 14
3 1 2 7
1 100 1 101
12
7
-1
For the -st query, you can get to the destination in minutes as follows.
- Use Elevator to get from Floor to Floor of Skyscraper , in minutes.
- Use the skybridge on Floor to get from Skyscraper to Skyscraper , in minute.
- Use Elevator to get from Floor to Floor of Skyscraper , in minutes.
For the -rd query, the destination is unreachable, so -1
should be printed.
1 1 1
1 1 2
1 1 1 2
1