Go: package main

import (
	"fmt"
	"container/heap"
)

type Node struct {
	Id int
	Dis int
}

type PriorityQueue []*Node

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].Dis < pq[j].Dis
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PriorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.(*Node))
}

func (pq *PriorityQueue) Pop() interface{} {
	n := len(*pq)
	pop := (*pq)[n-1]
	*pq = (*pq)[0:n-1]
	return pop
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	c := make([][]int, n)
	pq := make(PriorityQueue, 0, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		c[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
			fmt.Scan(&c[u][j])
		}

		if u == 0 {
			d[u] = 0
		} else {
			d[u] = 1 << 30
		}
		pq.Push(&Node{u, d[u]})
	}
	heap.Init(&pq)

	for pq.Len() > 0 {
		node := heap.Pop(&pq).(*Node)
		for i := 0; i < len(g[node.Id]); i++ {
			nextId := g[node.Id][i]
			nextDis := node.Dis + c[node.Id][i]
			if nextDis < d[nextId] {
				d[nextId] = nextDis
				heap.Push(&pq, &Node{nextId, nextDis})
			}
		}
	}

	for i := 0; i < n; i++ {
		fmt.Println(i, d[i])
	}
}

Python: trial = int(input())
graph = []
for t in range(trial):
    graph.append([int(n) for n in input().split(" ")])
root = []
ans = []
stack = [[0,0]]
cnt = 0
while len(stack) > 0:
    index,out = stack[0][0],stack[0][1]
    for get in range(graph[index][1]):
        if graph[index][get*2 + 2] not in root:
            stack.append([graph[index][get*2 +2],graph[index][get*2 +3] + out ])
    if stack[0][0] not in root:
        root.append(stack[0][0])
        ans.append([str(stack[0][0]),str(stack[0][1])])
    stack.pop(0)
    stack = sorted(stack,key=lambda x:x[1])
ans = sorted(ans,key=lambda x:x[0])
for a in ans:
    print(" ".join(a))

Python: N = int(input())

#M[u][v]:隣接行列
M=[]
for n in range(N):
    input_temp=list(map(int,input().split()))
    M_temp=[-1]*N
    for k in range(0,2*input_temp[1],2):
        M_temp[input_temp[2+k]]=input_temp[2+k+1]
    M.append(M_temp)
    
#color:各頂点の訪問状態を示す
color=['white']*N

#d:TとV-Tを繋ぐ辺の中で重みが最小の辺の重みの値を保持
d=[10**10]*N

#p[v-1]:MSTにおけるvの親を保持
p=[-1]*N

#初期値
d[0] = 0
p[0] = -1

# ans=0
while True:
#     ans+=1
    mincost=10**10
    for i in range(N):
        if color[i] != 'BLACK' and d[i] < mincost:
            mincost = d[i]
            u = i
#     print(u,mincost)       
    if mincost == 10**10:
        break
    
    color[u] = 'BLACK'
    
    for v in range(N):
        if color[v] != 'BLACK' and M[u][v]!=-1:
#             print(u,v,color[v],M[u][v],d[v])
            if d[u]+M[u][v] < d[v]:
                d[v] = d[u] + M[u][v]
                p[v] = u
                color[v] = 'GRAY'
    # print(color,mincost,d,p)
    
for d_ in enumerate(d):
    print(d_[0],d_[1])

Python: N=int(input())
rinsetu=[[1000000 for i in range(N)] for j in range(N)]
d=[1000000]*(N)
color=["white"]*N
for i in range(N):
    a=list(map(int,input().split()))
    for j in range(a[1]):
        j1=j*2+2
        j2=j*2+3
        rinsetu[i][a[j1]]=a[j2]
def hunterhunter():
    d[0]=0
    while(True):
        mincost=1000000
        for i in range(N):
            if color[i]=="white" and mincost>d[i]:
                mincost=d[i]
                u=i
        if mincost==1000000:break
        color[u]="black"
        for i in range(N):
            if color[i]=="white" and rinsetu[u][i]!=1000000:
                if rinsetu[u][i]+d[u]<d[i]:
                    d[i]=rinsetu[u][i]+d[u]
hunterhunter()
for i,cost in enumerate(d):
    print(i,cost)

Python: n = int(raw_input())
G = [[-1] * n] * n
d = [0] * n
 
for i in range(n):
    v = map(int, raw_input().split())
    for j in range(v[1]):
        G[v[0]][v[2+2*j]] = int(v[3+2*j])
 
