My python code TLE’s on this question. I use the optimized version of dijkstra’s algorithm (heap) but it’s too slow. I need to make it about 1.5-2x faster, any tips?

Here’s the code:

```
"""
ID: solasky1
LANG: PYTHON3
TASK: butter
"""
import collections
import heapq
import math
with open('butter.in') as f:
C, V, E = map(int, f.readline().strip().split())
cows, weights = [], collections.defaultdict(dict)
cow_dists = []
for _ in range(C):
cows.append(int(f.readline().strip()))
for _ in range(E):
start, end, cost = map(int, f.readline().strip().split())
weights[start][end] = cost
weights[end][start] = cost
#dijkstra's algorithm
#calculates the shortest distance from every cow to every pasture
for cow in range(C):
heap = [(0, cows[cow])]
dist = {}
while heap:
time, start = heapq.heappop(heap)
if start not in dist:
dist[start] = time
for end in weights[start]:
heapq.heappush(heap, (dist[start] + weights[start][end], end))
cow_dists.append(dist)
#calculates minimum distance that every cow has to travel to a pasture
res = math.inf
for vertex in range(1, V + 1):
s = 0
for d in cow_dists:
s += d[vertex]
if s < res:
res = s
with open('butter.out', 'w') as f:
f.write(f'{res}\n')
```

Here’s the screenshot of the error.