Problem
Shortest Path With K Free Edges
Learn this problemProblem statement
You are given an undirected weighted graph with n nodes labeled from 1 to n. The normal weighted edges are given in edges, where each edge is [u, v, w].
You are also given a list of special edges called magical bridges. Each bridge is given as [u, v] and can be traversed with cost 0. You may use at most k magical bridge traversals in total.
Return the minimum cost required to travel from node 1 to node n. If it is not possible, return -1.
Function
shortestPathWithKFreeEdges(n: int, edges: int[][], bridges: int[][], k: int) → longExamples
Example 1
n = 4edges = [[1, 2, 10], [2, 4, 10], [1, 3, 5], [3, 4, 20]]bridges = [[1, 4]]k = 1return = 0Use the magical bridge directly from node 1 to node 4 with cost 0.
Example 2
n = 3edges = [[1, 2, 5]]bridges = []k = 1return = -1Node 3 cannot be reached.
Constraints
- The graph is undirected.
edges[i] = [u, v, w]describes a weighted edge.bridges[i] = [u, v]describes a zero-cost magical bridge.0 <= k