Go: package main

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

type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }
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 = i
	pq[j].index = j
}
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
}

type Q struct {
	pq *PriorityQueue
	ds map[int]int // It has always same value as pq. For random access.
}

func newQ() *Q {
	pq := new(PriorityQueue)
	heap.Init(pq)
	return &Q{
		pq: pq,
		ds: make(map[int]int),
	}
}
func (q *Q) pop() (u, d int) {
	item := heap.Pop(q.pq).(*Item)
	delete(q.ds, item.value)
	return item.value, item.priority
}
func (q *Q) get(u int) (int, bool) { // returns d
	d, ok := q.ds[u]
	return d, ok
}
func (q *Q) set(u, d int) {
	q.ds[u] = d
	for _, item := range *q.pq {
		if item.value != u {
			continue
		}

		item.priority = d
		heap.Fix(q.pq, item.index)
		return
	}
	heap.Push(q.pq, &Item{
		priority: d,
		value:    u,
	})
}
func (q *Q) len() int {
	return len(q.ds)
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := newQ()
	q.set(0, 0)
	visited := make(map[int]struct{})
	out := make([]int, n)

	for q.len() > 0 {
		u, d := q.pop()
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			dd := d + c
			if d, ok := q.get(uu); !ok || d > dd {
				q.set(uu, dd)
			}
		}

		out[u] = d
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, d)
	}
}

Python: from heapq import heappop, heappush 


if __name__ == '__main__':
	n = int(input())
	graph = [[-1]*n for i in range(n)]
	for i in range(n):
		node_data = list(map(int, input().split()))
		index = node_data[0]
		edge_num = node_data[1]
		node_list = node_data[2:]
		for i in range(edge_num):
			graph[index][node_list[i * 2]] = node_list[i * 2 + 1]

	cost_map = [-1 for i in range(n)]

	heap_q = []
	heap_q.append((0, 0))

	while len(heap_q) != 0:
		node = heappop(heap_q)
		cost = node[0]
		u = node[1]

		if cost_map[u] != -1:
			continue
		cost_map[u] = cost

		for v in range(n):
			if graph[u][v] != -1:
				heappush(heap_q, (graph[u][v] + cost, v))

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

Python: V=int(input())
inf=10**9
cost=[[inf]*V for _ in range(V)]
for i in range(V):
    l=list(map(int,input().split()))
    for j in range(l[1]):
        v,c=l[2*j+2],l[2*j+3]
        cost[i][v]=c
d=[inf]*V
d[0]=0
used=[0]*V

while True:
    v=-1
    for u in range(V):
        if (not used[u] and (v==-1 or d[u]<d[v])):
            v=u
    if v==-1:
        break
    used[v]=1
    for u in range(V):
        d[u]=min(d[u],d[v]+cost[v][u])

for i in range(V):
    print(i,d[i])

Python: import heapq

N = int(input())
A = [[]for i in range(N)]

for i in range(N):
    u, k, *nums = list(map(int,input().split()))
    for j in range(k):
        t = nums[j*2]
        c = nums[j*2+1]
        A[u].append([t, c])

memo = [1E18] * N
memo[0] = 0
qu = []
heapq.heappush(qu,(0,0))
while len(qu) != 0:
    atop = heapq.heappop(qu)
    for ne in A[atop[1]]:
        next_cost = atop[0] + ne[1]
        if(memo[ne[0]] > next_cost):
            memo[ne[0]] = next_cost
            heapq.heappush(qu, (next_cost,ne[0]))
for i in range(N):
    print(i, memo[i])

Python: #!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, maxint


def main():
    num = int(stdin.readline())

    L = []
    for _ in xrange(num):
        L.append([int(s) for s in stdin.readline().split()])

    weight = [maxint] * num
    weight[0] = 0
    V = set(xrange(1, num))
    index = 0

    while V:
        Li = L[index]
        for i in xrange(Li[1]):
            v = Li[i*2+2]
            if v not in V:
                continue
            c = Li[i*2+3]
            weight[v] = min(weight[v], c + weight[index])

        index = min(V, key=lambda i: weight[i])
        V.remove(index)

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


main()

Python: def dijkstra(n):
    distance_list[0] = 0
    while True:
        min_distance = 10**9
        u = -1
        for i in range(n):
            distance = distance_list[i]
            if flag_list[i] < 2 and distance < min_distance:
                min_distance = distance
                u = i
        if u == -1:
            break
        flag_list[u] = 2
        for v in range(n):
            edge = m_list[u][v]
            if flag_list[v] < 2 and -1 < edge:
                if distance_list[u] + edge < distance_list[v]:
                    distance_list[v] = distance_list[u] + edge
                    p[v] = u
                    flag_list[v] = 1


if __name__ == "__main__":
    n = int(input())
    m_list = [[-1]*n  for _ in range(n)]
    for i in range(n):
        in_list = list(map(int, input().split()))
        adj_num = in_list[1]
        tmp = []
        if adj_num > 0:
            for j in range(adj_num):
                m_list[i][in_list[j*2+2]] = in_list[j*2+3]

    flag_list = [0]*n # 0: 未到達, 1: 処理中, 2: 完了
    distance_list = [10**9]*n
    p = [-1]*n
    dijkstra(n)
    for i, d in enumerate(distance_list):
        print(i, d)

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: n = int(input())
G = [[-1] * n] * n
d = [0] * n
 
for i in range(n):
    v = map(int, input().split())
    for j in range(v[1]):
        G[v[0]][v[2+2*j]] = int(v[3+2*j])
 
flag = [0] * n
tn = [1]
r = 0
while True:
    for i in range(n):
        if flag[i] == 0 and i in tn:
            flag[i] = 1
            d[i] = r
            for j in range(n):
                if G[i][j] != -1 and flag[j] == 0:
                    tn.append(j)
    if len(tn) == n:
        break
    r += 1
 
for i in range(1, n+1):
    print(str(i) + " " + str(d[i]))