import numpy as np
def read_graph():
    n = int(input("Enter the number of nodes"))
    adj=np.zeros((n,n))   
    for i in range(n):
        for j in range(n):
            if (i !=j):
                adj[i][j] = int(input("Enter the weight of edge from node " + str(i) + " to "+str(j)))
    for i in range(n):
        for j in range(n):
            if (adj[i][j]==0):
                adj[i][j] = 9999
    return n, adj


def dfs_rec(visited,i):
    visited[i]=1 
    for j in range(n):
        if (adj[i][j] < 9999):
            if (visited[j] ==0):
                dfs_rec(visited,j)
    v_stack.append(i)   

def top_order():
    visited=np.zeros(n)
    for i in range(n):
        if (visited[i] ==0):
            dfs_rec(visited,i)
    while (len(v_stack) > 0):
        t_order.append(v_stack.pop())   

def s_dis(s):
    dist=np.array(n*[9999])
    dist[s]=0
    for j in t_order:
        for k in range(n):  # check all the edges going from j.
            if (adj[j][k] <9999) and (dist[j]+adj[j][k] < dist[k]):
                dist[k]=dist[j]+adj[j][k]
    print(dist)

        

t_order =[]
v_stack =[]
top_order()   
s = int(input("Enter start node"))
s_dis(s)      
dist=np.array(5*[9999])
dist[2]=5
for j in dist:
    print(j)
