Minimum Cost to Buy Apples Problem
Description
LeetCode Problem 2473.
You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads, where roads[i] = [a_i, b_i, cost_i] indicates that there is a bidirectional road between cities a_i and b_i with a cost of traveling equal to cost_i.
You can buy apples in any city you want, but some cities have different costs to buy apples. You are given the 1-based array appleCost where appleCost[i] is the cost of buying one apple from city i.
You start at some city, traverse through various roads, and eventually buy exactly one apple from any city. After you buy that apple, you have to return back to the city you started at, but now the cost of all the roads will be multiplied by a given factor k.
Given the integer k, return a 1-based array answer of size n where answer[i] is the minimum total cost to buy an apple if you start at city i.
Example 1:

Input: n = 4, roads = [[1,2,4],[2,3,2],[2,4,5],[3,4,1],[1,3,4]], appleCost = [56,42,102,301], k = 2
Output: [54,42,48,51]
Explanation: The minimum cost for each starting city is the following:
- Starting at city 1: You take the path 1 -> 2, buy an apple at city 2, and finally take the path 2 -> 1. The total cost is 4 + 42 + 4 * 2 = 54.
- Starting at city 2: You directly buy an apple at city 2. The total cost is 42.
- Starting at city 3: You take the path 3 -> 2, buy an apple at city 2, and finally take the path 2 -> 3. The total cost is 2 + 42 + 2 * 2 = 48.
- Starting at city 4: You take the path 4 -> 3 -> 2 then you buy at city 2, and finally take the path 2 -> 3 -> 4. The total cost is 1 + 2 + 42 + 1 * 2 + 2 * 2 = 51.
Example 2:

Input: n = 3, roads = [[1,2,5],[2,3,1],[3,1,2]], appleCost = [2,3,1], k = 3
Output: [2,3,1]
Explanation: It is always optimal to buy the apple in the starting city.
Constraints:
2 <= n <= 10001 <= roads.length <= 20001 <= a_i, b_i <= na_i != b_i1 <= cost_i <= 10^5appleCost.length == n1 <= appleCost[i] <= 10^51 <= k <= 100- There are no repeated edges.
Solution
Approach: Dijkstra from Every City
For a start city i, if the apple is bought in city j and the shortest road distance between them is d, the total cost is appleCost[j] + d * (k + 1): one trip there at normal price and one trip back multiplied by k.
So run Dijkstra (with a heap) from each city, and take the minimum of appleCost[u] + dist[u] * (k + 1) over all cities u reached. The start city itself has distance 0, so buying at home is covered.
Complexity: O(n * m log m) time and O(n + m) space, where n is the number of cities and m the number of roads.
from collections import defaultdict
from heapq import heappop, heappush
from math import inf
class Solution:
def minCost(
self, n: int, roads: List[List[int]], appleCost: List[int], k: int
) -> List[int]:
def dijkstra(i):
q = [(0, i)]
dist = [inf] * n
dist[i] = 0
ans = inf
while q:
d, u = heappop(q)
ans = min(ans, appleCost[u] + d * (k + 1))
for v, w in g[u]:
if dist[v] > dist[u] + w:
dist[v] = dist[u] + w
heappush(q, (dist[v], v))
return ans
g = defaultdict(list)
for a, b, c in roads:
a, b = a - 1, b - 1
g[a].append((b, c))
g[b].append((a, c))
return [dijkstra(i) for i in range(n)]