What is stored in heap structure in the following example?
You would put triplets into your heap, storing

(distance, i, j)


where distance is the distance, i and j are the indices.

When you "pop" the topmost (smallest distance) element from the heap using the appropriate algorithm, the second smallest will now become the top.

Python has a module heapq for building min-heaps. Simply add tuples with heapq.heappush, and remove the smallest with heapq.heappop. To convert an array into a heap, use heapq.heapify (faster than calling heappush for every element).

Note that putting n^2 values into a heap yields runtime O(n^2 log n^2) which is fairly expensive.