Trains the technique from
LeetCode 3928Minimum Cost to Buy Apples IIThis 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 region has n depots numbered from 0, and depot i sells a crate for prices[i].
Each entry of roads is [u, v, cost, tax] for a two-way road. Driving that road outward costs cost, and driving it back costs cost * tax.
Starting from a depot, you drive to any depot, buy exactly one crate there, and drive home along the very same roads you came by. Buying at home is allowed and costs no driving at all.
Return, for each depot in turn, the least a round trip can cost.
Example 1
Each road is worth its cost times one more than its tax, so the three roads weigh 9, 8 and 8. From depot 0 the cheapest trip drives to depot 1 for 9 and buys at 9, coming to 18, which beats buying at home for 27.
Example 2
With no roads at all, every depot can only buy at home.
Example 3
Every depot sells at the same price and the roads are heavily taxed, so no trip beats buying at home.
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 min_cost(n: int, prices: list[int], roads: list[list[int]]) -> list[int]:public int[] minCost(int n, int[] prices, int[][] roads)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.