flag = [0] * n
tn = [0]
r = 0
while True:
    for i in range(n):
        if flag[i] == 0 and i in tn:
            flag[i] = 1
            d[i] = r
            for j in range(n):
                if G[i][j] != -1 and flag[j] == 0:
                    tn.append(j)
    if len(tn) == n:
        break
    r += 1
 
for i in range(1, n+1):
    print(str(i) + " " + str(d[i]))

Python: from queue import PriorityQueue

def sssp(adj_list):
    n = len(adj_list)
    d = [None]*n
    pq = PriorityQueue()
    pq.put((0, 0)) # d, v

    while(not pq.empty()):
        d_v, v = pq.get()
        if d[v] is not None:
            continue
        d[v] = d_v
        for u, c_vu in adj_list[v]:
            pq.put((d[v]+c_vu, u))

    return d


if __name__ == '__main__':
    n = int(input())
    adj_list = []
    for _ in range(n):
        adj_list.append([])

    for _ in range(n):
        l = list(map(int, input().rstrip().split(' ')))
        u = l[0]
        k = l[1]
        adj_list_for_u = []
        for v, c in zip(l[2::2], l[3::2]):
            adj_list_for_u.append((v,c))
        adj_list[u] = adj_list_for_u

    d = sssp(adj_list)
    for u, d_u in zip(range(n), d):
        print (u, d_u)

Python: from collections import deque

n = int(input())
g = []
d = [10**10] * n
d[0] = 0

for i in range(n):
    temp = list(map(int, input().split()))
    k = temp[1]
    temp = temp[2:]
    adj = []
    for j in range(k):
        v, c = temp[j*2], temp[j*2+1]
        adj.append([v, c])
    g.append(adj)

# for i in range(n):
#     print(g[i])

q = deque()
q.append((0, g[0]))
done = set()

while q:
    node = q.popleft()
    prev = node[0]
    adj = node[1]
    # print('\nadj:', adj)

    for next in adj:
        v = next[0]
        w = next[1]
        # print('v:', v)
        # print('prev:', prev)

        # d[v] = min(d[v], d[prev] + w)
        if d[prev] + w < d[v]:
            d[v] = d[prev] + w
            # Need to add to Q regardless of it's in done or not,
            # because the shortest path was udpated.
            q.append((v, g[v]))
        if v not in done:
            done.add(v)
            q.append((v, g[v]))

# print('d:', d)

for i in range(len(d)):
    print(i, d[i])

Python: n = int(input())
edgeList = []
rootList = [-1 for i in range(n)]
parent = [-1 for i in range(n)]
lenList = [0 for i in range(n)]
sumLength = 0
for i in range(n):
    a = list(map(int, input().split()))
    for j in range(a[1]):
        edgeList.append([a[2*j+3], i, a[2*j+2]])


rootList = [-1 for i in range(n)]
parent = [0 for i in range(n)]
lenList = [float("inf") for i in range(n)]
lenList[0] = 0
def getRoot(x):
    r = rootList[x]
    if r < 0:
        rootList[x] = x
    elif r != x:
        rootList[x] = getRoot(r)
    return rootList[x]

flag = True
while flag:
    flag = False
    edgeList.sort(key=lambda x: (x[0] + lenList[x[1]]))
    for e in edgeList:    # weight:e[0], e[1]->e[2]
        if e[2] == 0:
            continue
        x = getRoot(e[1])
        y = getRoot(e[2])
        if x == 0 != y:
            parent[e[2]] = e[1]
            lenList[e[2]] = lenList[e[1]] + e[0]
            rootList[y] = 0
            flag = True
            break

for i, j in zip(range(n), lenList):
    print(i, j)

Python: n=int(input())
inf=10000000
p=[-1 for i in range(n)]
M=[[inf for j in range(n)] for i in range(n)]
d=[inf for i in range(n)]
color=["white" for i in range(n)]
for i in range(n):
    tmp=list(map(int,input().split()))
    root=tmp[0]
    k=tmp[1]
    for j in range(k):
        v=tmp[2*(j+1)]
        M[root][v]=tmp[2*(j+1)+1]

def dijkstra(s):
    d[s]=0
    while True:
        mincost=inf
        for i in range(n):
            if color[i] is not "black" and d[i]<mincost:
                mincost=d[i]
                u=i
        if mincost is inf:
            break
        color[u]="black"
        for j in range(n):
            if color[j] is not "black" and M[u][j] is not inf:
                if d[u]+M[u][j]<d[j]:
                    d[j]=d[u]+M[u][j]
                    color[j]="gray"
                    p[j]=u

dijkstra(0)
for i in range(n):
    print(i,d[i])