We have a weighted directed graph and three special nodes: src1, src2, and dest. We need to find a subgraph (a subset of edges) such that both src1 and src2 can reach dest, and the total weight of edges in this subgraph is minimized.
Both paths from src1 and src2 to dest can share edges. If both paths pass through some common node before reaching dest, the edges from that common node to dest are counted only once. The problem reduces to finding a meeting node m where both paths converge, and from m a single path continues to dest.
In other words, we want to minimize: dist(src1, m) + dist(src2, m) + dist(m, dest) over all possible meeting nodes m.
n <= 10^5 and edges.length <= 10^5 -> Anything worse than near-linear-with-a-log-factor per traversal is too slow. A constant number of Dijkstra runs fits; running Dijkstra once per node does not.weighti >= 1 -> All weights are positive, so Dijkstra applies (no need for Bellman-Ford).10^5 edges each with weight up to 10^5 means a path can sum past 10^10, beyond 32-bit int. Distances and the final answer must use 64-bit integers.dest require a separate reverse graph rather than reusing the forward edges.For every possible meeting node m, compute the shortest path from src1 to m, from src2 to m, and from m to dest. The answer is the minimum sum across all valid meeting nodes. Computing dist(src1, m) and dist(src2, m) for all m takes two Dijkstra runs, but dist(m, dest) for every m is found by running Dijkstra separately from each node, which is O(n * E log V) and too slow for n = 10^5.
src1 to get dist1[v] for all v.src2 to get dist2[v] for all v.m where both dist1[m] and dist2[m] are finite, run Dijkstra from m and check dist(m, dest).dist1[m] + dist2[m] + dist(m, dest) across all valid m.Loading animation...
The next approach replaces the per-node Dijkstra runs with a single one. Computing dist(m, dest) for every m is itself a single-source shortest-path problem if the edges are reversed.
The bottleneck in the brute force is computing dist(m, dest) for every node m. Dijkstra already computes the shortest path from one fixed source to every other node. The distance from m to dest in the original graph equals the distance from dest to m in the graph with every edge reversed. So reversing all edges and running one Dijkstra from dest on that reversed graph yields dist(m, dest) for all m at once.
With three Dijkstra runs:
src1 on the forward graph gives dist1[m] for all m.src2 on the forward graph gives dist2[m] for all m.dest on the reverse graph gives distDest[m] = shortest path from m to dest.The answer is min(dist1[m] + dist2[m] + distDest[m]) over all nodes m.
Take any valid subgraph. It contains a path P1 from src1 to dest and a path P2 from src2 to dest. Both paths end at dest, so they share at least dest itself. Let m be the first node, walking backward from dest, that lies on both paths. From m onward the two paths coincide, so the subgraph cost is at least dist(src1, m) + dist(src2, m) + dist(m, dest), with each term a shortest distance. Minimizing this sum over every node m therefore lower-bounds every valid subgraph, and the bound is achieved by laying down the three shortest paths through the minimizing m. So the minimum sum is exactly the answer.
src1 on the forward graph to get dist1[].src2 on the forward graph to get dist2[].dest on the reverse graph to get distDest[].m from 0 to n-1, if all three distances are finite, compute dist1[m] + dist2[m] + distDest[m].Loading animation...