Shortest Distance
Ninjaland is a country consisting of ‘N’ states and ‘M’ paths. There are two types of paths that connect any two states. One is the normal path, and the other is a special path. Both paths have their respective lengths. You can use either of the paths to travel between two states. Your task is to determine the shortest distance between the two given states such that at most, one(possibly zero) special path is included in this path.
Note:
1. All the paths are directed.
2. Multiple paths can be present between two states.
3. All the states are connected with each other.
4. It does not have any self-loop.
5. At least one path always exists between the two given states.
For Example:
If ‘N’ = 3, ‘M’ = 2, and given values of paths are
[ [1, 2, 2, 3],
[2, 3, 4, 2] ]
You have to calculate the shortest distance between ‘X’ = 1 and ‘Y’ = 3
In the diagram, we can observe no direct edge from state 1 to state, 3 but we can go from state 1 to state 2 using the normal path of length 2 and then from state 2 to state 3 using the special path of length 2. So the total length will be 4, and we can clearly see that no other path can be smaller than this. Hence, the answer is 4.
Input Format:
The first line contains an integer ‘T’, which denotes the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, denoting the number of states and the number of paths, respectively.
The next ‘M’ lines contain four space-separated integers, ‘A’, ‘B’, ‘W1’, ‘W2’, denoting a normal path from ‘A’ to ‘B’ having a length ‘W1’ and a special path having a length ‘W2’.
The last line of each test case contains two space-separated integers, ‘X’ and ‘Y’, denoting the states between which the shortest distance has to be calculated.
Output Format:
For each test case, print an integer denoting the shortest path length between the two given states.
Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N < 10^3
N-1 <= M <= 10^3
1 <= A,B,X,Y <= N
1 <= W1,W2 <= 10^6
Time Limit: 1 sec
CodingNinjas
author
2y
1)First calculate how many steps you need to travel on the x-axis to reach the destination
2)Then calculate how many steps you need to travel on the y-axis to reach the destination
3)Add the no of steps...read more
CodingNinjas
author
2y
Special PathsSpace Complexity: O(1)Explanation: Time Complexity: O(1)Explanation:
Python (3.5)
Python (3.5)
'''
Time Complexity: O(N + M * log N)
Space Complexity: O(N),
Where N is the total number of nodes and...read more
Help your peers!
Add answer anonymously...
Top Optum Associate Software Engineer interview questions & answers
Popular interview questions of Associate Software Engineer
Top HR questions asked in Optum Associate Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app