Path with hyperloops

4.7

3 votes
Algorithms, Graphs, Shortest Path Algorithms
Problem

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:

1N1051K,M1051(K+M)1051u,vN

Time Limit: 2
Memory Limit: 256
Source Limit:
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.

Editor Image

?