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: import heapq

n=int(input())
u=[0]*n
k=[0]*n
v=[0]*n
z=[[] for _ in range(n) ]
for i in range(n):
  u[i],k[i],*v[i] = map(int,input().split())
  for j in range(0,len(v[i]),2):
    z[i].append([v[i][j],v[i][j+1]])
d=[float("inf")]*n 
e=[-1] *n #いったことなければ -1


def dijkstra(x):
  e[x]=0 #ブクマ
  d[x]=0
  q= [ (d[x],x) ] 
  
  while len(q)>0:
    #print(d)
    b,a = heapq.heappop(q)
    e[a]=0
    
    if d[a] < b:
      pass
    else:
      to_go = z[a]
      for s,t in to_go:
        if e[s]==0:
          pass
        else:
          if d[s] > d[a]+t :
            d[s] = d[a]+t
            heapq.heappush( q, (d[s],s) )

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

Python: import queue
from sys import maxsize as INFTY
from collections import namedtuple
Q = namedtuple("Q", "u c")

n = int(input())
dic = {}

for _ in range(n):
    a = [int(x) for x in input().split()]
    a.reverse()

    u, k = a.pop(), a.pop()
    for _ in range(k):
        v, c = a.pop(), a.pop()
        dic[(u, v)] = c


def bfs(n, s):
    res = [INFTY] * n
    q = queue.Queue()
    q.put(Q(s, 0))

    while q.qsize() != 0:
        p = q.get()
        if res[p.u] <= p.c: continue
        res[p.u] = p.c

        for i in range(n):
            if i == p.u: continue
            v = (p.u, i)
            if v not in dic: continue
            q.put(Q(i, p.c + dic[v]))
    return res


ans = bfs(n, 0)
for i in range(n):
    print(i, ans[i])

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