You are given a positive integer n and a 2D integer array edges, where edges[i] = [ui, vi, wi].
There is a weighted connected 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 minimum cost of a path going from node 0 to node n - 1.
Example 1:
Input: n = 5, edges = [[0,1,2],[1,2,7],[2,3,7],[3,4,4]]
Output: 13
Explanation:
There is only one path going from node 0 to node 4: 0 -> 1 -> 2 -> 3 -> 4.
The edge weights on this path are 2, 7, 7, and 4.
Excluding the first edge with maximum weight, which is 1 -> 2, the cost of this path is 2 + 7 + 4 = 13.
Example 2:
Input: n = 3, edges = [[0,1,1],[1,2,1],[0,2,50000]]
Output: 0
Explanation:
There are two paths going from node 0 to node 2:
0 -> 1 -> 2The edge weights on this path are 1 and 1.
Excluding the first edge with maximum weight, which is 0 -> 1, the cost of this path is 1.
0 -> 2The 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 * 104n - 1 <= edges.length <= 109edges[i] = [ui, vi, wi]0 <= ui < vi < n[ui, vi] != [uj, vj]1 <= wi <= 5 * 104Problem Overview: Given a weighted graph, compute the minimum distance from source to destination where the largest edge weight on the chosen path is ignored in the final cost. If a path has total weight sum and its maximum edge weight is maxEdge, the effective distance becomes sum - maxEdge. The task is to choose a path that minimizes this adjusted distance.
Approach 1: Brute Force Edge Exclusion + Dijkstra (O(E * (V log V)) time, O(V + E) space)
Iterate over each edge and treat it as the "excluded maximum" candidate. Temporarily adjust the graph so that the selected edge does not contribute to the distance calculation, then run Dijkstra to compute the shortest path from source to destination. Track the minimum result across all candidates. This works because the excluded edge must be the largest edge on the chosen path, but the approach is expensive since shortest path computation runs for every edge.
Approach 2: Modified Dijkstra Tracking Maximum Edge (O(E log V) time, O(V) space)
Use a priority queue similar to standard Dijkstra, but each state keeps two values: the total path sum and the largest edge weight seen so far. The priority is determined by sum - maxEdge, which represents the effective cost after excluding the largest edge. When relaxing an edge, update sum and recompute maxEdge = max(currentMax, edgeWeight). Push the new state into the heap and keep the best effective distance for each node. This approach explores paths in increasing order of the adjusted cost and efficiently finds the optimal route.
The algorithm behaves like standard shortest path search but carries extra state to account for the removable maximum edge. Graphs with up to thousands of nodes remain efficient because the heap operations dominate the complexity.
Recommended for interviews: Interviewers expect the optimized modified Dijkstra solution. The brute force version shows understanding of shortest path fundamentals, but recognizing that the largest edge can be tracked during traversal demonstrates deeper knowledge of graph algorithms, Dijkstra's algorithm, and shortest path optimization. The final complexity of O(E log V) matches standard shortest path performance while handling the extra constraint.
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 edges into an adjacency list g, where 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 dist, where 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; 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 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 (cur, u, used) with the minimum path weight sum from the priority queue. If the current path weight sum cur is greater than dist[u][used], we skip this node.
If the current node u is node n-1 and we have already used the opportunity used = 1, we return the current path weight sum cur.
For each edge (v, w) of node u, we calculate the path weight sum to reach node v without using the opportunity: nxt = cur + w. If nxt < dist[v][used], we update dist[v][used] and enqueue (nxt, v, used).
If we haven't used the opportunity yet used = 0, we calculate the path weight sum to reach node v when using the opportunity: nxt = cur. If nxt < dist[v][1], we update dist[v][1] and enqueue (nxt, v, 1).
After the traversal ends, we return 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.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Edge Exclusion + Dijkstra | O(E * (V log V)) | O(V + E) | Useful for understanding the problem or when the graph is very small |
| Modified Dijkstra Tracking Maximum Edge | O(E log V) | O(V) | General case and interview‑expected optimal solution |
Practice Minimum Distance Excluding One Maximum Weighted Edge with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor