Code RoomK-th shortest path
HardPrep Room Coding #853

K-th shortest path

CodingAlgorithms & data structuresSenior–Staff~35 min

Given a weighted directed graph on n nodes (edges [u, v, w] with w >= 1), a source src, a destination dst, and an integer k, return the length of the k-th shortest path from src to dst by total weight. Paths may revisit nodes (so multiple distinct-length routes through cycles count), and the k smallest path lengths are taken in non-decreasing order (with multiplicity). Return -1 if fewer than k distinct paths exist / dst is unreachable in k arrivals.

Implement
kth_shortest(n: int, edges: list[list[int]], src: int, dst: int, k: int) → int
Examples
in[4,[[0,1,1],[0,2,4],[1,2,2],[2,3,1],[1,3,5]],0,3,2]out5
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[4,[[0,1,1],[0,2,4],[1,2,2],[2,3,1],[1,3,5]],0,3,2]5not run yetsample