Formatted question description: https://leetcode.ca/all/2203.html

# 2203. Minimum Weighted Subgraph With the Required Paths (Hard)

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 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 <= 105
• 0 <= edges.length <= 105
• edges[i].length == 3
• 0 <= fromi, toi, src1, src2, dest <= n - 1
• fromi != toi
• src1, src2, and dest are pairwise distinct.
• 1 <= weight[i] <= 105

Related Topics:
Graph, Shortest Path

Similar Questions:

Do Dijkstra 3 times.

First time: store the shortest distance from node a to all other nodes in array da.

Second time: store the shortest distance from node b to all other nodes in array db.

Third time: store the shortest distance from node dest to all other nodes via Reversed Graph in array dd.

The answer is the minimum da[i] + db[i] + dd[i] (0 <= i < N). // OJ: https://leetcode.com/problems/minimum-weighted-subgraph-with-the-required-paths/
// Time: O(ElogE + N)
// Space: O(E)
class Solution {
typedef pair<long, long> ipair;
public:
long long minimumWeight(int n, vector<vector<int>>& E, int a, int b, int dest) {
vector<vector<ipair>> G(n), R(n); // G is the original graph. R is the reversed graph
for (auto &e : E) {
int u = e, v = e, w = e;
G[u].emplace_back(v, w);
R[v].emplace_back(u, w);
}
vector<long> da(n, LONG_MAX), db(n, LONG_MAX), dd(n, LONG_MAX);
auto dijkstra = [&](vector<vector<ipair>> &G, int src, vector<long> &dist) {
priority_queue<ipair, vector<ipair>, greater<>> pq;
pq.emplace(0, src);
dist[src] = 0;
while (pq.size()) {
auto [cost, u] = pq.top();
pq.pop();
if (cost > dist[u]) continue;
for (auto &[v, w] : G[u]) {
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
pq.emplace(dist[v], v);
}
}
}
};
dijkstra(G, a, da);
dijkstra(G, b, db);
dijkstra(R, dest, dd);
long ans = LONG_MAX;
for (int i = 0; i < n; ++i) {
if (da[i] == LONG_MAX || db[i] == LONG_MAX || dd[i] == LONG_MAX) continue;
ans = min(ans, da[i] + db[i] + dd[i]);
}
return ans == LONG_MAX ? -1 : ans;
}
};


## Discuss

https://leetcode.com/problems/minimum-weighted-subgraph-with-the-required-paths/discuss/1844091/