Go: package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := make(map[int]int)
	q[0] = 0
	visited := make(map[int]struct{})
	out := make([]int, n)

	for len(q) > 0 {
		u := top(q)
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			if d, ok := q[uu]; !ok || d > q[u]+c {
				q[uu] = q[u] + c
			}
		}

		out[u] = q[u]
		delete(q, u)
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, d)
	}
}

func top(q map[int]int) int {
	minu := -1
	min := -1
	for u, d := range q {
		if min < 0 || d < min {
			minu = u
			min = d
		}
	}

	return minu
}

Python: WHITE = 0
GRAY = 1
BLACK = 2
INFTY = 10**13
def dijkstra(s):
    color = [WHITE for i in range(N)]
    d = [INFTY for i in range(N)]
    p = [-1 for i in range(N)]
    d[s] = 0

    while True:
        mincost = INFTY
        for i in range(N):
            if color[i] != BLACK and d[i] < mincost:
                mincost = d[i]
                u = i
        if mincost == INFTY:
            break
        
        color[u] = BLACK

        for v in range(N):
            if color[v] != BLACK and M[u][v] != -1:
                if d[u] + M[u][v] < d[v]:
                    d[v] = d[u] + M[u][v]
                    p[v] = u
                    color[v] = GRAY
    return d

N = int(input())
M = [[-1 for j in range(N)] for i in range(N)]
for i in range(N):
    ukvc = list(map(int, input().split()))
    u, k, vc = ukvc[0], ukvc[1], ukvc[2:]
    for v, c in zip(vc[0::2], vc[1::2]):
        M[u][v] = c

d = dijkstra(0)

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

Python: import sys
def dijkstra(n, a):
	v = [False]*n
	v[0] = True
	w = [sys.maxsize] * n
	w[0] = 0
	while True:
		m = -1
		mini = sys.maxsize
		for i in range(n):
			for j in range(n):
				c = a[i][j]
				if v[i] and not v[j] and c != 0:
					w[j] = min(w[j], w[i]+c)
					if mini > w[j]:
						mini = w[j]
						m = j
		if m == -1: break
		v[m] = True
	
	for i in range(n):
		print(str(i) + ' ' + str(w[i]))

n = int(input())
a = [[0 for x in range(n)] for x in range(n)]
for i in range(n):
	t = list(map(int,input().split()))
	u = t[0]
	k = t[1]
	for j in range(k):
		v = t[(j*2)+2]
		c = t[(j*2)+3]
		a[u][v] = c
dijkstra(n, a)

Python: import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array

# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()

    # N個の頂点を持つ有向グラフのデータを生成
    # 辺が存在しない場合には値をinfinityとする
    graph = [[INF] * N for _ in range(N)]
    for _ in range(N):
        v1, k, *vertices = il()
        for idx in range(0, k * 2, 2):
            v2, c = vertices[idx:idx + 2]
            graph[v1][v2] = c

    # ダイクストラ法
    dist = [INF] * N  # 各頂点までの距離を保持する配列
    dist[0] = 0       # 開始始点の頂点は距離をゼロとする
    visited = [0] * N # 各頂点の訪問状態を保持する配列[0: 未訪問, 1: 訪問済み]
    while True:
        min_cost, u = INF, INF
        # 起点となる頂点の探索
        for i in range(N):
            if min_cost > dist[i] and visited[i] != 1:
                u = i
                min_cost = dist[i]

        # 起点となる頂点が存在しない場合には処理を終了
        if u == INF: break
        visited[u] = 1  # 起点を訪問済みにする

        # 起点から次の頂点までの距離を更新
        for v in range(N):
            if graph[u][v] != INF and visited[v] != 1:
                if dist[v] > dist[u] + graph[u][v]:
                    dist[v] = dist[u] + graph[u][v]

    # 回答
    for n in range(N):
        print(n, dist[n])


if __name__ == '__main__':
    main()

Python: import sys

def dijkstra(v_count: int, edges: list, start: int,
             *, adj_matrix: bool = False, unreachable=float("inf")) -> list:
    """ ダイクストラ法
    :param v_count: 頂点数
    :param edges: 辺のリスト(隣接リストor隣接行列)
    :param start: スタートする頂点
    :param adj_matrix: edgesに渡したリストが隣接行列ならTrue
    :param unreachable: 到達不能を表すコスト値
                        隣接行列の辺の有無の判定および返すリストの初期値に使用
    """
    from heapq import heappush, heappop

    vertices = [unreachable] * v_count
    vertices[start] = 0
    q, rem = [(0, start)], v_count - 1

    while q and rem:
        cost, v = heappop(q)
        if vertices[v] < cost:
            continue
        rem -= 1

        dests = (filter(lambda x: x[1] != unreachable, enumerate(edges[v]))
                 if adj_matrix else edges[v])

        for dest, _cost in dests:
            newcost = cost + _cost
            if vertices[dest] > newcost:
                vertices[dest] = newcost
                heappush(q, (newcost, dest))

    return vertices

n = int(input())
edges = [[] for _ in [0]*n]
for a in (tuple(map(int, l.split())) for l in sys.stdin):
    edges[a[0]] = tuple(zip(a[2::2], a[3::2]))
vertices = dijkstra(n, edges, 0)
for i, n in enumerate(vertices):
    print(i, n)

Python: n = int(input())
INF = float("inf")
M = [[INF for _ in range(n)] for _ in range(n)]
for _ in range(n):
    a,*b = map(int,input().split())
    if b[0] != 0:
        c = b[1:]
        for i in range(len(c)//2):
            k,w = c[2*i],c[2*i+1]
            M[a][k] = w
searched = [0]*n
d = [INF]*n
p = [None]*n
d[0] = 0
p[0] = -1
while 1:
    mc = INF
    for i in range(n):
        if not searched[i] and d[i] < mc:
            mc = d[i]
            u = i
    if mc == INF:
        break
    searched[u] = 1
    for v in range(n):
        if not searched[v] and M[u][v] != INF:
            if d[u]+M[u][v] < d[v]:
                d[v] = d[u]+M[u][v]
                p[v] = u
for i in range(n):
    print(i,d[i])

Python: n = int(input())
G = [[-1 for i in range(n)] for j in range(n)]
d = [999999 for i in range(n)]
  
for i in range(n):
    v = input().split()
    for j in range(int(v[1])):
        G[int(v[0])][int(v[2+2*j])] = int(v[3+2*j])

flag = [0] * n
tn = [0]
d[0] = 0
for k in range(n):
    for i in range(n):
        if i in tn:
            for j in range(n):
                if G[i][j] != -1:
                    if d[j] > d[i] + G[i][j]:
                        d[j] = d[i] + G[i][j]
                    if j not in tn:
                        tn.append(j)
  
for i in range(n):
    print(str(i) + " " + str(d[i]))