Go: // ダイクストラのアルゴリズム
package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	num     int         // 頂点の番号
	weights map[int]int // 重みMAP
	parent  *node       // MSTへの重み最小の親
	sum     int         // 始点からの距離
	color   color
}

func (p *node) getMinToMst() int {
	if p.parent == nil {
		return math.MaxInt32
	}
	return p.weights[p.parent.num]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]*node, n)
	for i := 0; i < n; i++ {
		weights := eAtoi((strings.Fields(eGetline(scanner))))
		v := &node{weights[0], make(map[int]int, weights[1]), nil, 0, white}
		for j := 0; j < weights[1]; j++ {
			k := weights[j*2+2] // 相手の頂点名
			w := weights[j*2+3] // 重み
			v.weights[k] = w
		}
		nodes[i] = v
	}

	// 初回はnode[0]がMSTに入るように
	nodes[0].parent = nodes[0]

	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか

		var minNode *node
		min := math.MaxInt32
		for _, v := range nodes {
			if v.color == mst {
				continue
			}
			if v.getMinToMst() < min {
				min = v.getMinToMst()
				minNode = v
			}
		}
		if minNode == nil {
			// すべて完了している.
			break
		}

		// MSTに追加する頂点決定
		minNode.color = mst
		// 自分の親の合計値と合算する
		minNode.sum = minNode.parent.sum + min

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みの距離を加算
		for _, v := range nodes {
			if v.color == mst {
				continue
			}
			w, ok := v.weights[minNode.num]
			if !ok {
				// minNodeと接してない
				continue
			}

			if w < v.getMinToMst() {
				// minNodeへの距離がもっとも重みが少ないので親を変更
				v.parent = minNode
			}
		}
	}

	for i, v := range nodes {
		fmt.Printf("%d %d\n", i, v.sum)
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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

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 = [0]
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(n):
    print(str(i) + " " + str(d[i]))