Go: package main

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

type Node struct {
	Id int
	Dis int
}

type PriorityQueue []*Node

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

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].Dis < pq[j].Dis
}

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

func (pq *PriorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.(*Node))
}

func (pq *PriorityQueue) Pop() interface{} {
	n := len(*pq)
	pop := (*pq)[n-1]
	*pq = (*pq)[0:n-1]
	return pop
}

func main() {
	var n int
	fmt.Scan(&n)

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

	g := make([][]int, n)
	c := make([][]int, n)
	pq := make(PriorityQueue, 0, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		g[u] = make([]int, k)
		c[u] = make([]int, k)
		for j := 0; j < k; j++ {
			sc.Scan()
			g[u][j], _ = strconv.Atoi(sc.Text())
			sc.Scan()
			c[u][j], _ = strconv.Atoi(sc.Text())
		}

		if u == 0 {
			d[u] = 0
		} else {
			d[u] = 1 << 30
		}
		pq.Push(&Node{u, d[u]})
	}
	heap.Init(&pq)

	for pq.Len() > 0 {
		node := heap.Pop(&pq).(*Node)
		for i := 0; i < len(g[node.Id]); i++ {
			nextId := g[node.Id][i]
			nextDis := node.Dis + c[node.Id][i]
			if nextDis < d[nextId] {
				d[nextId] = nextDis
				heap.Push(&pq, &Node{nextId, nextDis})
			}
		}
	}

	wr := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		wr.WriteString(strconv.Itoa(i))
		wr.WriteString(" ")
		wr.WriteString(strconv.Itoa(d[i]))
		wr.WriteString("\n")
	}
	wr.Flush()
}

Python: import heapq

n = int(input())
maxim = 100010
adj = [[] for _ in range(n)]

for _ in range(n):
    u, k, *v = map(int, input().split())
    if k == 0:
        adj[u].append((0,0))
    for i in range(k):
        adj[u].append((v[i*2+1], v[i*2]))

def dijkstra():
    global adj
    PQ = []
    heapq.heapify(PQ)
    color = ['W']*n
    d = [maxim]*n
    d[0] = 0
    heapq.heappush(PQ, (0, 0))
    color[0] = 'G'

    while PQ:
        f = heapq.heappop(PQ)
        u = f[1]
        color[u] = 'B'
        if d[u] < f[0]*-1:
            continue
        for j in range(len(adj[u])):
            v = adj[u][j][1]
            if color[v] == 'B':
                continue
            if d[v] > d[u]+adj[u][j][0]:
                d[v] = d[u]+adj[u][j][0]
                heapq.heappush(PQ, (d[v], v))
                color[v] = 'G'
    return d

D = dijkstra()
for i, x in enumerate(D):
    print(i, x)

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: import heapq
WHITE = 0
GRAY = 1
BLACK = 2
INFTY = 10**13
def dijkstra(s):
    color = [WHITE for i in range(N)]
    d = [INFTY for i in range(N)]
    d[s] = 0
    pq = []
    heapq.heappush(pq, (0, s))

    while len(pq) != 0:
        u = heapq.heappop(pq)[1]
        color[u] = BLACK

        for v, c in VC[u]:
            if color[v] != BLACK:
                if d[u] + c < d[v]:
                    d[v] = d[u] + c
                    color[v] = GRAY
                    heapq.heappush(pq, (d[v], v))

    return d

N = int(input())
VC = [[] for i in range(N)]
for i in range(N):
    ukvc = list(map(int, input().split()))
    u, k, vc = ukvc[0], ukvc[1], ukvc[2:]
    for v, c in zip(vc[0::2], vc[1::2]):
        VC[u].append([v, c])

d = dijkstra(0)

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

Python: V=int(input())
 
G=[[] for i in range(V)]
d=[1001001001 for i in range(V)]
used=[False for i in range(V)]
 
import heapq
 
