Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

//make priority queue
type Item struct {
	id       int
	priority int // this state is priority and distans from start
	index    int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

// we want the ascending-priority-queue.
// So we use less than here.
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = j
	pq[j].index = i
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(dist int, id int) {
	for _, i := range *pq {
		if i.id == id {
			if i.priority > dist {
				i.priority = dist
			}
		}
	}
}

// the main code
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	return n
}

func dikstra(dist [][]int, n int) []int {
	// preparate priority queue
	inf := 1 << 30
	pq := make(PriorityQueue, n)
	shortests := make([]int, n)
	for i := 0; i < n; i++ {
		pq[i] = &Item{
			id:       i,
			priority: inf,
			index:    i,
		}

		shortests[i] = inf
	}
	pq[0].priority = 0
	shortests[0] = 0
	heap.Init(&pq)

	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		shortests[item.id] = item.priority
		for id, d := range dist[item.id] {
			if d != -1 {
				pq.update(item.priority+d, id)
			}
		}
		heap.Init(&pq)
	}
	return shortests
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// init
	n := nextInt(sc)
	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dist[i][j] = -1
		}
	}

	// std Input
	for i := 0; i < n; i++ {
		u := nextInt(sc)
		k := nextInt(sc)
		for j := 0; j < k; j++ {
			v, c := nextInt(sc), nextInt(sc)
			dist[u][v] = c
		}
	}

	shortests := dikstra(dist, n)
	for i, d := range shortests {
		fmt.Printf("%d %d\n", i, d)
	}
}

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