Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [][]int

func (g *Graph) dikstra(n int) string {
	var (
		u, minv = 0, 0
	)

	d := make([]int, n)
	color := make([]int, n)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		color[i] = WHITE
	}
	d[0] = 0
	color[0] = GRAY

	for {
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for v := 0; v < n; v++ {
			if color[v] != BLACK && (*g)[u][v] != INFTY {
				if d[v] > d[u]+(*g)[u][v] {
					d[v] = d[u] + (*g)[u][v]
					color[v] = GRAY
				}
			}
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		out.WriteString(fmt.Sprintf("%d ", i))
		if d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d\n", -1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d\n", d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make(Graph, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			g[i][j] = INFTY
		}
	}

	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())

		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())

			g[u][v] = c
		}
	}

	return g.dikstra(n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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()