Path with hyperloops
Practice
4.7 (3 votes)
Algorithms
Graphs
Shortest path algorithms
Problem
89% Success 890 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB 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\)

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...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
15 votes
Tags:
AlgorithmsGraphsMediumShortest Path Algorithm
Points:30
4 votes
Tags:
GraphsShortest Path AlgorithmsAlgorithms
Points:30
17 votes
Tags:
AlgorithmsApprovedDijkstra's algorithmGraphsMediumShortest Path Algorithm