def dijkstra(s):
    q=[]
    d[s]=0
    heapq.heappush(q,(0, s))
     
    while(len(q)):
         
        p=heapq.heappop(q)
        v=p[1]
        used[v]=True
        if d[v]<p[0]:
            continue
        for e in G[v]:
            if (not used[e[0]]) and d[e[0]]>d[v]+e[1]:
                d[e[0]]=d[v]+e[1]
                 
                heapq.heappush(q, (d[e[0]], e[0]))
 
import sys
for l in sys.stdin:
    l=list(map(int,l.split()))
    u=l[0]
    k=l[1]
    G[u]=zip(*[iter(l[2:])]*2)
     
dijkstra(0)
for i, c in enumerate(d):
    print(i, d[i])

Python: #single source shotest path 2

from heapq import heappush, heappop

n = int(input())
a = [None]*n
for i in range(n):
    u, k, *V = map(int, input().split())
    a[i] = [(p, q) for p, q in zip(V[::2], V[1::2])]

dist = [10**6]*n
dist[0] = 0
que = []
heappush(que,(0,0))

while que:
    cost, v = heappop(que)
    for w, c in a[v]:
        if cost + c < dist[w]:
            dist[w] = r = cost + c
            heappush(que, (r,w))
            
for i in range(n):
    print(f"{i} {dist[i]}")

Python: from heapq import *
h = []




INF = 1e9

n = int(input())
a = []
for i in range(n):
    b = list(map(int, input().split()))
    index = b[0]
    k = b[1]
    tmp = []
    for j in range(k):
        tmp.append(b[2*j+2:2*j+4])
    a.append(tmp)


def dijkstra(s):
    d = [INF] * n
    d[s] = 0

    heappush(h, (d[s], s))

    while h:
        pop = heappop(h)
        if pop[0] == INF:
            continue
        for node in a[pop[1]]:
            if d[pop[1]] + node[1] < d[node[0]]:
                d[node[0]] = d[pop[1]] + node[1]
                heappush(h, (d[pop[1]] + node[1], node[0]))

    return d
d = dijkstra(0)
for i in range(n):
    print(i, d[i])

Python: # coding: utf-8

import heapq


class Dijkstra:

    """
    Dijkstra's algorithm: find the shortest path from a vertex
    O(E + log(V))
    verified:
        https://onlinejudge.u-aizu.ac.jp/#/courses/lesson/1/ALDS1/12/ALDS1_12_B
    """

    def __init__(self, V, G, INF=float("inf")):

        """
        V: the number of vertexes
        E: adjacency list (all edge in G must be 0 or positive)
        INF: Infinity distance
        """
        self.V = V
        self.G = G
        self.INF = INF
        self.distance, self.prev = list(), list()


    def dijkstra(self, start):

        Q = list()
        self.distance = [self.INF for _ in range(self.V)]
        self.prev = [-1 for _ in range(self.V)]

        self.distance[start] = 0
        heapq.heappush(Q, (0, start))

        while len(Q) > 0:
            dist, from_v = heapq.heappop(Q)
            if self.distance[from_v] < dist:
                continue
            for to_v, nxt_dist in self.G[from_v]:
                if self.distance[from_v] + nxt_dist >= self.distance[to_v]:
                    continue
                self.distance[to_v] = self.distance[from_v] + nxt_dist
                heapq.heappush(Q, (self.distance[to_v], to_v))
                self.prev[to_v] = from_v
        
        return self.distance


    def get_path(self, goal):

        path = [goal]
        while self.prev[goal] != -1:
            path.append(prev[goal])
            goal = self.prev[end]

        return path[::-1]


if __name__ == "__main__":
    
    """
    an example of use
    """

    n = int(input())
    g = [[] for _ in range(n)]
    for _ in range(n):
        v, _, *e = map(int, input().split())
        for u, d in zip(e[::2], e[1::2]):
            g[v].append((u, d))
 

    dist = Dijkstra(n, g).dijkstra(0)
    for i, d in enumerate(dist):
        print(i, d)