Go: package main

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

const BIG_COST = 200000

type Path struct {
	node int
	cost int
}

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

	nNodes := scanInt(sc)

	// 各頂点につながる頂点一覧を得るための隣接リスト
	graph := make([][]Path, nNodes)

	for i := 0; i < nNodes; i++ {
		u := scanInt(sc)
		k := scanInt(sc)
		graph[i] = make([]Path, k)
		for d := 0; d < k; d++ {
			graph[u][d] = Path{
				node: scanInt(sc),
				cost: scanInt(sc),
			}
		}
	}

	// 0 から各頂点への最小コストを保存する配列
	minCosts := make([]int, nNodes)

	minCosts[0] = 0
	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	// 各頂点が調査済みかどうかを保持する配列
	checked := make([]bool, nNodes)

	// 最小コストのパスを高速に取得するためのヒープ。
	// コストが小さいものほどルートに来るようにする。
	heap := NewHeap(func(a, b Path) bool {
		return a.cost < b.cost
	})

	heap.insert(Path{node: 0, cost: 0})
	for heap.size > 0 {
		u := heap.extractMax()

		// 隣接行列を使っていた時とは違い、ヒープには
		// 同じノードへのパスが複数含まれうる。そのため既によりコストの低い
		// パスが見つかっている場合は無視する。
		if minCosts[u.node] < u.cost {
			continue
		}

		checked[u.node] = true

		for _, v := range graph[u.node] {
			cost := v.cost + u.cost
			if !checked[v.node] && cost < minCosts[v.node] {
				minCosts[v.node] = cost
				heap.insert(Path{node: v.node, cost: cost})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i, cost := range minCosts {
		fmt.Fprintf(w, "%d %d\n", i, cost)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

// --- From ALDS1_9_C: Priority Queue

type Heap struct {
	size   int
	heap   []Path
	isHigh func(a, b Path) bool
}

func NewHeap(isHigh func(a, b Path) bool) Heap {
	heap := make([]Path, 1) // 先頭は不使用
	return Heap{size: 0, heap: heap, isHigh: isHigh}
}

func (h *Heap) insert(v Path) {
	h.size += 1
	if h.size > len(h.heap)-1 {
		h.heap = append(h.heap, v)
	} else {
		h.heap[h.size] = v
	}

	for i := h.size; i > 1; {
		parent := i / 2
		if h.isHigh(h.heap[parent], h.heap[i]) {
			break
		}
		h.heap[i], h.heap[parent] = h.heap[parent], h.heap[i]
		i = parent
	}
}

func (h *Heap) extractMax() Path {
	if h.size <= 0 {
		panic("cannot extract from empty heap")
	}

	max := h.heap[1]
	h.heap[1] = h.heap[h.size]
	h.size -= 1
	h.maxHeapify(1)

	return max
}

func (h *Heap) maxHeapify(idx int) {
	left := idx * 2
	right := left + 1
	largest := idx

	if left <= h.size {
		if h.isHigh(h.heap[left], h.heap[idx]) {
			largest = left
		}
		if right <= h.size && h.isHigh(h.heap[right], h.heap[largest]) {
			largest = right
		}
	}

	if largest != idx {
		h.heap[idx], h.heap[largest] = h.heap[largest], h.heap[idx]
		h.maxHeapify(largest)
	}
}

Python: from heapq import*
I=float("inf")
def m():
 n=int(input())
 A=[]
 for _ in range(n):
  e=list(map(int,input().split()))
  A+=[zip(e[2::2],e[3::2])]
 d=[0]+[I]*n
 H=[(0,0)]
 while H:
  u=heappop(H)[1]
  for v,c in A[u]:
   t=d[u]+c
   if d[v]>t:
    d[v]=t
    heappush(H,(t,v))
 print('\n'.join(f'{i} {d[i]}'for i in range(n)))
m()

Python: #coding:utf-8
dic = {}
nil = -1

class Doubly:
    def __init__(self,key,l=nil,r=nil):
        self.l = l
        self.r = r
        self.key = key

    def Insert(self,key):
        obj = Doubly(key)
        obj.l = fake
        fakeR = fake.r
        fake.r = obj
        obj.r = fakeR
        fakeR.l = obj
        if key in dic.keys():
            dic[key].append(obj)
        else:
            dic[key] = [obj]
    def deleteFirst(self):
        fakeR = fake.r
        fake.r = fakeR.r
        fakeR.r.l = fake
        key = fakeR.key
        if dic != {}:
            dic[key].pop(0)
            if dic[key] == []:
                del dic[key]

    def deleteLast(self):
        fakeL = fake.l
        fake.l = fakeL.l
        fakeL.l.r = fake
        key = fakeL.key
        if dic != {}:
            dic[key].pop(0)
            if dic[key] == []:
                del dic[key]
        

    def Delete(self,target):
        if target in dic.keys():
            obj = dic[target].pop(0)
            objL = obj.l
            objR = obj.r
            objL.r = objR
            objR.l = objL
            if dic[target] == []:
                del dic[target]
        


n = int(input())

fake = Doubly(nil)
fake.l = fake
fake.r = fake


for i in range(n):
    data = list(input().split())
    order = data[0]
    if order == "insert":
        Doubly.Insert(Doubly, int(data[1]))
    elif order == "delete":
        Doubly.Delete(Doubly, int(data[1]))
    elif order == "deleteFirst":
        Doubly.deleteFirst(Doubly)
    else:
        Doubly.deleteLast(Doubly)

obj = fake.r
List = []
while obj.key != -1:
    List.append(str(obj.key))
    obj = obj.r

print(" ".join(List))

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3

output:
0 0
1 2
2 2
3 1
4 3
"""

import sys
import heapq as hp

WHITE, GRAY, BLACK = 0, 1, 2


def generate_adj_table(v_table):
    for each in v_table:
        v_index, v_adj_length, *v_adj_list = map(int, each)
        # assert len(v_adj_list) == v_adj_length * 2
        for pair in zip(v_adj_list[::2], v_adj_list[1::2]):
            init_adj_table[v_index][pair[0]] = pair[1]

    return init_adj_table


def dijkstra_path_heap():
    # path search init
    distance[init_vertex] = 0
    path_heap = []
    # heapq: rank by tuple[0], at here it's d[u]
    hp.heappush(path_heap, (0, init_vertex))

    while path_heap:
        current = hp.heappop(path_heap)[1]

        color[current] = BLACK

        for adj, cost in adj_table[current].items():
            if color[adj] is not BLACK:
                # alt: d[u] + w[u,v]
                alt_path = distance[current] + cost
                if alt_path < distance[adj]:
                    # update path_list
                    distance[adj] = alt_path
                    # update heap
                    hp.heappush(path_heap, (alt_path, adj))
                    color[adj] = GRAY
                    parents[adj] = current

    return distance


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

    distance = [float('inf')] * vertices
    parents = [-1] * vertices
    color = [WHITE] * vertices
    init_adj_table = tuple(dict() for _ in range(vertices))
    init_vertex = 0

    adj_table = generate_adj_table(v_info)
    ans = dijkstra_path_heap()
    for i, v in enumerate(ans):
        print(i, v)

Python: import sys

f_i = sys.stdin

n = int(f_i.readline())

class VCost:
    def __init__(self, v, cost):
        self.v_n = v
        self.cost = cost
    def __lt__(self, other):
        return self.cost < other.cost
    def __gt__(self, other):
        return self.cost > other.cost

adj = [[VCost(int(v), int(c)) for v, c in zip(x.split()[2::2], x.split()[3::2])] for x in f_i]

import heapq

def dijkstra():
    PQ = []
    isVisited = [False] * n
    distance = [999900001] * n

    distance[0] = 0
    heapq.heappush(PQ, VCost(0, 0))
    
    while PQ:
        uc = heapq.heappop(PQ)
        u = uc.v_n

        if uc.cost > distance[uc.v_n]:
            continue

        isVisited[u] = True
        
        for vc in adj[u]:
            v = vc.v_n
            if isVisited[v] == True:
                continue
            t_cost = distance[u] + vc.cost
            if t_cost < distance[v]:
                distance[v] = t_cost
                heapq.heappush(PQ, VCost(v, t_cost))

    for v, d in enumerate(distance):
        print(v, d)


dijkstra()