Minimum Weighted Subgraph With the Required Paths Problem
Description
LeetCode Problem 2203.
You are given an integer n denoting the number of nodes of a weighted directed graph. The nodes are numbered from 0 to n - 1.
You are also given a 2D integer array edges where edges[i] = [from_i, to_i, weight_i] denotes that there exists a directed edge from from_i to to_i with weight weight_i.
Lastly, you are given three distinct integers src1, src2, and dest denoting three distinct nodes of the graph.
Return the minimum weight of a subgraph of the graph such that it is possible to reach dest from both src1 and src2 via a set of edges of this subgraph. In case such a subgraph does not exist, return -1.
A subgraph is a graph whose vertices and edges are subsets of the original graph. The weight of a subgraph is the sum of weights of its constituent edges.
Example 1:

Input: n = 6, edges = [[0,2,2],[0,5,6],[1,0,3],[1,4,5],[2,1,1],[2,3,3],[2,3,4],[3,4,2],[4,5,1]], src1 = 0, src2 = 1, dest = 5
Output: 9
Explanation:
The above figure represents the input graph.
The blue edges represent one of the subgraphs that yield the optimal answer.
Note that the subgraph [[1,0,3],[0,5,6]] also yields the optimal answer. It is not possible to get a subgraph with less weight satisfying all the constraints.
Example 2:

Input: n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2
Output: -1
Explanation:
The above figure represents the input graph.
It can be seen that there does not exist any path from node 1 to node 2, hence there are no subgraphs satisfying all the constraints.
Constraints:
3 <= n <= 10^50 <= edges.length <= 10^5edges[i].length == 30 <= from_i, to_i, src1, src2, dest <= n - 1from_i != to_isrc1,src2, anddestare pairwise distinct.1 <= weight[i] <= 10^5
Solution
Approach: Three Dijkstra runs
Two shortest paths computed independently would count their shared part twice, so we think about where the two paths join. Both end at dest, so there is a vertex p (possibly dest itself) where the walk from src1 and the walk from src2 meet; from p they share the path to dest. The cost of such a subgraph is the sum of three shortest distances: src1 → p, src2 → p and p → dest.
Run Dijkstra from src1 and from src2 on the original graph, and from dest on the reversed graph (this gives p → dest distances). Then try every vertex p, take the smallest d1[p] + d2[p] + d3[p] among vertices reachable in all three runs, and return -1 if there is none.
Complexity: O((n + m) log m) time, O(n + m) space.
from collections import defaultdict
from heapq import heappop, heappush
from math import inf
class Solution:
def minimumWeight(
self, n: int, edges: List[List[int]], src1: int, src2: int, dest: int
) -> int:
def dijkstra(g, u):
dist = [inf] * n
dist[u] = 0
q = [(0, u)]
while q:
d, u = heappop(q)
if d > dist[u]:
continue
for v, w in g[u]:
if dist[v] > dist[u] + w:
dist[v] = dist[u] + w
heappush(q, (dist[v], v))
return dist
g = defaultdict(list)
rg = defaultdict(list)
for f, t, w in edges:
g[f].append((t, w))
rg[t].append((f, w))
d1 = dijkstra(g, src1)
d2 = dijkstra(g, src2)
d3 = dijkstra(rg, dest)
ans = min(sum(v) for v in zip(d1, d2, d3))
return -1 if ans >= inf else ans