Shortest Path in Ninjaland

Ninjaland consists of ‘N’ states and ‘M’ paths. Each path connecting two states can either be a normal path or a special path, each with its unique length. Your task is to find the shortest distance between two given states such that the path includes at most one special path.

Example:

Input:
N = 3, M = 2
Paths = [[1, 2, 2, 3], [2, 3, 4, 2]]
X = 1, Y = 3
Output:
4
Explanation:

You can travel from state 1 to state 2 via the normal path (length 2) and from state 2 to state 3 via the special path (length 2), totaling 4.

Constraints:

  • 1 <= T <= 10
  • 1 <= N < 103
  • N-1 <= M <= 103
  • 1 <= A,B,X,Y <= N
  • 1 <= W1,W2 <= 106
  • Time Limit: 1 sec
Note:
All paths are directed with no self-loops, and all states are inter-connected. Multiple paths can exist between two states, but at least one path will always exist between the two given states.
Be the first one to answer
Add answer anonymously...
Optum Global Solutions Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter