Problem

Shortest Path With K Free Edges

ZorvynINTERNOA

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.

Examples
01 · Example 1
n = 4
edges = [[1, 2, 10], [2, 4, 10], [1, 3, 5], [3, 4, 20]]
bridges = [[1, 4]]
k = 1
return = 0

Use the magical bridge directly from node 1 to node 4 with cost 0.

02 · Example 2
n = 3
edges = [[1, 2, 5]]
bridges = []
k = 1
return = -1

Node 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
drafts saved locally
public long shortestPathWithKFreeEdges(int n, int[][] edges, int[][] bridges, int k) {
  // write your code here
}
n4
edges[[1, 2, 10], [2, 4, 10], [1, 3, 5], [3, 4, 20]]
bridges[[1, 4]]
k1
expected0
sign in to submit