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: INFTY = 1 << 21
WHITE = 0
GRAY = 1
BLACK = 2
 
n = int(input())
M = [list(map(int, input().split())) for i in range(n)]
 
d = [INFTY for i in range(n)]
color = [INFTY for i in range(n)]
 
for i in range(n):
    u = int(input())
    k = int(input())
    for j in range(k):
        v = int(input())
        c = int(input())
        M[u][v] = c
        
def dijkstra():
    global M, d, p, color
    d[0] = 0
    color[0] = GRAY
    while(1):
        minv = INFTY
        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 v in range(n):
            if (color[v] != BLACK and M[u][v] != INFTY):
                if (d[v] > d[u] + M[u][v]):
                    d[v] = d[u] + M[u][v]
                    color[v] = GRAY
    for i in range(n):
        print(str(i) + " " + str(d[i]))
print(dijkstra())

Python: # Graph II - Single Source Shortest Path I
def dijkstra(length, adj_m):
    INFTY = float('inf')
    d = [INFTY for _ in range(length)]
    d[0] = 0
    Q = [i for i in range(length)]
    while Q:
        idx = INFTY
        minv = INFTY
        for q in Q:
            if d[q] < minv:
                idx, minv = q, d[q]
        u = Q.pop(Q.index(idx))
        for i, v in enumerate(adj_m[u]):
            if not v == -1:
                if d[i] > d[u] + v:
                    d[i] = d[u] + v
    return d

n = int(input())
adj_m = [[-1 for _ in range(n)] for _ in range(n)]
for _ in range(n):
    ss = list(map(int, input().split()))
    u, k = ss[0], ss[1]
    for i in range(k):
        vi,ci = ss[2*i+2], ss[2*i+3]
        adj_m[u][vi] = ci
d = dijkstra(n, adj_m)
for i,v in enumerate(d): print(i,v)

Python: import argparse
import sys
inf = sys.maxsize
import heapq

def dijkstra_heap(edge_list, node):    
    distance_list = n_node * [inf]
    distance_list[node] = 0
    heap = []
    heapq.heappush(heap, (0, 0))

    while(heap):
        temp_dist, from_node = heapq.heappop(heap)
        if distance_list[from_node] < temp_dist:
            continue
        
        for to_node, cost in edge_list[from_node].items():
            if distance_list[to_node] > distance_list[from_node] + cost:
                distance_list[to_node] = distance_list[from_node] + cost
                heapq.heappush(heap, (distance_list[to_node], to_node))
    
    return distance_list


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', type=str)
    args = parser.parse_args()
    edge_input = []
    if args.input:
        f = open(args.input, 'r')
        n_node = int(f.readline())                
        for _ in range(n_node):            
            edge_input.append(tuple(map(int, f.readline().split()))[2:])
        f.close()
    else:
        n_node = int(input())
        for i in range(n_node):            
            edge_input.append(tuple(map(int, input().split()))[2:])
    
    edge_list = [None] * n_node
    for i in range(n_node):
        edge_list[i] = {}
        for j in range(0, len(edge_input[i]), 2):
            edge_list[i][edge_input[i][j]] = edge_input[i][j+1]

    distance_list = dijkstra_heap(edge_list, 0)

    # # bellman_ford(edge_list, 0)
    # distance_list = dijkstra(edge_list, 0)
    for i, ditance in enumerate(distance_list):
        print(i, distance_list[i])

Python: import sys
input = sys.stdin.readline


def dijkstra_v2(start: int, matrix: list) -> list:
    """dijkstra法: 始点startから各頂点への最短距離を求める
    計算量: O(V^2)
    """
    INF = 10 ** 18
    n = len(matrix)
    used = [False] * n
    dist = [INF] * n
    dist[start] = 0
    while True:
        v = -1
        for u in range(n):
            if not used[u] and (v == -1 or dist[u] < dist[v]):
                v = u
        if v == -1:
            break
        used[v] = True
        for nxt_v in range(n):
            dist[nxt_v] = min(dist[nxt_v], dist[v] + matrix[v][nxt_v])
    return dist
  

n = int(input())
info = [list(map(int, input().split())) for i in range(n)]
INF = 10 ** 18

matrix = [[INF] * n for i in range(n)]
for a in range(n):
    matrix[a][a] = 0
    for j in range(1, len(info[a]) // 2):
        b, cost = info[a][slice(2 * j, 2 * (j + 1))]
        matrix[a][b] = cost

dist = dijkstra_v2(0, matrix)
for v, ans in enumerate(dist):
    print(v, ans)

Python: #!/usr/bin/python3
from heapq import heappop, heappush
n = int(input())
inf = 100 * 100000

M = [[] for i in range(n)]

for i in range(n):
    L = list(map(int, input().split()))
    u = L[0]
    k = L[1]
    v = L[2::2]
    c = L[3::2]
    for j in range(k):
        M[u].append((v[j], c[j]))
result = [0] * n
S = set()
V = set(range(n))
S.add(0)
d = []
for m in M[0]:
    heappush(d, (m[1], m[0]))
T = V - S

while len(T) != 0:
    c, v = heappop(d)
    while v in S:
        c, v = heappop(d)
    result[v] = c
    S.add(v)
    T.remove(v)
    for m in M[v]:
        if m[0] in T:
            heappush(d, (result[v] + m[1], m[0]))

for i, v in enumerate(result):
    print(i, v)