Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
const INF = 1 << 60

type Edge struct {
	to, cost int
}

type Node struct {
	id, cost int
}

type PQ []Node

func (pq PQ) Len() int           { return len(pq) }
func (pq PQ) Less(i, j int) bool { return pq[i].cost < pq[j].cost }
func (pq PQ) Swap(i, j int)      { pq[i], pq[j] = pq[j], pq[i] }

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

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

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	E := make([][]Edge, N)
	for i := 0; i < N; i++ {
		id, k := sc.nextInt(), sc.nextInt()
		E[id] = make([]Edge, k)
		for j := 0; j < k; j++ {
			to, cost := sc.nextInt(), sc.nextInt()
			E[id][j] = Edge{to, cost}
		}
	}

	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	done := make([]bool, N)
	pq := &PQ{}
	heap.Init(pq)
	d[0] = 0
	heap.Push(pq, Node{0, 0})
	for pq.Len() > 0 {
		cur := heap.Pop(pq).(Node).id
		if done[cur] {
			continue
		}
		done[cur] = true
		for i := 0; i < len(E[cur]); i++ {
			to, cost := E[cur][i].to, E[cur][i].cost+d[cur]
			if !done[to] && cost < d[to] {
				d[to] = cost
				heap.Push(pq, Node{to, cost})
			}
		}
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i, d[i])
	}
	wtr.Flush()
}

C++: #include <bits/stdc++.h>

using namespace std;

vector<pair<int, int>> adja[10000];

#define INF (1 << 30)

int main() {
	int n, id, k, a, b;

	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> id >> k;
		for (int j = 0; j < k; ++j) {
			cin >> a >> b;
			adja[id].push_back(make_pair(a, b));
		}
	}

	priority_queue<pair<int, int>> que;
	que.push(make_pair(0, 0)); //cost, node
	int cost[10000];
	fill(cost, cost + n, INF);

	while (!que.empty()) {
		for (int i = 0, size = que.size(); i < size; ++i) {
			pair<int, int> top = que.top();
			que.pop();

			if (cost[top.second] < top.first) {
				continue;
			}
			cost[top.second] = top.first;

			for (int j = 0, len = adja[top.second].size(); j < len; ++j) {
				que.push(make_pair(top.first + adja[top.second][j].second, adja[top.second][j].first));
			}
		}
	}

	for (int i = 0; i < n; ++i) {
		cout << i << " " << cost[i] << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
int n,H[100][100];
void dd()
{
    int minv;
    int d[100],c[100];
    for(int i=0;i<n;i++)
    d[i]=INFTY;
    d[0]=0;
    while(1)
    {
        minv=INFTY;
        int u=-1;
        for(int i=0;i<n;i++)
        {
            if(minv>d[i]&&c[i]!=BLACK)
            {
                u=i;
                minv=d[i];
            }
        }
        if(u==-1) break;
        c[u]=BLACK;
        for(int v=0;v<n;v++)
        {
            if(c[v]!=BLACK&&H[u][v]!=INFTY)
            {
                if(d[v]>d[u]+H[u][v])
                {
                    d[v]=d[u]+H[u][v];
                    c[v]=GRAY;
                }
            }
        }
    }
    for(int i=0;i<n;i++)
            printf("%d %d\n",i,(d[i]==INFTY?-1:d[i]));
}
int main()
{
    int a,b,c,d;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
        H[i][j]=INFTY;
    for(int i=0;i<n;i++)
    {
        scanf("%d %d",&a,&b);
        for(int j=0;j<b;j++)
        {
            scanf("%d %d",&c,&d);
            H[a][c]=d;
        }
    }
    dd();
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX=10000;
static const int INFTY=2000000000;

vector<pair<int,int> > A[MAX];
int dk[MAX],H[MAX+1],tail,where[MAX];

void cminHeap(int i){
    int left,right,least;
    left=2*i;right=left+1;least=i;
    if(left<=tail&&dk[H[left]]<dk[H[i]])least=left;
    if(right<=tail&&dk[H[right]]<dk[H[least]])least=right;
    if(i==least)return;
    swap(H[i],H[least]);swap(where[H[i]],where[H[least]]);
    cminHeap(least);
}

void pminHeap(int i){
    int parent=i/2;
    if(parent==0||dk[H[parent]]<=dk[H[i]])return;
    swap(H[parent],H[i]);swap(where[H[parent]],where[H[i]]);
    pminHeap(parent);
}

void heap_update(int u){
    int x=H[u];
    cminHeap(u);
    if(H[u]==x)pminHeap(u);
}

void SSSP(int n){
    for(int i=0;i<n;i++){
        dk[i]=INFTY;
        H[i+1]=i;
        where[i]=i+1;
    }
    dk[0]=0;
    tail=n;
    int v;
    while(tail>0){
        v=H[1];H[1]=H[tail];where[H[tail]]=1;
        tail--;where[v]=-1;
        cminHeap(1);
        vector<pair<int,int> >::iterator it;
        for(it=A[v].begin();it<A[v].end();it++){
            pair<int,int> item=*it;
            if(where[item.first]!=-1){
                if(dk[item.first]>dk[v]+item.second){
                    dk[item.first]=dk[v]+item.second;
                    heap_update(where[item.first]);
                }
            }
        }
    }
}

int main(){
    int n,v,k,u,d;
    cin>>n;
    for(int i=0;i<n;i++){
        scanf("%d %d",&v,&k);
        for(int j=0;j<k;j++){
            scanf("%d %d",&u,&d);
            A[v].push_back(make_pair(u,d));
        }
    }
    SSSP(n);
    for(int i=0;i<n;i++)printf("%d %d\n",i,dk[i]);
}