import heapq
import math

def path_print(p, destination):
    if p[destination] == destination:
        print(destination, end = " ")
    else:
        path_print(p, p[destination])
        print(destination, end = " ")
        

def dijkstra(graph, source):
    v = len(graph)
    d = [math.inf] * v
    p = [None] * v
    fop = [0] * v
    
    heap = []
    
    d[source] = 0
    p[source] = source
    
    heapq.heappush(heap, (d[source], source))
    
    while len(heap) > 0:
        distance, current_node = heapq.heappop(heap)
        
        if fop[current_node] == 1:
            continue
        
        fop[current_node] = 1
        
        for neighbor, cost in graph[current_node]:
            
            if fop[neighbor] == 1:
                continue
            
            if d[neighbor] > d[current_node] + cost:
                d[neighbor] = d[current_node] + cost
                heapq.heappush(heap, (d[neighbor], neighbor))
                p[neighbor] = current_node
    

    path_print(p, 2)

graph = {
    0: [(1, 2), (2, 10)],
    1: [(2, 4)],
    2: []
    }

dijkstra(graph, 0)

