Path with hyperloops
Algorithms
Graphs
Shortest path algorithms
90.0% success
1,507 attempts
2s time
256MB memory
1024KB max code

You live in year \(2050\) where hyperloop has been invented. Currently you are in city \(X\) and you want to reach city \(Y\) using minimum amount of money. From city \(u\) to \(v\) if you use hyperloop than it costs \(2\) dollars otherwise it costs \(1\) dollar.

Print the minimum cost to reach the end city from the starting city.

Input Format:
you are given \(N, M, K, start, end\) - the number of cities, number of roads and number of hyperloops, starting and ending city.

Next \(M\) line contains \(2\) values \(u\) and \(v\) meaning there is a bidirectional path between \(u\) and \(v\).

Next \(K\) lines contains \(2\) values \(u\) and \(v\) meaning there is a bidirectional hyperloop between city \(u\) and \(v\).

Output Format:
If it is impossible to reach the end city then print \(-1\)

Otherwise print the minimum cost to reach the end city from the starting city

Constraints:

\(1 \leq N \leq 10^5\)
\(1 \leq K,M \leq 10^5\)
\(1 \leq (K+M) \leq 10^5\)
\(1 \leq u,v \leq N\)

Examples
Input
5 3 3 1 3
1 5
5 4
2 3
1 4
1 2
4 3
Output
3
Explanation

It can be clearly seen that if go through path 1 -> 2 -> 3.

It costs 3 dollars and for all other paths it costs more than 3 dollars.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading Editor...