Go: package main

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

const (
	white int = iota
	gray
	black
)

type node struct {
	id     int
	parent int
	edge   []int
	weight []int
	color  int
}

type graph struct {
	nodes    []node
	distance []int
}

var infty = 1 << 32
var null = -1

func constructgraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n := nextInt(sc)

	g := &graph{make([]node, n), make([]int, n)}

	for i := 0; i < n; i++ {
		id := nextInt(sc)
		edges := nextInt(sc)
		n := node{}
		n.id = id
		n.edge = make([]int, edges)
		n.weight = make([]int, edges)
		n.color = white
		for j := 0; j < edges; j++ {
			to := nextInt(sc)
			w := nextInt(sc)
			n.edge[j] = to
			n.weight[j] = w
		}
		g.nodes[id] = n
	}
	return g
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func (g *graph) preparedijkstra(s int) {
	for i := 0; i < len(g.nodes); i++ {
		g.nodes[i].color = white
		g.nodes[i].parent = null
		g.distance[i] = infty
	}
	g.distance[s] = 0
}

func (g *graph) dijkstra(s int) []int {
	g.preparedijkstra(s)

	for {
		var u int
		mincost := infty
		for i, d := range g.distance {
			if g.nodes[i].color != black && mincost > d {
				mincost = d
				u = i
			}
		}

		if mincost == infty {
			break
		}

		g.nodes[u].color = black

		for i, e := range g.nodes[u].edge {
			if g.nodes[e].color != black {
				if g.distance[e] > g.distance[u]+g.nodes[u].weight[i] {
					g.distance[e] = g.distance[u] + g.nodes[u].weight[i]
					g.nodes[e].parent = u
					g.nodes[e].color = gray
				}
			}
		}
	}
	return g.distance
}

func printShortestPath(d []int) {
	for i, v := range d {
		fmt.Printf("%d %d\n", i, v)
	}
}

func main() {
	g := constructgraph()
	d := g.dijkstra(0)
	printShortestPath(d)
}

Python: from collections import namedtuple
import heapq

Edge = namedtuple('Edge', ('fr', 'to', 'cost'))
inf = int(1e9)


def bellman_ford(n, start, edges):
    dists = [inf for _ in range(n)]
    dists[start] = 0
    while True:
        updated = False
        for e in edges:
            if dists[e.fr] != inf and dists[e.fr] + e.cost < dists[e.to]:
                updated = True
                dists[e.to] = dists[e.fr] + e.cost

        if not updated:
            break

    return dists


def dijkstra(n, costs):
    dists = [inf for _ in range(n)]
    dists[0] = 0

    queue = []
    for (i, d) in enumerate(dists):
        heapq.heappush(queue, (d, i))

    while len(queue) > 0:
        d, i = heapq.heappop(queue)
        if d > dists[i]:
            continue

        for v in range(n):
            if v == i or costs[i][v] == inf:
                continue

            if dists[i] + costs[i][v] < dists[v]:
                dists[v] = dists[i] + costs[i][v]
                heapq.heappush(queue, (dists[v], v))

    return dists


def warshall_floyd(n, costs):
    for i in range(n):
        print(costs[i])

    dists = [[costs[i][j] for j in range(n)] for i in range(n)]

    for k in range(n):
        for i in range(n):
            for j in range(n):
                dists[i][j] = min(dists[i][j], dists[i][k] + dists[k][j])

    return dists


n = int(input())

m = [[inf if i != j else 0 for i in range(n)] for j in range(n)]
es = []
for i in range(n):
    fs = list(map(int, input().split()))
    ne = fs[1]
    for j in range(ne):
        v = fs[2+2*j]
        c = fs[2+2*j+1]
        m[i][v] = c
        es.append(Edge(i, v, c))


# d = bellman_ford(n, 0, es)
# d = dijkstra(n, m)
d = warshall_floyd(n, m)
for (i, di) in enumerate(d[0]):
    print(i, di)

Python: #coding: utf-8

inf = 1000000000


