Go: package main

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

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	route := make([][]Node, n)
	weight := make([]int, n)
	for i := 0; i < n; i++ {
		weight[i] = NIL
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		route[i] = make([]Node, k)
		for j := 0; j < k; j++ {
			route[u][j] = Node{id: scanInt(), weight: scanInt()}
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for _, nextNode := range route[node.id] {
			if weight[nextNode.id] != NIL {
				continue
			}
			nextNode.weight += node.weight
			heap.Push(h, nextNode)
		}
	}

	for i, w := range weight {
		fmt.Println(i, w)
	}
}

Python: from sys import maxsize
from heapq import heappop, heappush


class Node:
    WHITE = 0
    GRAY = 1
    BLACK = 2

    def __init__(self):
        self.adjs = []
        self.color = Node.WHITE
        self.dist = maxsize

    def __lt__(self, other):
        return self.dist < other.dist

    def __repr__(self):
        return "Node({},{},{})".format(self.adjs, self.color, self.dist)


def read_adj():
    n = int(input())
    nodes = [Node() for _ in range(n)]

    for i in range(n):
        u, _, *rest = [int(x) for x in input().split()]
        for i in range(0, len(rest) - 1, 2):
            nodes[u].adjs.append((rest[i], rest[i+1]))

    return nodes


def shortest_path(nodes):
    h = []

    nodes[0].dist = 0
    nodes[0].color = Node.GRAY
    heappush(h, (0, 0))

    while len(h) > 0:
        min_node, min_dist = heappop(h)
        if nodes[min_node].dist < min_dist:
            continue

        nodes[min_node].color = Node.BLACK

        for adj, c in nodes[min_node].adjs:
            if nodes[min_node].color == Node.BLACK:
                continue
            if nodes[min_node].dist + c < nodes[adj].dist:
                nodes[adj].dist = nodes[min_node].dist + c
                nodes[adj].color = Node.GRAY
                heappush(h, (adj, nodes[adj].dist))


def print_dist(nodes):
    for u, n in enumerate(nodes):
        print(u, n.dist)


def main():
    nodes = read_adj()
    shortest_path(nodes)
    print_dist(nodes)


if __name__ == '__main__':
    main()

Python: from sys import stdin
from collections import deque
import heapq

def read_graph(n):
    A = [ [] for _ in range(n) ]
    for _ in range(n):
        line = deque(stdin.readline().strip().split())
        u = line.popleft()
        line.popleft()
        for l, k in enumerate(line):
            if l % 2 == 0:
                v = k
            else:
                A[int(u)].append((int(v), int(k)))
    return A

def dijkstra(n, A, s):
    color = [ 'WHITE' for i in range(n) ]
    d = [ float('inf') for i in range(n) ]
    d[s] = 0
    pq = []

    heapq.heappush(pq, (s, 0))

    while pq:
        costu, u = heapq.heappop(pq)
        color[u] = 'BLACK'

        if d[u] < costu:
            continue

        for v, costv in A[u]:
            if color[v] != 'BLACK':
                if d[u] + costv < d[v]:
                    d[v] = d[u] + costv
                    color[v] = 'GRAY'
                    heapq.heappush(pq, (d[v], v))
    return d

n = int(input())
A = read_graph(n)
for i, k in enumerate(dijkstra(n, A, 0)):
    print(i, k)

Python: from heapq import heappop, heappush
import sys


class SSSP:
    """Given weighted graph, find Single Sorce Shortest Path from node 0.

    >>> st = SSSP([[(2, 3), (3, 1), (1, 2)], \
                   [(0, 2), (3, 4)], \
                   [(0, 3), (3, 1), (4, 1)], \
                   [(2, 1), (0, 1), (1, 4), (4, 3)], \
                   [(2, 1), (3, 3)]])
    >>> [st.weight(n) for n in range(5)]
    [0, 2, 2, 1, 3]
    """
    def __init__(self, graph):
        self.size = len(graph)
        self.tree = []
        self.weights = [0] * self.size
        self._search(graph)

    def weight(self, n):
        return self.weights[n]

    def _search(self, graph):
        h = []
        count = 1
        for i, w in graph[0]:
            heappush(h, (w, 0, i))

        while count < self.size:
            while len(h) > 0:
                weight, src, dst = heappop(h)
                if self.weights[dst] == 0 and dst > 0:
                    self.weights[dst] = weight
                    count += 1
                    self.tree.append((src, dst))
                    for i, w in graph[dst]:
                        if self.weights[i] == 0 and dst > 0:
                            heappush(h, (w+weight, dst, i))
                    break


def run():
    n = int(input())

    nodes = [None] * n
    for line in sys.stdin:
        id_, deg, *edges = [int(i) for i in line.split()]
        nodes[id_] = []
        for _ in range(deg):
            tgt, wgt, *edges = edges
            nodes[id_].append((tgt, wgt))

    st = SSSP(nodes)
    for node in range(n):
        print('{} {}'.format(node, st.weight(node)))


if __name__ == '__main__':
    run()

Python: #from heapdict import heapdict
import sys
import heapq
input=sys.stdin.readline

def dijkstra(G,start,n):#グラフ,初期頂点,頂点数
    INF=2<<28
    dist=[INF]*n;dist[start]=0
    used=[False]*n;used[start]=True
    q=[]

    for v in G[start]:
        heapq.heappush(q,v)
    
    while len(q)>0:
        now1,now2=heapq.heappop(q)
        #print(now1,now2)
        if used[now2]:
            continue
        v=now2
        dist[v]=now1
        used[v]=True

        for e1,e2 in G[v]:
            if not used[e2]:
                heapq.heappush(q,(e1+dist[v],e2))

    return dist

def main():
    N=int(input().rstrip())
    g=[[] for _ in range(N)]
    for _ in range(N):
        u,k,*c=map(int,input().split())
        for j in range(0,k*2,2):
            g[u].append((c[j+1],c[j]))

    ans=dijkstra(g,0,N)
    for i,x in enumerate(ans):
        print(i,x)

if __name__=="__main__":
    main()

Python: import heapq

INFTY = 1<<20

N = int(input())
M = [[] for _ in range(N)]
for i in range(N):
    u, k, *adjacents = map(int, input().split())
    for j in range(k):
        edge = adjacents[2*j]
        weight = adjacents[2*j+1]
        M[i].append((weight, edge))

mst = [False for _ in range(N)]
d = [INFTY for _ in range(N)]
d[0] = 0
pq = []
heapq.heappush(pq, (0, 0))

while len(pq) != 0:
    u = heapq.heappop(pq)
    u_edge = u[1]
    u_cost = u[0]

    mst[u_edge] = True

    if d[u_edge] < u_cost:
        continue

    for v in M[u_edge]:
        v_edge = v[1]
        v_cost = v[0]
        if mst[v_edge] == True:
            continue
        if d[u_edge]+v_cost < d[v_edge]:
            d[v_edge] = d[u_edge]+v_cost
            heapq.heappush(pq, (d[v_edge], v_edge))

for i in range(N):
    print("{} {}".format(i, d[i]))