BackmediumUncategorized uncategorized medium

Minimum Weighted Path Distance Solution

Problem Statement

Given a directed weighted graph represented by an adjacency list, where nodes are indexed from 0 to N-1, determine the shortest path distance from a source node `src` to a target node `dst`. Each edge has a non-negative weight. If no path exists between the source and the target, return -1.

Example 1
Input
n = 5, edges = [[0,1,10], [1,2,5], [0,2,20]], src = 0, dst = 2
Output
15

Explanation: The path 0 -> 1 -> 2 results in a total weight of 15, which is less than the direct path of 20.

Constraints

  • 2 <= n <= 10^4
  • 0 <= edges.length <= 10^5
  • 0 <= weight <= 10^6
  • src != dst
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free