#abc287h. Ex - Directed Graph and Query
Ex - Directed Graph and Query
Score : points
问题描述
存在一个含有 个顶点和 条边的有向图。顶点编号为 到 ,第 条有向边从顶点 指向顶点 。
在该图上,一条路径的成本定义为:
- 路径上(包括起点和终点)顶点的最大索引值。
对于每个 ,解决以下问题:
- 找出从顶点 到顶点 的最小成本路径。如果不存在这样的路径,则输出
-1
。
由于可能存在的大规模输入和输出,建议使用快速输入输出方法。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a directed graph with vertices and edges. The vertices are numbered through , and the -th directed edge goes from vertex to vertex .
The cost of a path on this graph is defined as:
- the maximum index of a vertex on the path (including the initial and final vertices).
Solve the following problem for each .
- Find the minimum cost of a path from vertex to vertex . If there is no such path, print
-1
instead.
The use of fast input and output methods is recommended because of potentially large input and output.
Constraints
- If , then .
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines.
The -th line should contain the answer for .
Sample Input 1
4 4
1 2
2 3
3 1
4 3
3
1 2
2 1
1 4
Sample Output 1
2
3
-1
For , the path via the -st edge from vertex to vertex has a cost of , which is the minimum.
For , the path via the -nd edge from vertex to vertex and then via the -rd edge from vertex to vertex has a cost of , which is the minimum.
For , there is no path from vertex to vertex , so -1
should be printed.
update @ 2024/3/10 12:01:00