Trains the technique from
LeetCode 2203Minimum Weighted Subgraph With the Required PathsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A network has n yards numbered 0 through n - 1, joined by the one-way roads roads, where roads[i] = [a, b, w] runs from yard a to yard b and costs w to lay.
Choose a set of roads to lay so that the depot at depot can be reached from from1 by following laid roads, and likewise from from2. A laid road is paid for once however many journeys use it.
Return the smallest total cost, or -1 when no set of roads manages it. The three named yards are all different.
Example 1
Both starting yards can run straight to the depot for ten apiece, twenty in all. Joining at yard 2 instead costs one from each starting yard and one more from there to the depot, three altogether, since the last stretch is paid for once.
Example 2
The two journeys share nothing, so they meet only at the depot itself, and the two roads there cost five and seven.
Example 3
There are no roads at all, so the depot cannot be reached from anywhere.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def minimum_weight(n: int, roads: list[list[int]], from1: int, from2: int, depot: int) -> int:public long minimumWeight(int n, int[][] roads, int from1, int from2, int depot)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.