Minimum Cost to Buy Apples II Problem
Description
LeetCode Problem 3928.
You are given an integer n and an integer array prices of length n, where prices[i] is the price of apples at shop i.
You are also given a 2D integer array roads, where roads[i] = [u_i, v_i, cost_i, tax_i] represents a bidirectional road:
u_iandv_iare the shops connected by the road.cost_iis the cost to travel the road without carrying apples.tax_iis the multiplier applied tocost_iwhen traveling with apples.
For each shop i, you can either:
- Buy apples locally at shop
iforprices[i]. - Travel empty to any shop
jusing any number of roads, buy apples forprices[j], and return to shopiwhile carrying apples, payingcost * taxon each road used for the return trip.
The forward path, where you travel empty, and the return path may be different.
Return an integer array ans of length n, where ans[i] is the minimum total cost to buy apples starting from shop i.
Example 1:
Input: n = 2, prices = [8,3], roads = [[0,1,1,2]]
Output: [6,3]
Explanation:

Shop i prices[i] Shop j prices[j] cost_i tax_i Travel cost Return cost Total Minimum
0 8 1 3 1 2 1 1 * 2 = 2 1 + 2 + 3 = 6 min(8, 6) = 6
1 3 0 8 1 2 1 1 * 2 = 2 1 + 2 + 8 = 11 min(3, 11) = 3
Thus, the answer is [6, 3].
Example 2:
Input: n = 3, prices = [9,4,6], roads = [[0,1,1,3],[1,2,4,2]]
Output: [8,4,6]
Explanation:

Shop i prices[i] Shop j prices[j] cost_i tax_i Travel cost Return cost Total Minimum
0 9 1 4 1 3 1 1 * 3 = 3 1 + 3 + 4 = 8 min(9, 8) = 8
1 4 2 6 4 2 4 4 * 2 = 8 4 + 8 + 6 = 18 min(4, 18) = 4
2 6 1 4 4 2 4 4 * 2 = 8 4 + 8 + 4 = 16 min(6, 16) = 6
Thus, the answer is [8, 4, 6].
Example 3:
Input: n = 3, prices = [10,11,1], roads = [[0,2,1,3],[1,2,3,4],[0,1,5,2]]
Output: [5,11,1]
Explanation:

Shop i prices[i] Shop j prices[j] cost_i tax_i Travel cost Return cost Total Minimum
0 10 2 1 1 3 1 1 * 3 = 3 1 + 3 + 1 = 5 min(10, 5) = 5
1 11 2 1 3 4 3 3 * 4 = 12 3 + 12 + 1 = 16 min(11, 16) = 11
2 1 0 10 1 3 1 1 * 3 = 3 1 + 3 + 10 = 14 min(1, 14) = 1
Thus, the answer is [5, 11, 1].
Constraints:
1 <= n <= 1000prices.length == n1 <= prices[i] <= 10^90 <= roads.length <= min(n × (n - 1) / 2, 2000)roads[i] = [u_i, v_i, cost_i, tax_i]0 <= u_i, v_i <= n - 1u_i != v_i1 <= cost_i <= 10^91 <= tax_i <= 100- There are no repeated edges.
Solution
Approach: Two Dijkstra Runs per Shop
For shop i, buying at shop j costs prices[j] plus the cheapest empty trip from i to j plus the cheapest trip back from j to i with apples. The two trips are independent, so each uses its own shortest path:
- the empty trip uses road weights
cost; - the return trip uses road weights
cost * tax.
Roads are bidirectional, so both distances can be computed by running Dijkstra from i. Then
ans[i] = min(prices[i], min over reachable j of (go[j] + back[j] + prices[j]))
where j = i gives prices[i] again. Use 64-bit integers: sums can reach about 1014.
Complexity: O(n (n + m) log n) time and O(n + m) extra space, where m is the number of roads.
import heapq
class Solution:
def minCost(self, n: int, prices: List[int], roads: List[List[int]]) -> List[int]:
g = [[] for _ in range(n)]
for u, v, cost, tax in roads:
g[u].append((v, cost, cost * tax))
g[v].append((u, cost, cost * tax))
def dijkstra(src: int, which: int) -> List[int]:
inf = float("inf")
dist = [inf] * n
dist[src] = 0
pq = [(0, src)]
while pq:
d, u = heapq.heappop(pq)
if d > dist[u]:
continue
for e in g[u]:
nd = d + e[which]
if nd < dist[e[0]]:
dist[e[0]] = nd
heapq.heappush(pq, (nd, e[0]))
return dist
ans = []
for i in range(n):
go = dijkstra(i, 1)
back = dijkstra(i, 2)
best = prices[i]
for j in range(n):
if go[j] != float("inf"):
best = min(best, go[j] + back[j] + prices[j])
ans.append(best)
return ans