Go: package main

import (
	"strconv"
	"fmt"
	"bufio"
	"os"
)
const INF = 1 << 21
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}


func main() {
	scanner.Split(bufio.ScanWords)
	n := nextInt()
	adj := make([][]int, n)
	for i := range adj {
		adj[i] = make([]int, n)
	}

	for i := 0;i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			v := nextInt()
			c := nextInt()
			adj[u][v] = c
		}
	}

	// for i := range adj {
	// 	fmt.Println(adj[i])
	// }

	dist := make([]int, n)
	prev := make([]int, n)
	visited := make([]bool, n)
	dijkstra := func(start int, size int) {
		for i := 0; i < size; i++ {
			dist[i] = INF
			prev[i] = -1
			visited[i] = false
		}
		dist[start] = 0
		for {
			u := -1
			tmp_min := INF
			for i := 0; i < size; i++ {
				if visited[i] == false && dist[i] < tmp_min {
					tmp_min = dist[i]
					u = i
				}
			}
			if u == -1 {
				break
			}
			visited[u] = true
			for i := 0; i < size; i++ {
				if adj[u][i] > 0 {
					w := adj[u][i]
					newlen := dist[u] + w
					if newlen < dist[i] {
						dist[i] = newlen
						prev[i] = u
					}
				}
			}
		}
	}
	dijkstra(0, n)
	for i, cost := range dist {
		fmt.Printf("%d %d\n", i, cost)
	}
}

Python: #coding: utf-8

inf = 1000000000


def dijkstra(s):
    d = [inf for i in range(n)]
    color = ["white" for i in range(n)]
    p = [-1 for i in range(n)]
    d[s] = 0

    while True:
        mincost = inf
        for i in range(n):
            if color[i] != "black" and d[i] < mincost:
                mincost = d[i]
                u = i
        if mincost == inf:
            break

        color[u] = "black"

        for v in range(n):
            if color[v] != "black" and mtrx[u][v] != False:
                if d[u] + mtrx[u][v] < d[v]:
                    d[v] = d[u] + mtrx[u][v]
                    p[v] = u
                    color[v] = "gray"
    return d

n = int(input())
mtrx = [[False for j in range(n)] for i in range(n)]
for i in range(n):
    data = list(map(int,input().split()))
    k = data[1]
    for num in range(k):
        mtrx[i][data[2+2*num]] = data[2+2*num+1]

d = dijkstra(0)

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

Python: # ALDS1_12_V Single Source Shortest Path I
import sys

n = int(input())
G = [[float('inf')] * n for _ in range(n)]
for i in range(n):
    ukvc = list(map(int, sys.stdin.readline().strip().split()))
    u = ukvc[0]
    k = ukvc[1]

    for j in range(k):
        G[u][ukvc[2 + 2 * j]] = ukvc[3 + 2 * j]

p = [n] * n  # 親を保存するリスト　親ノードとしてあり得ない n を初期リストとする
c = ['white'] * n # 最小経路木への接続チェック　white 未接続　black 接続済
d = [float('inf')] * n  # ノード i までの最小経路を保存するリスト
d[0] = 0  # 根への距離は0とする
p[0] = -1  # 根の親は -1 とする
u = 0  # 次のループで最小経路木につなげるノード

while 'white' in c:
    c[u] = 'black'

    v = None
    minpath = float('inf')
    for i in range(n):
        d[i] = min(d[i], d[u] + G[u][i])
        if (d[i] < minpath) & (c[i] == 'white'):
            minpath = d[i]
            v = i
            p[i] = u

    if v is None:
        break

    u = v

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

Python: def dijkstra(n,s):
    inf=float("inf")
    color=["white" for i in range(n)]
    D=[inf for i in range(n)]
    P=[0 for i in range(n)]
    M=[[inf for i in range(n)] for j in range(n)]

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

    D[s]=0
    P[s]=-1

    while 1:
        cost,u=inf,0
        for i in range(n):
            if color[i]!="black" and D[i]<cost:
                cost=D[i]
                u=i

        if cost==inf:break

        color[u]="black"

        for i in range(n):
            if color[i]!="black" and M[u][i]!=inf:
                if D[u]+M[u][i]<D[i]:
                    D[i]=D[u]+M[u][i]
                    color[i]="gray"
                    P[i]=u

    return D


n=int(input())
distant=dijkstra(n,0)
for i in range(n):
    print(i,distant[i])

Python: def dijkstra():
    d[0] = 0
    color[0] = GRAY

    while True:
        minv = INF
        u = -1
        for i in range(N):
            if minv > d[i] and color[i] != BLACK:
                u = i
                minv = d[i]
        if u == -1:
            break
        color[u] = BLACK
        for i in range(N):
            if color[i] != BLACK and M[u][i] != INF:
                if d[i] > d[u] + M[u][i]:
                    d[i] = d[u] + M[u][i]
                    color[i] = GRAY

INF = 10 ** 18
WHITH = 0
GRAY = 1
BLACK = 2
N = int(input())
M = [[INF for _ in range(N)] for _ in range(N)]
d = [INF] * N
color = [WHITH] * N

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

dijkstra()
for i in range(N):
    v = d[i]
    if v == INF:
        v = -1
    print(i, v)

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	single_source_shortest_path
# CreatedDate:  2020-06-08 21:54:58 +0900
# LastModified: 2020-06-08 22:28:47 +0900
#


import os
import sys
from collections import deque
#import numpy as np
#import pandas as pd


def main():
    n = int(input())
    graph = [[] for _ in range(n)]
    for _ in range(n):
        line = list(map(int,input().split()))
        u,k = line[0],line[1]
        path = line[2:]
        for i in range(0,2*k,2):
            graph[u].append([path[i+1],path[i]])
    
#    print(graph)
    s=0
    distance=[-1]*n
    Q = deque([s])
    distance[0]=0
    

    while Q:
        u = Q.popleft()
        for dis,v in graph[u]:
            if distance[v]==-1:
                distance[v]=distance[u]+dis
                Q.append(v)

            if distance[v]>distance[u]+dis:
                distance[v]=distance[u]+dis
                Q.append(v)
            


    for i,dis in enumerate(distance):
        print(i,dis)


if __name__ == "__main__":
    main()

Python: INF = 100000 * 100

def dijkstra(s: int) -> None:
	d[s] = 0	
	while True:
		mincost = INF
		for i in range(n):
			if color[i] != "BLACK" and d[i] < mincost:
				mincost = d[i]
				u = i
		if mincost == INF:
			break
		color[u] = "BLACK"
		for v in range(n):
			if color[v] != "BLACK" and M[u][v] != None:
				if d[u] + M[u][v] < d[v]:
					d[v] = d[u] + M[u][v]
					pi[v] = u
					color[v] = "GRAY"


if __name__ == "__main__":
	n = int(input())
	G = []
	M = [[None for _ in range(n)] for _ in range(n)]
	color = ["WHITE" for _ in range(n)]
	d = [INF for _ in range(n)]
	pi = [None for _ in range(n)]

	for _ in range(n):
		l = list(map(int, input().split()))
		u = l[0]
		k = l[1]
		for i in range(k):
			v = l[2 * (i + 1)]
			c = l[2 * (i + 1) + 1]
			M[u][v] = c
	dijkstra(0)
	for v in range(n):
		print(f"{v} {d[v]}")