Go: package main

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

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	route := make([][]Node, n)
	weight := make([]int, n)
	for i := 0; i < n; i++ {
		weight[i] = NIL
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		route[i] = make([]Node, k)
		for j := 0; j < k; j++ {
			route[u][j] = Node{id: scanInt(), weight: scanInt()}
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for _, nextNode := range route[node.id] {
			if weight[nextNode.id] != NIL {
				continue
			}
			nextNode.weight += node.weight
			heap.Push(h, nextNode)
		}
	}

	for i, w := range weight {
		fmt.Println(i, w)
	}
}

Python: n = int(input())

adj = [[]] * n
for i in range(n):
    x = list(map(int, input().split()))
    adj[x[0]] = [c_v for c_v in zip(x[3::2], x[2::2])]

import heapq

def dijkstra():
    PQ = [(0, 0)]
    isVisited = [False] * n
    distance = [999900001] * n

    distance[0] = 0
    
    while PQ:
        uc, u = heapq.heappop(PQ)

        if uc > distance[u]:
            continue

        isVisited[u] = True
        
        for vc, v in adj[u]:
            if isVisited[v] == True:
                continue
            
            t_cost = distance[u] + vc
            if t_cost < distance[v]:
                distance[v] = t_cost
                heapq.heappush(PQ, (t_cost, v))

    for v, d in enumerate(distance):
        print(v, d)


dijkstra()

Python: # -*- coding: utf-8 -*-
from heapq import heapify, heappop, heappush

N = int(input())
INF = float("inf")
d = {n: INF for n in range(N)}
d[0] = 0
adj = [[] for _ in range(N)]

searched = [False for _ in range(N)]
searched[0] = True

for n in range(N):
    inp = tuple(map(int, input().split()))
    assert n == inp[0]
    for i in range(inp[1]):
        adj[n] = list(zip(inp[2::2], inp[3::2]))

heap = [[0, 0]]

while len(heap):
    pcost, p = heappop(heap)
    searched[p] = True
    
    if d[p] < pcost:
        continue
    
    for v in adj[p]:
        if not searched[v[0]]:
            new_cost = pcost + v[1]
            if new_cost < d[v[0]]:
                d[v[0]] = new_cost
                heappush(heap, [d[v[0]], v[0]])

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

Python: import sys
from heapq import*
e=sys.stdin.readline
I=float("inf")
def m():
 n=int(e())
 A=[]
 for _ in[0]*n:
  b=list(map(int,e().split()))
  A+=[zip(b[2::2],b[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: # Single Source Shortest Path 2
from heapq import heappush, heappop

INF = 10**15
N = int(input())
W = [tuple(zip(*[map(int, input().split()[2:])]*2)) for _ in [0]*N]

H = []
checked = [False]*N
D = [INF]*N
D[0] = 0


heappush(H, (D[0], 0))

while H:
    d, v = heappop(H)
    checked[v] = True

    if D[v] < d:
        continue

    for u, w in W[v]:
        if not checked[u]:
            if D[v] + w < D[u]:
                D[u] = D[v] + w
                heappush(H, (D[u], u))


[print(i, d) for i, d in enumerate(D)]

Python: import heapq
N=int(input())
rinsetu=[[] for j in range(N)]
d=[1000000]*(N)

color=["white"]*N
for i in range(N):
    a=list(map(int,input().split()))
    for j in range(a[1]):
        j1=j*2+2
        j2=j*2+3
        rinsetu[i].append([a[j1],a[j2]])
def hunterhunter():
    d[0]=0
    heap=[]
    heapq.heapify(heap)
    heapq.heappush(heap,[0,0])
    while(heap):
        mincost=heapq.heappop(heap)
        u=mincost[1]
        color[u]="black"
        for  i in rinsetu[u]:
            if color[i[0]]=="white":
                if i[1]+d[u]<d[i[0]]:
                    d[i[0]]=i[1]+d[u]
                    heapq.heappush(heap,[d[i[0]],i[0]])
hunterhunter()
for i,cost in enumerate(d):
    print(i,cost)

Python: n = int(input())

adj = [[100001 for j in range(n)] for i in range(n)]

for u in range(n):
    x = list(map(int, input().split()))
    for v, c in zip(x[2::2], x[3::2]):
        adj[u][v] = c

unvisited = [v for v in range(n)]

distance = [9900001] * n


def dijkstra(s):
    distance[s] = 0

    while True:
        mincost = 9900001
        for i in unvisited:
            if distance[i] < mincost:
                mincost = distance[i]
                u = i

        if mincost == 9900001:
            break

        unvisited.remove(u)

        for v in unvisited:
            weight_uv = adj[u][v]
            if weight_uv != 100001:
                t_d = distance[u] + weight_uv
                if t_d < distance[v]:
                    distance[v] = t_d



dijkstra(0)

for v, d in enumerate(distance):
    print(v, d)

Python: import sys

f_i = sys.stdin

n = int(f_i.readline())

adj = [list(map(int, x.split()[2:])) for x in f_i]

unvisited = [v for v in range(n)]

distance = [999900001] * n
distance[0] = 0

class VCost:
    def __init__(self, v, cost):
        self.v = v
        self.cost = cost
    def __lt__(self, other):
        return self.cost < other.cost

import heapq

vcost = [VCost(0, 0)]

def dijkstra():
    while vcost:
        uc = heapq.heappop(vcost)

        if uc.cost > distance[uc.v]:
            continue
        
        unvisited.remove(uc.v)
        
        for v, c in zip(adj[uc.v][::2], adj[uc.v][1::2]):
            if v in unvisited:
                t_cost = uc.cost + c
                if t_cost < distance[v]:
                    distance[v] = t_cost
                    heapq.heappush(vcost, VCost(v, t_cost))



dijkstra()

for v, d in enumerate(distance):
    print(v, d)

Python: import sys

f_i = sys.stdin

n = int(f_i.readline())

adj = [[]] * n
for x in f_i:
    x = list(map(int, x.split()))
    adj[x[0]] = [c_v for c_v in zip(x[3::2], x[2::2])]

import heapq

def dijkstra():
    PQ = [(0, 0)]
    isVisited = [False] * n
    distance = [999900001] * n

    distance[0] = 0
    
    while PQ:
        uc, u = heapq.heappop(PQ)

        if uc > distance[u]:
            isVisited[u] = True
        
            for vc, v in adj[u]:
                if isVisited[v] == False:
                    t_cost = distance[u] + vc
                    if t_cost < distance[v]:
                        distance[v] = t_cost
                        heapq.heappush(PQ, (t_cost, v))

    for v, d in enumerate(distance):
        print(v, d)


dijkstra()

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()