def dijkstra(s):
    d = [inf for i in range(n)]
    color = ["white" for i in range(n)]
    p = [-1 for i in range(n)]
    d[s] = 0

    while True:
        mincost = inf
        for i in range(n):
            if color[i] != "black" and d[i] < mincost:
                mincost = d[i]
                u = i
        if mincost == inf:
            break

        color[u] = "black"

        for v in range(n):
            if color[v] != "black" and mtrx[u][v] != False:
                if d[u] + mtrx[u][v] < d[v]:
                    d[v] = d[u] + mtrx[u][v]
                    p[v] = u
                    color[v] = "gray"
    return d

n = int(input())
mtrx = [[False for j in range(n)] for i in range(n)]
for i in range(n):
    data = list(map(int,input().split()))
    k = data[1]
    for num in range(k):
        mtrx[i][data[2+2*num]] = data[2+2*num+1]

d = dijkstra(0)

for i, num in enumerate(d):
    print(i,num)

Python: # ALDS1_12_V Single Source Shortest Path I
import sys

n = int(input())
G = [[float('inf')] * n for _ in range(n)]
for i in range(n):
    ukvc = list(map(int, sys.stdin.readline().strip().split()))
    u = ukvc[0]
    k = ukvc[1]

    for j in range(k):
        G[u][ukvc[2 + 2 * j]] = ukvc[3 + 2 * j]

p = [n] * n  # 親を保存するリスト　親ノードとしてあり得ない n を初期リストとする
c = ['white'] * n # 最小経路木への接続チェック　white 未接続　black 接続済
d = [float('inf')] * n  # ノード i までの最小経路を保存するリスト
d[0] = 0  # 根への距離は0とする
p[0] = -1  # 根の親は -1 とする
u = 0  # 次のループで最小経路木につなげるノード

while 'white' in c:
    c[u] = 'black'

    v = None
    minpath = float('inf')
    for i in range(n):
        d[i] = min(d[i], d[u] + G[u][i])
        if (d[i] < minpath) & (c[i] == 'white'):
            minpath = d[i]
            v = i
            p[i] = u

    if v is None:
        break

    u = v

for i in range(n):
    print('{} {}'.format(i, d[i]))

Python: def dijkstra(n,s):
    inf=float("inf")
    color=["white" for i in range(n)]
    D=[inf for i in range(n)]
    P=[0 for i in range(n)]
    M=[[inf for i in range(n)] for j in range(n)]

    for i in range(n):
        weight=list(map(int,input().split()))[2:]
        for j in range(len(weight)//2):
            M[i][weight[2*j]]=weight[2*j+1]

    D[s]=0
    P[s]=-1

    while 1:
        cost,u=inf,0
        for i in range(n):
            if color[i]!="black" and D[i]<cost:
                cost=D[i]
                u=i

        if cost==inf:break

        color[u]="black"

        for i in range(n):
            if color[i]!="black" and M[u][i]!=inf:
                if D[u]+M[u][i]<D[i]:
                    D[i]=D[u]+M[u][i]
                    color[i]="gray"
                    P[i]=u

    return D


n=int(input())
distant=dijkstra(n,0)
for i in range(n):
    print(i,distant[i])

Python: def dijkstra():
    d[0] = 0
    color[0] = GRAY

    while True:
        minv = INF
        u = -1
        for i in range(N):
            if minv > d[i] and color[i] != BLACK:
                u = i
                minv = d[i]
        if u == -1:
            break
        color[u] = BLACK
        for i in range(N):
            if color[i] != BLACK and M[u][i] != INF:
                if d[i] > d[u] + M[u][i]:
                    d[i] = d[u] + M[u][i]
                    color[i] = GRAY

INF = 10 ** 18
WHITH = 0
GRAY = 1
BLACK = 2
N = int(input())
M = [[INF for _ in range(N)] for _ in range(N)]
d = [INF] * N
color = [WHITH] * N

for i in range(N):
    lst = list(map(int, input().split()))
    for j in range(lst[1]):
        M[lst[0]][lst[2 + (j * 2)]] = lst[3 + (j * 2)]

dijkstra()
for i in range(N):
    v = d[i]
    if v == INF:
        v = -1
    print(i, v)