15% off all plans & roadmaps Use code
See plansLeetCode 2203 · Solution & editorial
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.
Minimum Weighted Subgraph With the Required Paths is a Graph Traversal Patterns (DFS & BFS) problem, in the Shortest Path (Dijkstra's Algorithm) subpattern. The pattern covers DFS/BFS traversal, topological sort, shortest paths, Union-Find, and MST
Master Dijkstra's algorithm for finding shortest paths in weighted graphs with non-negative edges.
The reference solution runs in O((n + m) * log n) time and O(n + m) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Minimum Weighted Subgraph With the Required Paths is a Graph Traversal Patterns (DFS & BFS) problem, in the Shortest Path (Dijkstra's Algorithm) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.