VOOZH about

URL: https://www.geeksforgeeks.org/dsa/shortest-path-with-one-curved-edge-in-an-undirected-graph/

โ‡ฑ Shortest path with one curved edge in an undirected Graph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Shortest path with one curved edge in an undirected Graph

Last Updated : 20 Nov, 2025

Given an undirected connected graph represented using an adjacency list adj[][][]. For each vertex i, the list adj[i] contains entries of the form {u, w1, w2}, where u is the neighboring vertex, w1 is the weight of the straight edge between i and u, and w2 is the weight of the curved edge between them. Thus, every pair of connected vertices has two parallel edges: one straight and one curved.
We are also given two vertices a and b, and we need to determine the minimum cost required to travel from a to b under the constraint that:

  • we may use any number of straight edges
  • we may use at most one curved edge in the entire path.

If no such path exists that satisfies this restriction, we must return -1.

Examples:

Input: a = 1, b = 3, adj[][][] = [[[1, 1, 4], [2, 2, 4], [3, 3, 1]],
[[0, 1, 4], [3, 6, 5]],
[[0, 2, 4]],
[[0, 3, 1], [1, 6, 5]]]

๐Ÿ‘ 1-

Output: 2
Explanation: We can follow the path 1->0->3. This gives a distance of 1+3 = 4 if we follow all straight paths. But we can take the curved path  from 0-> 3, which costs 1. This will result in a cost of 1+1 = 2

Input: a = 0, b = 1, adj[][][] = [[[1, 4, 1]],
[[0, 4, 1]]]

๐Ÿ‘ file

Output: 1
Explanation: Take the curved path from 0 to 1 which costs 1. 

To solve this problem, our goal is to find the shortest path from node a to node b using any number of straight edges but at most one curved edge. Since all edge weights in the graph are positive, the best algorithm for finding shortest paths is Dijkstraโ€™s algorithm, because it always picks the node with the smallest distance first and guarantees the minimum distance to every node it processes.

[Approach 1] Using Dijkstra with State Compression - O((V+E) log V) Time and O(V+E) Space

To handle the rule of using at most one curved edge, we track whether we have already used the curved edge while reaching each node. For this, we use a 2D distance array:

  • dist[node][0] -> shortest distance to node without using any curved edge
  • dist[node][1] -> shortest distance to node after using one curved edge

This allows us to maintain two separate states for every node and correctly compute paths that use zero or one curved edge.

We push states in the priority queue in the form (distance, node, usedCurve) and always process the smallest one first. When we explore edges from a node, we do two types of relaxations.

  • Straight edge - always allowed, updates dist[next][usedCurve].
  • Curved edge - allowed only if usedCurve == 0, and it updates dist[next][1] since after using it we cannot use another curved edge.

Finally, the answer is min(dist[b][0], dist[b][1]), because the shortest path may or may not use a curved edge. If both values are infinite, no valid path exists.


Output
2

[Approach 2] Using Two-Dijkstra Method with Curved-Edge Relaxation - O((V+E) log V) Time and O(V+E) Space

We want to travel from a to b, and the main challenge is deciding where to use the one curved edge. To simplify this, we first compute the shortest straight-edge distances from a to every node and from b to every node. Once we have distances, we can easily try placing the curved edge between any two connected nodes.

We know the distance from a -> every node, and the distance from b -> every node. Now for each curved edge we can form two possible paths:

  • a -> u (straight) + u -> v (curved edge) + v -> b (straight)
  • a -> v (straight) + v -> u (curved edge) + u -> b (straight)

These two combinations represent both ways of using the curved edge exactly once. However, the problem states that we may use at most one curved edge. Therefore, we also need to consider the case where we do not use any curved edge at all. This normal straight-path distance from a -> b is simply da[b]. So, the final answer will be the minimum among all these cases.


Output
2
Comment
Article Tags:
Article Tags: