Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost Path with Edge Reversals

Try to solve the Minimum Cost Path with Edge Reversals problem.

Statement

You are given an integer n representing the number of nodes in a directed graph, labeled from 00 to n−1n - 1. You are also given a 2D2 {D} integer array edges, where edges[i] = [u_i, v_i, w_i] represents a directed edge from node u_i to node v_i with a traversal cost of w_i.

You can traverse any directed edge from u_i to v_i with cost w_i. Additionally, you may reverse any edge and traverse it in the opposite direction from v_i to u_i with a cost of 2×wi2 \times w_i.

Return the minimum total cost to travel from node 00 to node n−1n - 1. If node n−1n - 1 cannot be reached, return −1-1.

Constraints:

  • 2≤2 \leq n ≤5×104\leq 5 \times 10^4

  • 1≤1 \leq edges.length ≤105\leq 10^5

  • edges[i] =[ui,vi,wi]= [u_i, v_i, w_i]

  • 0≤ui,vi≤n−10 \leq u_i, v_i \leq n - 1

  • 1≤wi≤10001 \leq w_i \leq 1000

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost Path with Edge Reversals

Try to solve the Minimum Cost Path with Edge Reversals problem.

Statement

You are given an integer n representing the number of nodes in a directed graph, labeled from 00 to n−1n - 1. You are also given a 2D2 {D} integer array edges, where edges[i] = [u_i, v_i, w_i] represents a directed edge from node u_i to node v_i with a traversal cost of w_i.

You can traverse any directed edge from u_i to v_i with cost w_i. Additionally, you may reverse any edge and traverse it in the opposite direction from v_i to u_i with a cost of 2×wi2 \times w_i.

Return the minimum total cost to travel from node 00 to node n−1n - 1. If node n−1n - 1 cannot be reached, return −1-1.

Constraints:

  • 2≤2 \leq n ≤5×104\leq 5 \times 10^4

  • 1≤1 \leq edges.length ≤105\leq 10^5

  • edges[i] =[ui,vi,wi]= [u_i, v_i, w_i]

  • 0≤ui,vi≤n−10 \leq u_i, v_i \leq n - 1

  • 1≤wi≤10001 \leq w_i \leq 1000