Minimum Cost to Buy Apples - Problem

You're a traveling merchant looking to buy apples from various cities and return home! 🍎

Given n cities numbered from 1 to n, you have a network of bidirectional roads connecting them. Each road has a travel cost, and each city sells apples at different prices. Here's the twist: after buying an apple, all road costs get multiplied by factor k for your return journey!

Your goal: For each possible starting city, find the minimum total cost to:

  1. Travel to any city to buy exactly one apple
  2. Return to your starting city (with increased road costs)

Input:

  • n cities and roads array where roads[i] = [a, b, cost]
  • appleCost array where appleCost[i-1] is the cost of buying an apple from city i
  • Factor k that multiplies return journey costs

Output: Array where answer[i-1] is the minimum total cost starting from city i

Input & Output

example_1.py — Basic Triangle
$ Input: n = 3, roads = [[1,2,5],[2,3,2],[1,3,3]], appleCost = [1,2,3], k = 2
Output: [1, 2, 3]
💡 Note: From city 1: Best is to buy from city 1 itself (0+1+0=1). From city 2: Best is to buy from city 2 itself (0+2+0=2). From city 3: Best is to buy from city 3 itself (0+3+0=3).
example_2.py — Linear Path
$ Input: n = 2, roads = [[1,2,10]], appleCost = [5,1], k = 3
Output: [5, 1]
💡 Note: From city 1: Buy from city 1 (0+5+0=5) or buy from city 2 (10+1+10*3=41). Min = 5. From city 2: Buy from city 2 (0+1+0=1) or buy from city 1 (10+5+10*3=45). Min = 1.
example_3.py — Single City
$ Input: n = 1, roads = [], appleCost = [10], k = 5
Output: [10]
💡 Note: Only one city, must buy apple there with no travel cost.

Constraints

  • 1 ≤ n ≤ 1000
  • 0 ≤ roads.length ≤ min(n × (n - 1) / 2, 2000)
  • roads[i].length == 3
  • 1 ≤ ai, bi ≤ n
  • ai ≠ bi
  • 1 ≤ costi ≤ 1000
  • appleCost.length == n
  • 1 ≤ appleCost[i] ≤ 1000
  • 1 ≤ k ≤ 10
  • There are no repeated edges

Visualization

Tap to expand
Minimum Cost to Buy Apples INPUT 1 2 3 cost=5 cost=3 cost=2 Apple Costs: $1 $2 $3 City 1 City 2 City 3 n=3, k=2 Return cost = k * travel roads=[[1,2,5],[2,3,2],[1,3,3]] ALGORITHM STEPS 1 Modified Dijkstra Run from each city 2 Cost Formula dist*(1+k) + appleCost 3 Track Minimum For each starting city 4 Build Result Store min cost per city Example: Start City 1 Buy at 1: 0*(1+2)+1 = 1 Buy at 2: 5*(1+2)+2 = 17 Buy at 3: 3*(1+2)+3 = 12 BUT via 2--3: 5+2=7, 7*(1+2)+3=24 (worse) Min for City 1: 8 (via 3) FINAL RESULT Minimum Costs: City 1 Start Cost: 8 City 2 Start Cost: 8 City 3 Start Cost: 10 Output Array: [8, 8, 10] OK - Verified Optimal paths found Key Insight: Total cost = (going distance + return distance) + apple price = dist * (1 + k) + appleCost[city] Use modified Dijkstra: instead of tracking just distance, track minimum total cost including apple purchase. The return multiplier k is applied uniformly, so shortest path there = shortest path back (same route). TutorialsPoint - Minimum Cost to Buy Apples | Optimal Solution
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 23 Apple 18
28.4K Views
Medium-High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen