Find minimum distance in a graph by adding at most one edge
Problem
Given a weighted directed graph with a source s and destination t, you may add at most one extra directed edge (from a given set of candidate edges, each with a weight). Find the minimum possible distance from s to t after adding at most one such edge (or none).
Input / Output
- Input: the graph (weighted adjacency list), source
s, destinationt, and the candidate edges you may add. - Output: the minimum
s -> tdistance achievable by adding at most one edge.
Constraints
- Weighted, directed graph; non-negative weights (Dijkstra applies).
- Add at most one additional edge.
- Target complexity: O(E log V).
Example
- If the direct
s -> tdistance is 20, but adding candidate edge(u, v, 3)givesdist_src[u] + 3 + dist_dst[v] = 12, the answer is 12.
asked …