21. Shortest Path Using At Most One Curved Edge
β GFG solution to the Shortest Path Using At Most One Curved Edge problem: find minimum path length using Dijkstra with state tracking for curved edge usage. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an undirected, connected graph with V vertices numbered from 0 to V-1 and E double-edges. Each double-edge connects vertices x and y with two options:
A straight edge with weight
w1A curved edge with weight
w2
Your task is to find the shortest path from vertex a to vertex b such that you can use at most one curved edge in the entire path. If no such path exists, return -1.
π Examples
Example 1
Input: V = 4, E = 4, a = 1, b = 3, edges[][] = [[0, 1, 1, 4], [0, 2, 2, 4], [0, 3, 3, 1], [1, 3, 6, 5]]
Output: 2
Explanation:
Path 1 -> 0 -> 3 using straight edges costs 1 + 3 = 4.
But using the curved edge from 0 -> 3 gives 1 + 1 = 2, which is optimal.Example 2
Input: V = 2, E = 1, a = 0, b = 1, edges[][] = [[0, 1, 4, 1]]
Output: 1
Explanation:
Take the curved path from 0 to 1 which costs 1.π Constraints
$1 \le V \le 10^6$
$0 \le E \le 10^6$
$0 \le a, b \le V - 1$
$0 \le \text{edges}[i][0], \text{edges}[i][1] \le V-1$
$0 \le \text{edges}[i][2], \text{edges}[i][3] \le 10^4$
β
My Approach
The optimal approach uses Dijkstra's Algorithm with State Tracking to handle the constraint of using at most one curved edge:
Dijkstra with State Tracking
Graph Representation:
Build an adjacency list where each edge stores both straight weight
w1and curved weightw2.Each node can be in two states: curved edge not used (state 0) or already used (state 1).
Distance Array:
Maintain a 2D distance array
d[V][2]where:d[u][0]= minimum distance to nodeuwithout using any curved edged[u][1]= minimum distance to nodeuafter using exactly one curved edge
Initialize all distances to infinity, except
d[a][0] = 0.
Priority Queue:
Use a min-heap priority queue storing
{distance, node, curved_used}.Process nodes in order of increasing distance.
Relaxation:
For each neighbor
vof current nodeu:Straight edge: Update
d[v][current_state]usingw1if it gives a shorter path.Curved edge: If curved edge hasn't been used yet (state 0), try updating
d[v][1]usingw2.
Result:
Return
min(d[b][0], d[b][1])as the shortest path to destinationb.If both are infinity, return
-1.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O((V + E) log V), where V is the number of vertices and E is the number of edges. Each edge is relaxed at most twice (once for each state), and priority queue operations take O(log V) time.
Expected Auxiliary Space Complexity: O(V + E), where O(V) is used for the distance array storing two states per vertex, O(E) for the adjacency list, and O(V) for the priority queue in the worst case.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated