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: n = int(input())
a = []
for i in range(n):
    b = list(map(int, input().split()))
    c = [-1] * n
    for j in range(b[1]):
        c[b[2 + j * 2]] = b[3 + j * 2]
    a.append(c)

m = [float('inf')] * n
f = [0] * n
m[0] = 0
f[0] = 1
v = 0

for i in range(n-1) :
    for j in range(n) :
        if (a[v][j] + m[v] < m[j]) & (f[j] == 0) & (a[v][j] != -1):
            m[j] = a[v][j] + m[v]
    
    min = 2001
    minj = 0
    for j in range(n) :
        if (m[j] < min) & (f[j] == 0):
            min = m[j]
            minj = j
    
    v = minj
    f[minj] = 1

[print(i, m[i]) for i in range(n)]

Python: from collections import deque
import sys
import heapq
n = int(input())
V = [[int(i) for i in input().split()] for _ in range(n)]
dis = [sys.maxsize for _ in range(n)]
dis[0] = 0
h = []
for i in range(n):
    heapq.heappush(h,(dis[i],i))
while(len(h)>0):
    node = heapq.heappop(h)
    tmp = node[1]
    for i in range(V[tmp][1]):
        ind = V[tmp][2*i+2]
        cos = V[tmp][2*i+3]
        if cos + dis[tmp] < dis[ind]:
            dis[ind] = cos + dis[tmp]
            heapq.heappush(h,(dis[ind],ind))
for i,j in enumerate(dis):
    print(i,j)

Python: import heapq

def main():

    INF = 1000000009
    n = int(input())
    e = [[] for _ in range(n)]
    for i in range(n):
        inf = list(map(int,input().split()))
        for j in range((inf[1])):
            e[inf[0]].append([inf[2+j*2],inf[2+j*2+1]])
    
    dp = [INF]*n
    dp[0] = 0
    pq = [[0,0]]
    heapq.heapify(pq)
    while pq:
        cost,v = heapq.heappop(pq)
        if dp[v]<cost:continue

        for i in range(len(e[v])):
            u = e[v][i][0]
            ncost = cost+e[v][i][1]
            if ncost < dp[u]:
                dp[u] = ncost
                heapq.heappush(pq,[ncost,u])
    for i in range(n):
        print(i,dp[i])


if __name__ == '__main__':
    main()

Python: n = int(raw_input())
G = [[-1] * n] * n
d = [0] * n
 
for i in range(n):
    v = map(int, raw_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]))

Python: n = int(input())
data = [list(map(int, input().split())) for _ in range(n)]
INF = 10000000000000000000
M = [[-1]*n for _ in range(n)]
for i in data:
    for j in range(i[1]):
        M[i[0]][i[2+j*2]] = i[3+j*2]
color = ['White']*n
d = [INF]*n
p = [None]*n

d[0] = 0
p[0] = -1

while True:
    minct = INF
    for i in range(n):
        if color[i] != 'Black' and d[i] < minct:
            minct = d[i]
            u = i
    if minct == INF:
        break
    color[u] = 'Black'
    for j in range(n):
        if color[j] != 'Black' and M[u][j] != -1:
            if d[u] + M[u][j] < d[j]:
                d[j] = d[u] + M[u][j]
                p[j] = u
                color[j] = 'Gray'
for i in range(n):
    print('{} {}'.format(i, d[i]))

Python: #coding:utf-8

inf = 1000000
n = int(input())
M = [[inf for i in range(n)] for j in range(n)]

for i in range(n):
    data = list(map(int,input().split()))
    u = data[0]
    k = data[1]
    for j in range(0,2*k,2):
        v, w = data[j+2], data[j+3]
        M[u][v] = w

def dijkstra(s):
    color = ["white" for i in range(n)]
    d = [inf for i in range(n)]
    p = [-1 for i in range(n)]
    d[s] = 0
    while True:
        mincost = inf
        for i in range(n):
            if color[i] != "black" and d[i] < mincost:
                mincost = d[i]
                u = i
        if mincost == inf:
            break
        color[u] = "black"

        for v in range(n):
            if color[v] != "black" and M[u][v] != inf:
                if d[u] + M[u][v] < d[v]:
                    d[v] = d[u] + M[u][v]
                    p[v] = u
                    color[v] = "gray"
    return d

dList = dijkstra(0)
for i, d in enumerate(dList):
    print(i,d)

Python: import sys
readline = sys.stdin.readline
write = sys.stdout.write

N = int(readline())

G = [None]*N
W = 0
for i in range(N):
    j, k, *V = map(int, readline().split())
    G[i] = [(v, c) for v, c in zip(V[::2], V[1::2])]
    if V:
        W = max(W, max(V[1::2]))

def dial(N, G, W, s):
    B = [-1]*(N*W + 1)
    L = [-1]*(N*W + 1)
    *prv, = range(-1, N-1)
    *nxt, = range(1, N+1)
    nxt[-1] = -1
    prv[s+1] = s-1
    nxt[s-1] = (s+1 if s < N-1 else -1)
    prv[s] = nxt[s] = -1
    B[0] = L[0] = s
    B[N*W] = +(s == 0)
    L[N*W] = (N-1 if s < N-1 else N-2)

    dist = [N*W] * N
    dist[s] = 0
    for w in range(N*W):
        v = B[w]
        while v != -1:
            for x, c in G[v]:
                e = w + c
                if e < dist[x]:
                    d = dist[x]
                    dist[x] = e

                    p = prv[x]; n = nxt[x]
                    if p != -1:
                        nxt[p] = n
                    else:
                        B[d] = n
                    if n != -1:
                        prv[n] = p
                    else:
                        L[d] = p

                    l = L[e]
                    if l != -1:
                        nxt[l] = x
                    else:
                        B[e] = x
                    prv[x] = l; nxt[x] = -1
                    L[e] = x
            v = nxt[v]
    return dist
dist = dial(N, G, W, 0)
ans = []
for i in range(N):
    ans.append("%d %d\n" % (i, dist[i]))
sys.stdout.writelines(ans)