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*
I=float("inf")
def m():
 n=int(input())
 A=[]
 for _ in[0]*n:
  e=list(map(int,input().split()))
  A+=[zip(e[2::2],e[3::2])]
 d=[0]+[I]*n
 H=[(0,0)]
 while H:
  u=heappop(H)[1]
  for v,c in A[u]:
   t=d[u]+c
   if d[v]>t:
    d[v]=t
    heappush(H,(t,v))
 print('\n'.join(f'{i} {d[i]}'for i in range(n)))
m()

Python: from heapq import heappop,heappush
INF = float("inf")
white = 0; gray = 1; black =2

def dijkstra(s):
    # 頂点sをrootに設定
    dist[s] = 0 
    heappush(PQ,(0,s))
    color[s] = gray

    while PQ:
        #最短経路木に頂点(最短経路木とつながる最小コストの辺を持つ頂点)を追加
        mincost,u = heappop(PQ)
        color[u] = black
        if dist[u] < mincost: continue # 最短じゃなかったら無視

        # 追加した頂点uの隣接頂点についてsからの最小コストを更新
        for v,cost in adj[u]:
            if color[v] == black: continue # 確定済み
            if dist[v] > dist[u] + cost:
                dist[v] = dist[u] + cost
                heappush(PQ,(dist[v],v))
                color[v] = gray

if __name__ == "__main__":
    n = int(input())
    adj = [[] for _ in range(n)]
    for i in range(n):
        inp = [int(x) for x in input().split()]
        u = inp[0]
        for v,c in zip(inp[2::2],inp[3::2]): adj[u].append((v,c))
    color = [white]*n #各頂点の訪問状態 （white:未訪問 gray:暫定 black:確定）
    dist = [INF]*n # 始点sからの最小コスト
    PQ = []
    dijkstra(0)
    for i in range(n): print("{} {}".format(i,dist[i]))

Python: import heapq
def Dsa(n):
    inf = 10 ** 6 + 1
    dt = [0] + [inf] * (n - 1)
    q = [(0, 0)]
    while q:
        u = heapq.heappop(q)[1]
        for (v, c) in ed[u]:
            alt = dt[u] + c
            if dt[v] > alt:
                dt[v] = alt
                heapq.heappush(q, (alt, v))
    return dt
 
n = int(input())
ed = [[]] * n
for _ in range(n):
    l = list(map(int, input().split()))
    ed[l[0]] = (e for e in zip(l[2::2], l[3::2]))
for i, c in enumerate(Dsa(n)):
    print(i, c)

Python: import heapq

INF = 1e9

n = int(input())
adj_list = [[]] * n

for u in range(n):
    U = list(map(int, input().split()[2:]))
    adj_list[u] = zip(U[0::2], U[1::2]) # adj_list[u] = (v, cost)


def dijkstra(x):
    d = [INF] * x
    d[0] = 0
    hq = [(0, 0)] # (distance, v)??????????????¬??????????????????(key, ????´?)???????????????push, pop?????????
    
    while hq:
        u = heapq.heappop(hq)[1]
        for v, c in adj_list[u]:
            if d[v] > d[u] + c:
                d[v] = d[u] + c
                heapq.heappush(hq, (d[u] + c, v))
                
    return d

for i, di in enumerate(dijkstra(n)):
    print(i,di)

Python: import heapq as hp

INF = 1000000

def diikstraMethd(G, s):
    # ?????????
    n = len(G)
    d = [INF for _ in range(n)]
    d[s] = 0
    PQ = [(d[s], s)]

    while PQ:
        u = hp.heappop(PQ)[1]
        for v, c in G[u]:
            alt = d[u] + c
            if d[v] > alt:
                d[v] = alt
                hp.heappush(PQ, (alt, v))

    return d


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

    d = diikstraMethd(G, 0)

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

Python: from heapq import heapify, heappush, heappop

# input
n = int(input())

nodes = [[] * n for i in range(n)]
for _ in range(n):
    line = list(map(int, input().split()))
    u = line.pop(0)
    k = line.pop(0)
    for i in range(k):
        v = line.pop(0)
        c = line.pop(0)
        nodes[u].append((v, c))


# 変数定義
inf = 100 * 100000 + 1

d = [inf] * n
V = set([i for i in range(n)]) #nodeの一覧
S = set() #探索済みnodeの一覧

# 探索
start = 0
target = start
d[start] = 0

H = []

#for i in range(n):
#    heappush(H, (i, d[i]))
heappush(H, (start, d[start]))

while V != S:
    u, c = heappop(H)
    S.add(u)
    for v, c in nodes[u]:
        if d[u] + c < d[v]:
            d[v] = d[u] + c
            heappush(H, (v, d[v]))
    
# 出力
for i in range(n):
    print(f"{i} {d[i]}")

Python: import heapq
class Edge:
    src=-1
    dest=-1
    cost=-1

def Dijkstra(graph,s):
    INF=int(1e9)
    d=[INF]*len(graph)
    d[s]=0
    used=[False]*len(graph)
    que=[]
    heapq.heappush(que,(0,s))
    while len(que)!=0:
        val=heapq.heappop(que)
        v=val[1]
        if used[v] : continue
        used[v]=True
        for i in range(0,len(graph[v])):
            e=graph[v][i]
            if d[v]+e.cost<d[e.dest]:
                d[e.dest]=d[v]+e.cost
                heapq.heappush(que,(d[e.dest],e.dest))
    return d

V=int(input())
graph=[[] for i in range(0,V)]
for i in range(0,V):
    vals=list(map(int,input().split(" ")))
    k=vals[1]
    for j in range(0,k):
        v=vals[2+j*2]
        c=vals[3+j*2]
        e=Edge()
        e.src=i
        e.dest=v
        e.cost=c
        graph[i].append(e)

d=Dijkstra(graph,0)
for i in range(0,V):
    print(str(i)+" "+str(d[i]))

Python: import sys
readline = sys.stdin.readline
from heapq import heapify, heappush, heappop
INF = float("inf")
def MAIN():
    n = int(input())
    G = [[i, INF] for i in range(n)]
    G[0][1] = 0
    m = {}
    for _ in range(n):
        A = list(map(int, readline().split()))
        m[A[0]] = {}
        for i in range(2, len(A), 2):
            m[A[0]][A[i]] = A[i + 1]
    dp = [(0, 0)]
    while dp:
        cost, u = heappop(dp)
        for v, c in m[u].items():
            if G[v][1] > G[u][1] + c:
                G[v][1] = G[u][1] + c
                heappush(dp, (G[v][1], v))
    print(*G)
MAIN()