Find the shortest weighted path between two nodes in an undirected graph

Quick Overview

Given an undirected graph with non-negative edge weights, find the minimum total weight of a path between a source node and a destination node, or return -1 when none exists. It tests weighted shortest-path reasoning, priority queue use, and careful handling of unreachable nodes and zero-weight edges.

Find the shortest weighted path between two nodes in an undirected graph

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an undirected, weighted graph with `n` nodes labeled `0` to `n - 1`. The graph is described by a list `edges`, where each `edges[i] = [u, v, w]` is an edge between nodes `u` and `v` with non-negative weight `w`. Given a source node `src` and a destination node `dst`, return the length of the shortest path from `src` to `dst`, where the length of a path is the sum of its edge weights. If no path exists, return `-1`. ### Function Signature ```python def shortest_path(n: int, edges: list[list[int]], src: int, dst: int) -> int: ``` ### Rules - Every edge can be traveled in both directions at the same cost `w`. - If `src == dst`, the answer is `0`. - The same pair of nodes may appear in more than one edge, and an edge may join a node to itself. Treat each one as a separate edge. ### Constraints - `1 <= n <= 10^4` - `0 <= len(edges) <= 10^5` - `len(edges[i]) == 3` - `0 <= u, v < n` - `0 <= w <= 10^4` - `0 <= src, dst < n` ### Examples ```text Input: n = 5, edges = [[0,1,4],[0,2,1],[2,1,2],[1,3,1],[3,4,3],[2,4,8]], src = 0, dst = 4 Output: 7 ``` The path `0 -> 2 -> 1 -> 3 -> 4` costs 1 + 2 + 1 + 3 = 7. Routes with fewer edges cost more: `0 -> 1 -> 3 -> 4` costs 8 and `0 -> 2 -> 4` costs 9. ```text Input: n = 4, edges = [[0,1,5],[2,3,2]], src = 0, dst = 3 Output: -1 ``` Nodes 0 and 1 are connected only to each other, and so are nodes 2 and 3, so no path leads from 0 to 3. ```text Input: n = 3, edges = [[1,0,2],[2,1,0]], src = 0, dst = 2 Output: 2 ``` The edges are listed as `[1,0,2]` and `[2,1,0]`, but they are undirected, so `0 -> 1 -> 2` is a valid path with cost 2 + 0 = 2. A zero-weight edge adds nothing to the total.

Overview: Given an undirected graph with non-negative edge weights, find the minimum total weight of a path between a source node and a destination node, or return -1 when none exists. It tests weighted shortest-path reasoning, priority queue use, and careful handling of unreachable nodes and zero-weight edges.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an undirected, weighted graph with n nodes labeled 0 to n - 1. The graph is described by a list edges, where each edges[i] = [u, v, w] is an edge between nodes u and v with non-negative weight w.

Given a source node src and a destination node dst, return the length of the shortest path from src to dst, where the length of a path is the sum of its edge weights. If no path exists, return -1.

Function Signature

def shortest_path(n: int, edges: list[list[int]], src: int, dst: int) -> int:

Rules

  • Every edge can be traveled in both directions at the same cost w .
  • If src == dst , the answer is 0 .
  • The same pair of nodes may appear in more than one edge, and an edge may join a node to itself. Treat each one as a separate edge.

Constraints

  • 1 <= n <= 10^4
  • 0 <= len(edges) <= 10^5
  • len(edges[i]) == 3
  • 0 <= u, v < n
  • 0 <= w <= 10^4
  • 0 <= src, dst < n

Examples

Input: n = 5, edges = [[0,1,4],[0,2,1],[2,1,2],[1,3,1],[3,4,3],[2,4,8]], src = 0, dst = 4
Output: 7

The path 0 -> 2 -> 1 -> 3 -> 4 costs 1 + 2 + 1 + 3 = 7. Routes with fewer edges cost more: 0 -> 1 -> 3 -> 4 costs 8 and 0 -> 2 -> 4 costs 9.

Input: n = 4, edges = [[0,1,5],[2,3,2]], src = 0, dst = 3
Output: -1

Nodes 0 and 1 are connected only to each other, and so are nodes 2 and 3, so no path leads from 0 to 3.

Input: n = 3, edges = [[1,0,2],[2,1,0]], src = 0, dst = 2
Output: 2

The edges are listed as [1,0,2] and [2,1,0], but they are undirected, so 0 -> 1 -> 2 is a valid path with cost 2 + 0 = 2. A zero-weight edge adds nothing to the total.