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への重み最小の親
	sum     int         // 始点からの距離
	color   color
}

func (p *node) getMinToMst() int {
	if p.parent == nil {
		return math.MaxInt32
	}
	return p.weights[p.parent.num]
}

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, 0, 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].parent = nodes[0]

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

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

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

		// MSTに追加する頂点決定
		minNode.color = mst
		// 自分の親の合計値と合算する
		minNode.sum = minNode.parent.sum + min

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みの距離を加算
		for _, v := range nodes {
			if v.color == mst {
				continue
			}
			w, ok := v.weights[minNode.num]
			if !ok {
				// minNodeと接してない
				continue
			}

			if w < v.getMinToMst() {
				// minNodeへの距離がもっとも重みが少ないので親を変更
				v.parent = minNode
			}
		}
	}

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

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 relax(u, v, w):
    if D[u] + w < D[v]:
        D[v] = D[u] + w
        P[v] = u

INF = float('inf')
N = int(input())
D = [INF for i in range(N)]
P = [None for i in range(N)]
C = ['w' for i in range(N)]
D[0] = 0

G = [[INF for j in range(N)] for i in range(N)]
for i in range(N):
    x = list(map(int, input().split()))
    g = x[2:]
    for j, n in enumerate(g):
        if j % 2 == 0:
            G[i][n] = g[j + 1]


while True:
    mincost = INF
    u = -1
    for i in range(N):
        if C[i] != 'b' and D[i] < mincost:
            mincost = D[i]
            u = i
    if u == -1:
        break
    C[u] = 'b'
    for v, w in enumerate(G[u]):
        if w != INF and C[v] != 'b':
            relax(u, v, w)
            C[v] = 'g'

for i, d in enumerate(D):
    print('{0} {1}'.format(i, d))

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

def main():
    INF = float('inf')
    V = int(input())
    dist = [INF] * V
    visited = [False] * V
    graph = [None] + [deque() for i in range(V-1)]
    
    q = []
    tmp = list(map(int, input().split()))
    for i in range(tmp[1]): heappush(q, (tmp[2+2*i+1], tmp[2+2*i]))
        
    for i in range(1, V):
        tmp = list(map(int, input().split()))
        
        for j in range(tmp[1]):
            graph[i].append((tmp[2+2*j+1], tmp[2+2*j]))
        
    dist[0], visited[0] = 0, True
        
    while q:
        cost, v = heappop(q)
        if visited[v]: continue
    
        visited[v] = True
        dist[v] = cost
        
        for c, u in graph[v]:
            if visited[u] or dist[u] < cost + c: continue
            heappush(q, (cost+c, u))
    
    for i in range(V): print(i, dist[i])
        
if __name__ == '__main__':
    main()

Python: n = int(input())
g = [[2**64]*n for i in range(n)]
for i in range(n):
    u, k, *l, = map(int, input().split())
    for j in range(k):
        v, c = l[j*2], l[j*2+1]
        g[u][v] = c

'''
ダイクストラ法による単一始点最短経路
'''
d = [2**64]*n
d[0] = 0

s = []
v = [i for i in range(n)]
while len(s) < n:
    c, u = 2**32, -1
    for vv in v:
        if d[vv] < c:
            c, u = d[vv], vv
    s.append(u)
    v.remove(u)
    for i, c in enumerate(g[u]):
        if i in v and d[u]+g[u][i] < d[i]:
            d[i] = d[u]+g[u][i]

for i in range(n):
    print(f"{i} {d[i]}")

Python: 
def dijkstra(adj_matrix, n):
    dist = [float("inf") for _ in range(n)]
    visited = [False for _ in range(n)]
    parent = [-1 for _ in range(n)]

    start = 0
    dist[start] = 0
    visited[start] = True
    while True:
        for end in range(n):
            # startから出ているノードの距離を計算
            if not visited[end] and adj_matrix[start][end] > -1 and dist[start] + adj_matrix[start][end] < dist[end]:
                dist[end] = dist[start] + adj_matrix[start][end]
                parent[end] = start

        # 一番近い距離にあるノードを訪問する
        min_cost = float("inf")
        for nextnode in range(n):

            if not visited[nextnode] and dist[nextnode] < min_cost:
                min_cost = dist[nextnode]
                start = nextnode
        if min_cost == float("inf"):
            return parent, dist
        visited[start] = True


def main():
    n = int(input())
    adj_matrix = []
    for _ in range(n):
        row = [-1 for _ in range(n)]
        line = input().split()
        dim = int(line[1])
        for i in range(dim):
            row[int(line[2 + i * 2])] = int(line[2 + i * 2 + 1])
        adj_matrix.append(row)
    _, dist = dijkstra(adj_matrix, n)
    for i in range(n):
        print("{} {}".format(i, dist[i]))


if __name__ == "__main__":
    main()

Python: def diikstraMethd(G, s):
    # ?????????
    n = len(G)
    # White:0 Gyay:1 Black:2
    color = [0 for _ in range(n)]
    d = [INF for _ in range(n)]
    p = [None for _ in range(n)]

    d[s] = 0

    while True:
        mincost = INF
        for i in range(n):
            if color[i] != 2 and d[i] < mincost:
                mincost = d[i]
                u = i

        if mincost == INF: break

        color[u] = 2

        for v in range(n):
            if color[v] != 2 and M[u][v] != INF:
                if d[u] + M[u][v] < d[v]:
                    d[v] = d[u] + M[u][v]
                    p[v] = u
                    color[u] = 1

    return d

if __name__ == '__main__':
    n = int(input())
    M = [[INF for _ in range(n)] for i in range(n)]
    for _ in range(n):
        tmp = list(map(int, input().split()))
        u = tmp.pop(0)
        k = tmp.pop(0)
        for _ in range(k):
            v = tmp.pop(0)
            c = tmp.pop(0)
            M[u][v] = c
    d = diikstraMethd(M, 0)


    for i in range(n):
        print('{0} {1}'.format(i, d[i]))

Python: def shortest_path(V,uvc,start=0):
    distances = [INF]*V
    distances[start] = 0
    parents = list(range(V))

    for _ in range(V):
        modified = False
        for src,dst,cost in uvc:
            if distances[dst] > distances[src] + cost:
                distances[dst] = distances[src] + cost
                parents[dst] = src
                modified = True
        if modified == False:
            return distances,parents
    return None,None
import sys
INF = float("inf")
N = int(sys.stdin.readline())
G = tuple(tuple(map(int,sys.stdin.readline().rstrip().split()))[2:] for _ in range(N))
uvc = []
for i in range(N):
    for j in range(0,len(G[i]),2):
        uvc.append([i,G[i][j],G[i][j+1]])  
a,_ = shortest_path(N,uvc,0)
for i,v in enumerate(a):
    print(i,v)