LeetCode 题解工作台
包含要求路径的最小带权子图
给你一个整数 n ,它表示一个 带权有向 图的节点数,节点编号为 0 到 n - 1 。 同时给你一个二维整数数组 edges ,其中 edges[i] = [from i , to i , weight i ] ,表示从 from i 到 to i 有一条边权为 weight i 的 有向 边。 …
2
题型
2
代码语言
3
相关题
当前训练重点
困难 · 图
答案摘要
最短路问题。 我们假设从 出发到 的一条最短路径为 ,从 出发到 的一条最短路径为 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 图 题型思路
题目描述
给你一个整数 n ,它表示一个 带权有向 图的节点数,节点编号为 0 到 n - 1 。
同时给你一个二维整数数组 edges ,其中 edges[i] = [fromi, toi, weighti] ,表示从 fromi 到 toi 有一条边权为 weighti 的 有向 边。
最后,给你三个 互不相同 的整数 src1 ,src2 和 dest ,表示图中三个不同的点。
请你从图中选出一个 边权和最小 的子图,使得从 src1 和 src2 出发,在这个子图中,都 可以 到达 dest 。如果这样的子图不存在,请返回 -1 。
子图 中的点和边都应该属于原图的一部分。子图的边权和定义为它所包含的所有边的权值之和。
示例 1:

输入: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 输出:9 解释: 上图为输入的图。 蓝色边为最优子图之一。 注意,子图 [[1,0,3],[0,5,6]] 也能得到最优解,但无法在满足所有限制的前提下,得到更优解。
示例 2:

输入:n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2 输出:-1 解释: 上图为输入的图。 可以看到,不存在从节点 1 到节点 2 的路径,所以不存在任何子图满足所有限制。
提示:
3 <= n <= 1050 <= edges.length <= 105edges[i].length == 30 <= fromi, toi, src1, src2, dest <= n - 1fromi != toisrc1,src2和dest两两不同。1 <= weight[i] <= 105
解题思路
方法一:枚举三条最短路的交汇点
最短路问题。
我们假设从 出发到 的一条最短路径为 ,从 出发到 的一条最短路径为 。
, 两条路径一定存在着公共点 ,因为 一定是其中一个公共点。那么问题可以转换为求以下三条路径和的最小值:
- 从 到 的最短路
- 从 到 的最短路
- 从 到 的最短路(这里我们可以将原图的所有边反向,然后转换为从 到 的最短路)
我们进行三次 Dijkstra 算法,就可以求出 , , 到其他点的最短路径。
公共点可以有多个,因此我们在 范围内枚举公共点 ,找出边权之和最小的值即可。
时间复杂度 ,其中 m 表示数组 的长度。
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
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidates should discuss how to ensure that both paths (src1 to dest and src2 to dest) are captured in the subgraph.
- question_mark
Watch for optimization techniques in graph filtering, focusing on minimizing unnecessary edges.
- question_mark
Check if the candidate recognizes edge cases, such as no valid paths between the required nodes.
常见陷阱
外企场景- error
Failing to consider edge cases where no path exists from src1 or src2 to dest.
- error
Choosing the wrong shortest path algorithm for the given constraints.
- error
Not properly minimizing the subgraph or including unnecessary edges in the solution.
进阶变体
外企场景- arrow_right_alt
Consider handling graphs with negative edge weights by using Bellman-Ford instead of Dijkstra’s algorithm.
- arrow_right_alt
Challenge candidates with larger graphs, such as increasing the number of nodes and edges, to test algorithm efficiency.
- arrow_right_alt
Introduce dynamic graph changes during the process to test how the solution adapts to updates.