Go: package main

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

//make priority queue
type Item struct {
	id       int
	priority int // this state is priority and distans from start
	index    int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

// we want the ascending-priority-queue.
// So we use less than here.
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = j
	pq[j].index = i
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(dist int, id int) {
	for _, i := range *pq {
		if i.id == id {
			if i.priority > dist {
				i.priority = dist
			}
		}
	}
}

// the main code
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	return n
}

func dikstra(dist [][]int, n int) []int {
	// preparate priority queue
	inf := 1 << 30
	pq := make(PriorityQueue, n)
	shortests := make([]int, n)
	for i := 0; i < n; i++ {
		pq[i] = &Item{
			id:       i,
			priority: inf,
			index:    i,
		}

		shortests[i] = inf
	}
	pq[0].priority = 0
	shortests[0] = 0
	heap.Init(&pq)

	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		shortests[item.id] = item.priority
		for id, d := range dist[item.id] {
			if d != -1 {
				pq.update(item.priority+d, id)
			}
		}
		heap.Init(&pq)
	}
	return shortests
}

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

	// init
	n := nextInt(sc)
	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dist[i][j] = -1
		}
	}

	// std Input
	for i := 0; i < n; i++ {
		u := nextInt(sc)
		k := nextInt(sc)
		for j := 0; j < k; j++ {
			v, c := nextInt(sc), nextInt(sc)
			dist[u][v] = c
		}
	}

	shortests := dikstra(dist, n)
	for i, d := range shortests {
		fmt.Printf("%d %d\n", i, d)
	}
}

Python: INFTY = 1 << 21
WHITE = 0
GRAY = 1
BLACK = 2
 
n = int(input())
M = [list(map(int, input().split())) for i in range(n)]
 
d = [INFTY for i in range(n)]
color = [INFTY for i in range(n)]
 
for i in range(n):
    u = int(input())
    k = int(input())
    for j in range(k):
        v = int(input())
        c = int(input())
        M[u][v] = c
        
def dijkstra():
    global M, d, p, color
    d[0] = 0
    color[0] = GRAY
    while(1):
        minv = INFTY
        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 v in range(n):
            if (color[v] != BLACK and M[u][v] != INFTY):
                if (d[v] > d[u] + M[u][v]):
                    d[v] = d[u] + M[u][v]
                    color[v] = GRAY
    for i in range(n):
        print(str(i) + " " + str(d[i]))
print(dijkstra())

Python: # Graph II - Single Source Shortest Path I
def dijkstra(length, adj_m):
    INFTY = float('inf')
    d = [INFTY for _ in range(length)]
    d[0] = 0
    Q = [i for i in range(length)]
    while Q:
        idx = INFTY
        minv = INFTY
        for q in Q:
            if d[q] < minv:
                idx, minv = q, d[q]
        u = Q.pop(Q.index(idx))
        for i, v in enumerate(adj_m[u]):
            if not v == -1:
                if d[i] > d[u] + v:
                    d[i] = d[u] + v
    return d

n = int(input())
adj_m = [[-1 for _ in range(n)] for _ in range(n)]
for _ in range(n):
    ss = list(map(int, input().split()))
    u, k = ss[0], ss[1]
    for i in range(k):
        vi,ci = ss[2*i+2], ss[2*i+3]
        adj_m[u][vi] = ci
d = dijkstra(n, adj_m)
for i,v in enumerate(d): print(i,v)

Python: import argparse
import sys
inf = sys.maxsize
import heapq

def dijkstra_heap(edge_list, node):    
    distance_list = n_node * [inf]
    distance_list[node] = 0
    heap = []
    heapq.heappush(heap, (0, 0))

    while(heap):
        temp_dist, from_node = heapq.heappop(heap)
        if distance_list[from_node] < temp_dist:
            continue
        
        for to_node, cost in edge_list[from_node].items():
            if distance_list[to_node] > distance_list[from_node] + cost:
                distance_list[to_node] = distance_list[from_node] + cost
                heapq.heappush(heap, (distance_list[to_node], to_node))
    
    return distance_list


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', type=str)
    args = parser.parse_args()
    edge_input = []
    if args.input:
        f = open(args.input, 'r')
        n_node = int(f.readline())                
        for _ in range(n_node):            
            edge_input.append(tuple(map(int, f.readline().split()))[2:])
        f.close()
    else:
        n_node = int(input())
        for i in range(n_node):            
            edge_input.append(tuple(map(int, input().split()))[2:])
    
    edge_list = [None] * n_node
    for i in range(n_node):
        edge_list[i] = {}
        for j in range(0, len(edge_input[i]), 2):
            edge_list[i][edge_input[i][j]] = edge_input[i][j+1]

    distance_list = dijkstra_heap(edge_list, 0)

    # # bellman_ford(edge_list, 0)
    # distance_list = dijkstra(edge_list, 0)
    for i, ditance in enumerate(distance_list):
        print(i, distance_list[i])

Python: import sys
input = sys.stdin.readline


def dijkstra_v2(start: int, matrix: list) -> list:
    """dijkstra法: 始点startから各頂点への最短距離を求める
    計算量: O(V^2)
    """
    INF = 10 ** 18
    n = len(matrix)
    used = [False] * n
    dist = [INF] * n
    dist[start] = 0
    while True:
        v = -1
        for u in range(n):
            if not used[u] and (v == -1 or dist[u] < dist[v]):
                v = u
        if v == -1:
            break
        used[v] = True
        for nxt_v in range(n):
            dist[nxt_v] = min(dist[nxt_v], dist[v] + matrix[v][nxt_v])
    return dist
  

n = int(input())
info = [list(map(int, input().split())) for i in range(n)]
INF = 10 ** 18

matrix = [[INF] * n for i in range(n)]
for a in range(n):
    matrix[a][a] = 0
    for j in range(1, len(info[a]) // 2):
        b, cost = info[a][slice(2 * j, 2 * (j + 1))]
        matrix[a][b] = cost

dist = dijkstra_v2(0, matrix)
for v, ans in enumerate(dist):
    print(v, ans)

Python: #!/usr/bin/python3
from heapq import heappop, heappush
n = int(input())
inf = 100 * 100000

M = [[] for i in range(n)]

for i in range(n):
    L = list(map(int, input().split()))
    u = L[0]
    k = L[1]
    v = L[2::2]
    c = L[3::2]
    for j in range(k):
        M[u].append((v[j], c[j]))
result = [0] * n
S = set()
V = set(range(n))
S.add(0)
d = []
for m in M[0]:
    heappush(d, (m[1], m[0]))
T = V - S

while len(T) != 0:
    c, v = heappop(d)
    while v in S:
        c, v = heappop(d)
    result[v] = c
    S.add(v)
    T.remove(v)
    for m in M[v]:
        if m[0] in T:
            heappush(d, (result[v] + m[1], m[0]))

for i, v in enumerate(result):
    print(i, v)