#abc254g. G - Elevators
G - Elevators
Score : points
问题描述
存在一个由 座 层摩天大楼组成的综合建筑群。这些摩天大楼按编号从 到 编号,楼层则按编号从 到 编号。
从任何一座摩天大楼的任意一层楼出发,都可以通过空中连廊在一分钟内到达另一座任意摩天大楼的同一层楼。
另外,还有 部电梯。第 部电梯在摩天大楼 的第 层与第 层之间运行。利用这部电梯,在满足 条件下的每一对整数楼层 和 ,可以在 分钟内从摩天大楼 的第 层到达第 层。
接下来需要回答 个查询。
- 确定是否可以从摩天大楼 的第 层到达摩天大楼 的第 层,并在可能的情况下找出到达那里所需的最短时间。
以上为通义千问 qwen-max 翻译,仅供参考。
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.
Sample Input 1
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
Sample Output 1
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.
Sample Input 2
1 1 1
1 1 2
1 1 1 2
Sample Output 2
1
update @ 2024/3/10 10:50:35