You are given a positive integer n and a 2D integer array edges, where edges[i] = [ui, vi, wi].
There is a weightedconnected simple undirected graph with n nodes labeled from 0 to n - 1. Each [ui, vi, wi] in edges represents an edge between node ui and node vi with positive weight wi.
The cost of a path is the sum of weights of the edges in the path, excluding the edge with the maximum weight. If there are multiple edges in the path with the maximum weight, only the first such edge is excluded.
Return an integer representing the minimumcost of a path going from node 0 to node n - 1.
Excluding the first edge with maximum weight, which is 0 -> 1, the cost of this path is 1.
0 -> 2
The only edge weight on this path is 1.
Excluding the first edge with maximum weight, which is 0 -> 2, the cost of this path is 0.
The minimum cost is min(1, 0) = 0.
Β
Constraints:
2 <= n <= 5 * 104
n - 1 <= edges.length <= 109
edges[i] = [ui, vi, wi]
0 <= ui < vi < n
[ui, vi] != [uj, vj]
1 <= wi <= 5 * 104
The graph is connected.
Solutions
Solution 1: Dijkstra's Algorithm
The problem is essentially equivalent to finding a path from node \(0\) to node \(n-1\), where we have one opportunity to treat the weight of a traversed edge as \(0\), in order to minimize the sum of path weights.
We first convert \(\textit{edges}\) into an adjacency list \(\textit{g}\), where \(\textit{g}[u]\) stores all edges \((v, w)\) connected to node \(u\), indicating that there is an edge with weight \(w\) between node \(u\) and node \(v\).
Next, we use Dijkstra's algorithm to find the shortest path. We define a 2D array \(\textit{dist}\), where \(\textit{dist}[u][0]\) represents the minimum sum of path weights from node \(0\) to node \(u\) without using the opportunity to treat an edge weight as \(0\); \(\textit{dist}[u][1]\) represents the minimum sum of path weights from node \(0\) to node \(u\) having already used the opportunity to treat an edge weight as \(0\).
We use a priority queue \(\textit{pq}\) to store pending nodes. Initially, we enqueue \((0, 0, 0)\), indicating that we start from node \(0\), with a current path weight sum of \(0\), and haven't used the opportunity.
In each iteration, we dequeue the node \((\textit{cur}, u, \textit{used})\) with the minimum path weight sum from the priority queue. If the current path weight sum \(\textit{cur}\) is greater than \(\textit{dist}[u][\textit{used}]\), we skip this node.
If the current node \(u\) is node \(n-1\) and we have already used the opportunity \(\textit{used} = 1\), we return the current path weight sum \(\textit{cur}\).
For each edge \((v, w)\) of node \(u\), we calculate the path weight sum to reach node \(v\) without using the opportunity: \(\textit{nxt} = \textit{cur} + w\). If \(\textit{nxt} < \textit{dist}[v][\textit{used}]\), we update \(\textit{dist}[v][\textit{used}]\) and enqueue \((\textit{nxt}, v, \textit{used})\).
If we haven't used the opportunity yet \(\textit{used} = 0\), we calculate the path weight sum to reach node \(v\) when using the opportunity: \(\textit{nxt} = \textit{cur}\). If \(\textit{nxt} < \textit{dist}[v][1]\), we update \(\textit{dist}[v][1]\) and enqueue \((\textit{nxt}, v, 1)\).
After the traversal ends, we return \(\textit{dist}[n-1][1]\) as the answer.
The time complexity is \(O(m \times \log n)\), and the space complexity is \(O(n + m)\), where \(n\) and \(m\) are the number of nodes and edges, respectively.