Skip to content

Commit 7638a65

Browse files
Sync LeetCode submission Runtime - 207 ms (43.45%), Memory - 192.5 MB (46.83%)
1 parent ef73a6d commit 7638a65

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<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>
2+
3+
<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>
4+
5+
<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>
6+
7+
<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>
8+
9+
<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>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/17/example1drawio.png" style="width: 263px; height: 250px;" />
14+
<pre>
15+
<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
16+
<strong>Output:</strong> 9
17+
<strong>Explanation:</strong>
18+
The above figure represents the input graph.
19+
The blue edges represent one of the subgraphs that yield the optimal answer.
20+
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.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/17/example2-1drawio.png" style="width: 350px; height: 51px;" />
25+
<pre>
26+
<strong>Input:</strong> n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2
27+
<strong>Output:</strong> -1
28+
<strong>Explanation:</strong>
29+
The above figure represents the input graph.
30+
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.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
38+
<li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li>
39+
<li><code>edges[i].length == 3</code></li>
40+
<li><code>0 &lt;= from<sub>i</sub>, to<sub>i</sub>, src1, src2, dest &lt;= n - 1</code></li>
41+
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
42+
<li><code>src1</code>, <code>src2</code>, and <code>dest</code> are pairwise distinct.</li>
43+
<li><code>1 &lt;= weight[i] &lt;= 10<sup>5</sup></code></li>
44+
</ul>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// shortest path from src1 and src2 to a 'meeting point' and SP from dest to any 'meeting point'
2+
3+
class Solution {
4+
public:
5+
long long minimumWeight(int n, vector<vector<int>>& edges, int src1, int src2, int dest) {
6+
vector<vector<pair<int, int>>> graph(n);
7+
for (auto& e : edges) graph[e[0]].push_back({e[1], e[2]});
8+
9+
auto bfs = [&](int start) {
10+
vector<long long> costs(n, 1e18);
11+
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
12+
pq.push({0, start});
13+
costs[start] = 0;
14+
while (!pq.empty()) {
15+
auto [cost, node] = pq.top();
16+
pq.pop();
17+
18+
if (cost > costs[node]) continue;
19+
20+
for (auto& neigh : graph[node]) {
21+
int newNode = neigh.first;
22+
long long newCost = cost + neigh.second;
23+
24+
if (newCost < costs[newNode]) {
25+
costs[newNode] = newCost;
26+
pq.push({newCost, newNode});
27+
}
28+
}
29+
}
30+
return costs;
31+
};
32+
33+
vector<long long> src1Costs = bfs(src1);
34+
vector<long long> src2Costs = bfs(src2);
35+
36+
vector<vector<pair<int, int>>> graphR(n);
37+
for (int i=0; i<n; i++) {
38+
for (auto& e : graph[i]) {
39+
graphR[e.first].push_back({i, e.second});
40+
}
41+
}
42+
graph = graphR;
43+
44+
vector<long long> destCosts = bfs(dest);
45+
46+
long long ans = 1e18;
47+
for (int i=0; i<n; i++) ans = min(ans, src1Costs[i] + src2Costs[i] + destCosts[i]);
48+
49+
return ans < 1e18 ? ans : -1;
50+
}
51+
};

0 commit comments

Comments
 (0)