-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2473.py
30 lines (21 loc) · 1.01 KB
/
m2473.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
def minCost(self, n: int, roads: List[List[int]], appleCost: List[int], k: int) -> List[int]:
travelCostMultiplier = k + 1
output = []
roadChoices = defaultdict(list)
for road in roads :
roadChoices[road[0]].append((road[1], road[2]))
roadChoices[road[1]].append((road[0], road[2]))
for i in range(1, n + 1) :
toVisit = [] # (totalRoadCost, targetIndx)
toVisit.append((0, i))
distances = [inf] * n
while toVisit :
cost, node = heapq.heappop(toVisit)
if distances[node - 1] != inf :
continue
distances[node - 1] = cost
for nextNode, additionalCost in roadChoices[node] :
heapq.heappush(toVisit, (cost + additionalCost, nextNode))
output.append(min([distances[x] * travelCostMultiplier + appleCost[x] for x in range(n)]))
return output