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] = [fromi, toi, weighti] denotes that there exists a directed edge from fromi to toi
with weight weighti.
Lastly, you are given three distinct integers src1, src2, and dest
denoting three distinct nodes of the graph.
Return theminimum weight of a subgraph of the graph such that it is
possible to reachdestfrom bothsrc1andsrc2via 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.

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 =5Output: 9Explanation:
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.

Input: n =3, edges =[[0,1,1],[2,1,1]], src1 =0, src2 =1, dest =2Output: -1Explanation:
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.
To find the minimum subgraph where both src1 and src2 can reach dest, we need to find a node mid such that both sources can reach mid and mid can reach dest. The optimal solution is the minimum sum of shortest paths from src1 to mid, src2 to mid, and mid to dest.
classSolution {
publicintminimumWeight(int n, int[][] edges, int src1, int src2, int dest) {
List<int[]>[] g =new List[n], rg =new List[n];
for (int i = 0; i < n; i++) { g[i]=new ArrayList<>(); rg[i]=new ArrayList<>(); }
for (int[] e : edges) {
g[e[0]].add(newint[]{e[1], e[2]});
rg[e[1]].add(newint[]{e[0], e[2]});
}
long[] d1 = dijkstra(n, g, src1);
long[] d2 = dijkstra(n, g, src2);
long[] dd = dijkstra(n, rg, dest);
long ans = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (d1[i]== Long.MAX_VALUE|| d2[i]== Long.MAX_VALUE|| dd[i]== Long.MAX_VALUE) continue;
ans = Math.min(ans, d1[i]+ d2[i]+ dd[i]);
}
return ans == Long.MAX_VALUE?-1 : (int)ans;
}
privatelong[]dijkstra(int n, List<int[]>[] g, int src) {
long[] dist =newlong[n];
Arrays.fill(dist, Long.MAX_VALUE);
dist[src]= 0;
PriorityQueue<long[]> pq =new PriorityQueue<>(Comparator.comparingLong(a -> a[0]));
pq.offer(newlong[]{0, src});
while (!pq.isEmpty()) {
long[] cur = pq.poll();
long d = cur[0]; int u = (int)cur[1];
if (d > dist[u]) continue;
for (int[] e : g[u]) {
int v = e[0], w = e[1];
if (dist[v]> d + w) {
dist[v]= d + w;
pq.offer(newlong[]{dist[v], v});
}
}
}
return dist;
}
}
import heapq
from typing import List
classSolution:
defminimumWeight(self, n: int, edges: List[List[int]], src1: int, src2: int, dest: int) -> int:
defdijkstra(graph, src):
dist = [float('inf')] * n
dist[src] =0 heap = [(0, src)]
while heap:
d, u = heapq.heappop(heap)
if d > dist[u]: continuefor v, w in graph[u]:
if dist[v] > d + w:
dist[v] = d + w
heapq.heappush(heap, (dist[v], v))
return dist
g = [[] for _ in range(n)]
rg = [[] for _ in range(n)]
for u, v, w in edges:
g[u].append((v, w))
rg[v].append((u, w))
d1 = dijkstra(g, src1)
d2 = dijkstra(g, src2)
dd = dijkstra(rg, dest)
ans = float('inf')
for i in range(n):
if d1[i] == float('inf') or d2[i] == float('inf') or dd[i] == float('inf'): continue ans = min(ans, d1[i] + d2[i] + dd[i])
return-1if ans == float('inf') else int(ans)