<p>You are given an integer <code>n</code> denoting the number of nodes of a <strong>weighted directed</strong> graph. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p>

<p>You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> denotes that there exists a <strong>directed</strong> edge from <code>from<sub>i</sub></code> to <code>to<sub>i</sub></code> with weight <code>weight<sub>i</sub></code>.</p>

<p>Lastly, you are given three <strong>distinct</strong> integers <code>src1</code>, <code>src2</code>, and <code>dest</code> denoting three distinct nodes of the graph.</p>

<p>Return <em>the <strong>minimum weight</strong> of a subgraph of the graph such that it is <strong>possible</strong> to reach</em> <code>dest</code> <em>from both</em> <code>src1</code> <em>and</em> <code>src2</code> <em>via a set of edges of this subgraph</em>. In case such a subgraph does not exist, return <code>-1</code>.</p>

<p>A <strong>subgraph</strong> is a graph whose vertices and edges are subsets of the original graph. The <strong>weight</strong> of a subgraph is the sum of weights of its constituent edges.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/17/example1drawio.png" style="width: 263px; height: 250px;" />
<pre>
<strong>Input:</strong> 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
<strong>Output:</strong> 9
<strong>Explanation:</strong>
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.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/17/example2-1drawio.png" style="width: 350px; height: 51px;" />
<pre>
<strong>Input:</strong> n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2
<strong>Output:</strong> -1
<strong>Explanation:</strong>
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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li>
	<li><code>edges[i].length == 3</code></li>
	<li><code>0 &lt;= from<sub>i</sub>, to<sub>i</sub>, src1, src2, dest &lt;= n - 1</code></li>
	<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
	<li><code>src1</code>, <code>src2</code>, and <code>dest</code> are pairwise distinct.</li>
	<li><code>1 &lt;= weight[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def minimumWeight(self, n: int, edges: List[List[int]], src1: int, src2: int, dest: int) -> int:
        g = defaultdict(lambda: defaultdict(lambda: float('inf')))
        for u, v, w in edges:
            g[u][v] = min(g[u][v], w)
            
        # first to calculate the optimal solution for distance from src node to other node
        def helper(start):
            vis1 = defaultdict(lambda: float('inf'), {start: 0})
            q = [(0, start)]
            while q:
                s1, node1 = heappop(q)
                if node1 == dest:
                    continue
                for nei1 in g[node1]:
                    w1 = g[node1][nei1]
                    if vis1[nei1] <= s1 + w1: continue
                    vis1[nei1] = s1 + w1
                    heappush(q, (s1 + w1, nei1))
            return vis1
        
        vis1, vis2 = helper(src1), helper(src2)              
        # can't reach destination               
        if dest not in vis1 or dest not in vis2: return -1        
        # distance after collided node
        def rest(start, vis):        
            q = [(0, start, 0)] # distance, current node, already collide with path from other source node
            tmp = defaultdict(lambda: float('inf'), {(start, 0): 0})
            if start in vis: 
                heappush(q, (vis[start], start, 1))
                tmp[start, 1] = vis[start]            
            while q:
                s2, node2, reached = heappop(q)
                if node2 == dest and reached: #not only need to reach dest but also need to collide with path from other source node
                    return s2
                for nei2 in g[node2]:
                    w2 = g[node2][nei2]
                    #if not collide before then check for collision
                    if not reached and nei2 in vis:
                        if tmp[(nei2, 1)] <= s2 + w2 + vis[dest]: continue
                        tmp[(nei2, 1)] = s2 + w2 + vis[nei2]
                        heappush(q, (s2 + w2 + vis[nei2], nei2, 1))
                    # otherwise goes for the rest path
                    else:
                        if tmp[(nei2, reached)] <= s2 + w2: continue
                        tmp[(nei2, reached)] = s2 + w2
                        heappush(q, (s2 + w2, nei2, reached))        
        return min(rest(src1, vis2), rest(src2, vis1))
100900