Code RoomShortest path with edge limit
MediumPrep Room Coding #1037

Shortest path with edge limit

CodingAlgorithms & data structuresMid–Senior~25 min

Given a directed weighted graph with n nodes (0..n-1), edges [u, v, w] (non-negative weights), a source src, a destination dst, and an integer K, return the minimum total weight of a path from src to dst that uses AT MOST K edges, or -1 if no such path exists. K may be 0 (then only src reaches itself). Assume 1 <= n <= 500 and K <= n; a layered Bellman-Ford-style relaxation (not plain Dijkstra) is the intended approach.

Implement
shortest_path_max_edges(n: int, edges: list[list[int]], src: int, dst: int, K: int) → int
Examples
in[4,[[0,1,100],[1,2,100],[0,2,500],[2,3,100]],0,3,2]out600
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 25 min
InputExpectedGot
[4,[[0,1,100],[1,2,100],[0,2,500],[2,3,100]],0,3,2]600not run yetsample