Go: // ダイクストラのアルゴリズム
package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	num       int         // 頂点の番号
	weights   map[int]int // 重みMAP
	parent    *node       // MSTへの重み最小の親
	distFromS int         // 始点からの距離
	color     color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]*node, n)
	for i := 0; i < n; i++ {
		weights := eAtoi((strings.Fields(eGetline(scanner))))
		v := &node{weights[0], make(map[int]int, weights[1]), nil, math.MaxInt32, white}
		for j := 0; j < weights[1]; j++ {
			k := weights[j*2+2] // 相手の頂点名
			w := weights[j*2+3] // 重み
			v.weights[k] = w
		}
		nodes[i] = v
	}

	// 初回はnode[0]がMSTに入るように
	nodes[0].distFromS = 0

	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか

		var minNode *node
		min := math.MaxInt32
		for _, v := range nodes {
			if v.color == mst {
				continue
			}
			if v.distFromS < min {
				min = v.distFromS
				minNode = v
			}
		}
		if minNode == nil {
			// すべて完了している.
			break
		}

		// MSTに追加する頂点決定
		minNode.color = mst

		// fmt.Printf(">[%v]\n", minNode)
		// MSTに属していない頂点それぞれについて,Startからの最小の重みの距離を更新
		for i, v := range nodes {
			if v.color == mst {
				continue
			}
			w, ok := minNode.weights[i]
			if !ok {
				// minNodeと接してない
				continue
			}

			if minNode.distFromS+w < v.distFromS {
				// minNode経由の距離がもっとも重みが少ないので親を変更
				v.distFromS = minNode.distFromS + w
				v.parent = minNode
				// fmt.Printf("[%v %v]\n", v, minNode)
			}
		}
		// fmt.Printf("<\n")
	}

	for i, v := range nodes {
		fmt.Printf("%d %d\n", i, v.distFromS)
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

Python: def dijkstra(matrix, s):
    INF = 200000
    n = len(matrix)
    state, d, p = [False] * n, [INF] * n, [-1] * n
    d[s] = 0
    while True:
        mincost = INF
        for i in range(n):
            if not state[i] and d[i] < mincost:
                mincost = d[i]
                u = i
        if mincost == INF:
            break
        state[u] = True
        for v in range(n):
            if not state[v] and matrix[u][v] != -1:
                if d[u] + matrix[u][v] < d[v]:
                    d[v] = d[u] + matrix[u][v]
                    p[v] = u
    return d


def main():
    n = int(input())
    m = [[-1] * n for _ in range(n)]
    for _ in range(n):
        u, k, *vc = map(int, input().split())
        for v, c in zip(vc[::2], vc[1::2]):
            m[u][v] = c
    dist = dijkstra(m, 0)
    for i, d in enumerate(dist):
        print(i, d)


if __name__ == '__main__':
    main()

Python: n = int(input())
adjc_list = [[] for _ in range(n)]
INF = 99999999999999999999
V = [ele for ele in range(n)]
S = []
d = [INF] * n
p = [ele for ele in range(n)]

for i in range(n):
    tmp = [int(ele) for ele in input().split()]
    for j in range(1, tmp[1]*2, 2):
        v = tmp[j+1]
        w = tmp[j+2]
        adjc_list[i].append([w, v])
    adjc_list[i].sort()


def my_dijkstra_solver():
    root = 0  # given
    # 1. 初期化
    d = [INF] * n
    d[root] = 0
    VS = V

    # 2. 以下の処理を、S = Vになるまで繰り返す
    while VS:
        # 2.1. V-S の中から、 d[u] が最小である頂点 u を選択する。
        u = VS[0]
        for i in VS:
            if d[i] < d[u]:
                u = i

        S.append(u)  # 2.2. u を S に追加する.
        # 2.3. u に隣接し, かつ V-S に属するすべての頂点 v に対する以下の値を更新する
        VS.remove(u)
        for w, v in adjc_list[u]:
            if v in VS:
                if d[v] > d[u] + w:
                    d[v] = d[u] + w
                    p[v] = u  # 親をどこかで使うのはわかるが一旦忘れよう

    for i, cost in enumerate(d):
        print(i, cost)


if __name__ == "__main__":
    my_dijkstra_solver()

Python: from heapq import heappop, heappush
from collections import defaultdict


class Dijkstra:
    """Graph input:
    {
    node:{node:cost,node:cost...},
    node:{node:cost,node:cost...},
    ...
    }
    """

    def __init__(self, graph: dict, N: int):
        self.graph = graph
        self.costs = [float("inf")] * N

    def calculate(self, start):
        queue = list()

        # step1: push the start point into the priority queue
        self.costs[start] = 0
        heappush(queue, (0, start))  # (cost,node)

        while queue:
            # step2: get the minimum cost node from unfixed node and fix the cost
            cost_from_start, min_node = heappop(queue)

            # step3: update the direct connected nodes
            for link, cost in self.graph[min_node].items():
                if cost_from_start + cost < self.costs[link]:
                    self.costs[link] = cost_from_start + cost
                    heappush(queue, (self.costs[link], link))

        return self.costs


n = int(input())
graph = defaultdict(dict)

for _ in range(n):
    data = [int(i) for i in input().split()]
    u = data[0]
    count = data[1]
    for j in range(1, count + 1):
        v = data[2 * j]
        w = data[2 * j + 1]
        graph[u][v] = w

dijkstra = Dijkstra(graph, n)
costs = dijkstra.calculate(0)

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

Python: INF = 1.0e+6
WHITE = 0
BLACK = 1

def dijkstras(M):
    n = len(M)
    d = [INF] * n
    color = [WHITE] * n

    d[0] = 0

    while True:
        minv = INF
        u = -1
        for i in range(n):
            if d[i] < minv and color[i] != BLACK:
                u = i
                minv = d[i]

        if u == -1:
            break

        color[u] = BLACK

        for v in range(n):
            if color[v] != BLACK and M[u][v] != INF:
                if d[v] > d[u] + M[u][v]:
                    d[v] = d[u] + M[u][v]

    for i in range(n):
        print("{} {}".format(i, -1 if d[i] == INF else d[i]))


if __name__ == "__main__":
    n = int(input())
    M = [[INF] * n for i in range(n)]

    for i in range(n):
        arr = list(map(int, input().split()))
        for j in range(arr[1]):
            M[arr[0]][arr[2+j*2]] = arr[3+j*2]

    dijkstras(M)

Python: from heapq import heappush, heappop
import math
def dijkstra(N, G, s):
    dist = [math.inf] * N
    que = [(0,s)]
    dist[s] = 0
    ans = [[] for i in range(N)]
    while que:
        c,v = heappop(que)
        if dist[v] < c:
            
            continue
        for t, cost in G[v]:
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heappush(que,(dist[t],t))
    return dist
n = int(input())

g = [[]for i in range(n)]
for i in range(n):
    nums=list(map(int,input().split()))
    node=nums[0]
    for j in range(nums[1]):
        t=nums[j*2+2]
        c=nums[j*2+3]
        g[node].append([t,c])
   
ans = dijkstra(n,g,0)
for i in range(n):
    print(f'{i} {ans[i]}')