K-th shortest path
Your task
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.
Examples
kth_shortest(n: int, edges: list[list[int]], src: int, dst: int, k: int) -> intExample 1
Input
kth_shortest(4, [[0, 1, 1], [0, 2, 4], [1, 2, 2], [2, 3, 1], [1, 3, 5]], 0, 3, 2)Output
5