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: #!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

WHITE, GRAY, BLACK = 0, 1, 2
D_MAX = int(1e7 + 1)


def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_index = int(v_detail[0])
        v_adj_length = int(v_detail[1])
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == v_adj_length * 2
        for j in range(0, v_adj_length * 2, 2):
            init_adj_matrix[v_index][v_adj_list[j]] = int(v_adj_list[j + 1])

    return init_adj_matrix


def dijkstra_path():
    path_list[0] = 0

    while True:
        min_cost = D_MAX
        current_vertex = None
        for i in range(vertices_num):
            if color[i] is not BLACK and path_list[i] < min_cost:
                min_cost = path_list[i]
                current_vertex = i

        if min_cost == D_MAX:
            break

        color[current_vertex] = BLACK

        for v in range(vertices_num):
            current_v_weight = adj_matrix[current_vertex].get(str(v), -1)
            if current_v_weight < 0:
                continue
            elif color[v] is not BLACK:
                if path_list[current_vertex] + current_v_weight < path_list[v]:
                    path_list[v] = path_list[current_vertex] + current_v_weight
                    parent_list[v] = current_vertex
                    color[v] = GRAY

    return path_list


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num = int(_input[0])
    init_vertices_table = list(map(lambda x: x.split(), _input[1:]))
    # assert len(init_vertices_table) == vertices_num

    parent_list, path_list = [-1] * vertices_num, [D_MAX] * vertices_num
    color = [WHITE] * vertices_num
    init_adj_matrix = [{} for _ in range(vertices_num)]
    adj_matrix = generate_adj_matrix(init_vertices_table)
    ans = dijkstra_path()
    for j in range(vertices_num):
        print(j, ans[j])

Python: from heapq import heappop, heappush

def dijkstra1(g, source):
    # O(ElogV)

    n = len(g)
    inf = float("inf") # 高速化のためにはfloat("inf")ではないほうがいい
    dist = [inf] * n
    dist[source] = 0
    q = [(0, source)]

    while q:
        cost, u = heappop(q)
        if dist[u] < cost:
            continue
        for c, v in V[u]:
            if dist[v] > dist[u] + c:
                dist[v] = dist[u] + c
                heappush(q, (dist[v], v))

    return dist

def dijkstra2(g, source):
    n = len(V)
    inf = float("inf") # 高速化のためにはfloat("inf")ではないほうがいい
    used = [False]*n
    dist = [inf]*n
    dist[0] = 0
    
    for _ in range(n):
        c1, u = min([(dist[i], i) for i in range(n) if not used[i]])
        used[u] = True
        for c2, v in V[u]:
            if v==u: continue
            dist[v] = min(dist[v], c1+c2)
    return dist


if __name__ == "__main__":
    n = int(input())
    V = [[] for _ in range(n)]

    for _ in range(n):
        u, k, *vc = map(int, input().split())
        for i in range(0, 2*k, 2):
            v, c = vc[i], vc[i+1]
            V[u].append((c, v))

    d = dijkstra2(V, 0)
    for i, j in enumerate(d):
        print(i, j)

Python: import heapq

def dijkstra(graph, weights, s):
    d = {u: float('inf') for u in graph}
    d[s] = 0
    queue = []
    heapq.heappush(queue, (0, s))
    while queue:
        w, u = heapq.heappop(queue)
        for v in graph[u]:
            d_temp = d[u] + weights[u, v]
            if d[v] > d_temp:
                d[v] = d_temp
                heapq.heappush(queue, (d_temp, v))
    return d

n = int(input())
graph, weights = {}, {}
for _ in range(n):
    line = input().split()
    neighbors = []
    for i in range(0, 2 * int(line[1]), 2):
        neighbors.append(line[2+i])
        weights[line[0], line[2+i]] = int(line[3+i])
    graph[line[0]] = neighbors

d = dijkstra(graph, weights, '0')

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

Python: n=int(input())
M=[[-1]*n for _ in[0]*n]
for _ in[0]*n:
 e=list(map(int,input().split()))
 for i in range(e[1]):k=2*-~i;M[e[0]][e[k]]=e[k+1]
d=[0]+[1e6]*~-n
c=[1]*n
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: 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])