Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Node struct {
	cost int
	done bool
}

type Edge struct {
	to, cost int
}

const INF = 1 << 60

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	nodes := make([]Node, N)
	for i := 0; i < N; i++ {
		nodes[i].cost = INF
	}
	E := make([][]Edge, N)
	for i := 0; i < N; i++ {
		id, k := sc.nextInt(), sc.nextInt()
		for j := 0; j < k; j++ {
			to, cost := sc.nextInt(), sc.nextInt()
			E[id] = append(E[id], Edge{to, cost})
		}
	}
	nodes[0].cost = 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			n := nodes[i]
			if !n.done && n.cost < minCost {
				cur = i
				minCost = n.cost
			}
		}
		if minCost == INF {
			break
		}
		nodes[cur].done = true
		for i := 0; i < len(E[cur]); i++ {
			to, cost := E[cur][i].to, E[cur][i].cost+nodes[cur].cost
			if !nodes[to].done && cost < nodes[to].cost {
				nodes[to].cost = cost
			}
		}
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i, nodes[i].cost)
	}
	wtr.Flush()
}

Python: import sys
from collections import defaultdict


def main():
    n = int(input())
    lines = sys.stdin.readlines()
    vertex_weights = defaultdict(list)
    for i in range(n):
        u, k, *vtx_wght = map(int, lines[i].split())
        for v_i in range(k):
            vertex_weights[u].append((vtx_wght[2*v_i], vtx_wght[2*v_i+1]))

    dist_from_0 = {}
    vertices = [(0, 0)]
    while len(dist_from_0) != n:
        vertices.sort(key=lambda x: x[1], reverse=True)
        vtx, vtx_dist = vertices.pop()
        dist_from_0[vtx] = vtx_dist

        neighbor_vtx = vertex_weights[vtx]

        for n_vtx, n_dist in neighbor_vtx:
            if n_vtx not in dist_from_0.keys():
                vertices.append((n_vtx, vtx_dist + n_dist))

        [vertices.remove(x) for x in vertices[:] if x[0] in dist_from_0]


    answers = [None] * n
    sorted_dist = sorted(dist_from_0.items(), key=lambda x: x[0])
    for i in range(n):
        answers[i] = f"{sorted_dist[i][0]} {sorted_dist[i][1]}"
    [print(ans) for ans in answers]
    return


main()

Python: n=int(input())
d=[0]+[1e6]*n
c=[1]*n
M=[[-1]*n for _ in[0]*n]

for _ in[0]*n:
 data=list(map(int,input().split()))
 for i in range(data[1]):k=2*-~i;M[data[0]][data[k]]=data[k+1]

while 1:
 m,u=1e6,-1
 
 for i in range(n):
  if m>d[i]and c[i]:
      m,u=d[i],i
 if u<0:
     break
 c[u]=0
 
 for v in range(n):
  if c[v]and 1+M[u][v]and d[v]>d[u]+M[u][v]:
   d[v]=d[u]+M[u][v]
   
for i in range(n):
    print(i,d[i])

Python: n = int(input())
adj = [[float('inf')]*n for _ in range(n)]
for i in range(n):
    a = list(map(int, input().split()))
    for j,k in zip(a[2::2], a[3::2]):
        adj[i][j] = k
color = [0]*n
d = [float('inf')]*n
d[0] = 0

while 1:
    mincost = float('inf')
    for i in range(n):
        if color[i] != 1 and d[i] < mincost:
            mincost = d[i]
            u = i
    if mincost == float('inf'):
        break
        
    color[u] = 1
    
    for i in range(n):
        if color[i] != 1 and adj[u][i] != float('inf'):
            if d[u]+adj[u][i] < d[i]:
                d[i] = d[u] + adj[u][i]
                
for i in range(n):
    print(i, d[i])

Python: def dijkstra(s):
    color = ["white" for i in range(n)]

    d = [inf 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 ci[u][v] != inf:
                if d[u] + ci[u][v] < d[v]:
                    d[v] = d[u] + ci[u][v]
                    p[v] = u
                    color[v] = "gray"
    return d


inf = 10 ** 9
n = int(input())
ci = [[inf for i in range(n)] for j in range(n)]

for i in range(n):
    data = list(map(int, input().split()))
    u = data[0]
    k = data[1]

    for j in range(0, 2*k, 2):
        v, w = data[j+2], data[j+3]
        ci[u][v] = w

dList = dijkstra(0)

for i, d in enumerate(dList):
    print(i, 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):
    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)