#P2296. [NOIP 2014 提高组] 寻找道路
[NOIP 2014 提高组] 寻找道路
Description
In a directed graph where every edge has length , you are given a start and a terminal. Please find a path from the start to the terminal that satisfies the following conditions:
- For every vertex on the path, all endpoints of its outgoing edges are directly or indirectly able to reach the terminal.
- Among all paths satisfying condition 1, the path should be the shortest.
Note: Graph may contain multiple edges and self-loops. It is guaranteed that the terminal has no outgoing edges.
Please output the length of a path that satisfies the conditions.
Input Format
The first line contains two integers and separated by a space, denoting that the graph has vertices and edges.
Each of the next lines contains two integers separated by a space, indicating there is a directed edge from vertex to vertex .
The last line contains two integers separated by a space, indicating that the start is and the terminal is .
Output Format
Output a single line containing an integer, the length of the shortest path that satisfies the problem statement. If such a path does not exist, output .
3 2
1 2
2 1
1 3
-1
6 6
1 2
1 3
2 6
2 5
4 5
3 4
1 5
3
Hint
Sample 1 Explanation.

As shown in the figure above, arrows represent directed roads and dots represent cities. The start is not connected to the terminal , so a path satisfying the problem statement does not exist, hence the output is .
Sample 2 Explanation.
As shown in the figure above, a path that satisfies the conditions is . Note that vertex cannot appear in the answer path because vertex has an edge to vertex , and vertex cannot reach the terminal .
Constraints.
- For 30% of the testdata, , .
- For 60% of the testdata, , .
- For 100% of the testdata, , , , .
Translated by ChatGPT 5
京公网安备 11011102002149号