K-th shortest walk
You are given a directed graph on n nodes as edges [u, v, w] with positive integer weights, a source s, a target t, and an integer k. Return the length of the k-th shortest WALK from s to t, where walks may repeat nodes and edges and the same length may be achieved by different walks (each counted separately). Order walks by total length ascending; return the k-th value. Return -1 if fewer than k such walks exist. Constraints: up to 50 nodes; weights are positive (so no zero-cost cycles); k up to a few hundred.
Implement
kth_shortest(n: int, edges: list[list[int]], s: int, t: int, k: int) → intExamples
in
[3,[[0,1,1],[1,2,1],[1,1,2]],0,2,2]out4What 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
solution.py
InputExpectedGot
[3,[[0,1,1],[1,2,1],[1,1,2]],0,2,2]4not run yetsample