Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := makeGraph(n)
	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			c := getNextInt(scanner)
			g[u][v] = c
		}
	}

	ans := make([]int, n)
	im := make(IntMap, 1)
	im.increment(0)
	for len(im) < n {
		min := 1 << 30
		x := -1
		for u := range im {
			for v := 0; v < n; v++ {
				if im.get(v) != 0 {
					continue
				}
				if min > ans[u]+g[u][v] {
					min = ans[u] + g[u][v]
					x = v
				}
			}
		}
		im.increment(x)
		ans[x] = min
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i, ans[i]))
	}
	writer.Flush()
}

func makeGraph(n int) [][]int {
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = 1 << 30
		}
	}

	return g
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

Python: from collections import defaultdict

def dijkstra(G, s):
  heap = Heap()
  heap.push((s, 0))
  fixed = set()
  d = defaultdict(lambda: 999999999999999999999)
  d[s] = 0
  while len(heap.data) > 0:
    v = heap.pop()
    if v in fixed:
      continue
    for u in G[v]:
      if u in fixed:
        continue
      c = G[v][u]
      if d[v] + c < d[u]:
        d[u] = d[v] + c
        heap.push((u, d[u]))
    fixed.add(v)
  return d


class Heap:
  def __init__(self):
    self.data = []

  def push(self,x):
    self.data.append(x)
    i = len(self.data)-1
    while self._p(i) >= 0:
      if self.data[i][1] < self.data[self._p(i)][1]:
        tmp = self.data[i]
        self.data[i] = self.data[self._p(i)]
        self.data[self._p(i)] = tmp
        i = self._p(i)
      else:
        break

  def pop(self):
    ret = self.top()
    n = self.data.pop()
    if len(self.data) > 0:
      self.data[0] = n
      i = 0
      while True:
        if self._l(i) < len(self.data):
          l = self.data[self._l(i)]
        else:
          l = (-1, 999999999999999999999)
        if self._r(i) < len(self.data):
          r = self.data[self._r(i)]
        else:
          r = (-1, 999999999999999999999)
        if l[1] > r[1]:
          if r[1] < self.data[i][1]:
            tmp = self.data[i]
            self.data[i] = self.data[self._r(i)]
            self.data[self._r(i)] = tmp
            i = self._r(i)
          else:
            break
        else:
          if l[1] < self.data[i][1]:
            tmp = self.data[i]
            self.data[i] = self.data[self._l(i)]
            self.data[self._l(i)] = tmp
            i = self._l(i)
          else:
            break
    return ret[0]

  def top(self):
    return self.data[0]

  def _p(self, i):
    i = i+1
    return i//2-1

  def _l(self, i):
    i = i+1
    return i*2-1

  def _r(self, i):
    i = i+1
    return i*2


G = {}

n = int(input())
for _ in range(n):
  entry = [int(v) for v in input().strip().split(' ')]
  u = entry[0]
  k = entry[1]
  G[u] = {}
  for i in range(k):
    v = entry[2+i*2]
    c = entry[3+i*2]
    G[u][v] = c

d = dijkstra(G, 0)

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

Python: def dijkstra(n, s):
    inf = float('inf')
    color = ['white' for _ in range(n)]
    D = [inf for _ in range(n)]             
    P = [0 for i in range(n)]        
    M = [[inf for _ in range(n)] for _ 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 True:
        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())
distance = dijkstra(n, 0)
for i in range(n):
    print(i, distance[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]))

Python: n = int(input())
global S, Q, l
S = [0]
Q = [0]
l = [1000000 for _ in range(n)]
l[0] = 0
graph = [[-1 for _ in range(n)] for _ in range(n)]
def input_graph(n):
  for _ in range(n):
    node_id, k, *v_list = list(map(int, input().split()))
    for j in range(k):
      ad_node, w = v_list[2*j], v_list[2*j+1]
      graph[node_id][ad_node] = w 


def dijkstra():
  while Q:
    u = float('inf') #10000000
    u_c = float('inf') #10000000
    for i in range(n):
      if i in Q and u_c > l[i]:
        u = i
        u_c = l[i]
    #print(l, u, S, Q)
    for i in range(n):
      v = i 
      if v not in S and graph[u][v] != -1:
        S.append(v)
        Q.append(v)
        l[v] = l[u] + graph[u][v]
      if (v in Q) and (l[v] > l[u] + graph[u][v]) and graph[u][v] != -1:
        l[v] = l[u] + graph[u][v]
    Q.remove(u)


if __name__ == "__main__":
  input_graph(n)
  dijkstra()
  for i, c in enumerate(l):
    print(i, c)

Python: 
def dijkstra():
    global n, color, M, d, p

    min_value = 1000000
    d[0] = 0
    color[0] = 'GRAY'

    while True:
        min_value = 1000000
        u = -1

        for i in range(n):
            if min_value > d[i] and color[i] != 'BLACK':
                u = i
                min_value = d[i]

        if u == -1:
            break

        color[u] = 'BLACK'

        for v in range(n):
            if color[v] != 'BLACK' and M[u][v] != 1000000:
                if d[v] > d[u] + M[u][v]:
                    d[v] = d[u] + M[u][v]
                    color[v] = 'GRAY'

    for i in range(n):
        if d[i] == 1000000:
            d[i] = -1

        print('{} {}'.format(i, d[i]))


n = int(input())

color = ['WHITE'] * n
M = [[1000000 for i in range(n)] for j in range(n)]
d = [1000000] * n  # 重みが最小の辺の重み
p = [-1] * n  # MSTにおけるp[v]の親（どこから来たか？？）

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

    for j in range(degree):
        v = _[j*2+2]
        w = _[j*2+3]
        M[u][v] = w
# print(M)

dijkstra()