Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [][]int

func (g *Graph) dikstra(n int) string {
	var (
		u, minv = 0, 0
	)

	d := make([]int, n)
	color := make([]int, n)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		color[i] = WHITE
	}
	d[0] = 0
	color[0] = GRAY

	for {
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for v := 0; v < n; v++ {
			if color[v] != BLACK && (*g)[u][v] != INFTY {
				if d[v] > d[u]+(*g)[u][v] {
					d[v] = d[u] + (*g)[u][v]
					color[v] = GRAY
				}
			}
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		out.WriteString(fmt.Sprintf("%d ", i))
		if d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d\n", -1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d\n", d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make(Graph, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			g[i][j] = INFTY
		}
	}

	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())

		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())

			g[u][v] = c
		}
	}

	return g.dikstra(n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;

    int a[n][n];
    int done[n]; // -1: まだ ソレ以外: 確定距離
    for (int i = 0; i < n; ++i) {
        done[i] = -1;
        for (int j = 0; j < n; ++j) {
            a[i][j] = -1;
        }
    }
    for (int i = 0; i < n; ++i) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; ++j) {
            int v, c;
            cin >> v >> c;
            a[i][v] = c;
        }
    }

    // デバッグ用
//     for (int i = 0; i < n; ++i) {
//         for (int j = 0; j < n; ++j) {
//             cout << i << " " << j << " " << a[i][j] << endl;
//         }
//     }

    // 最初は0が確定済みのノードとなる
    done[0] = 0;

    // 確定済みのノードから行ける、未到達の最小のノードを探す
    // その最小のノードは次の確定済みとする...
    // とりあえずデバッグがてら10回す (最終的にwhileにして条件で対応する)
    // 全てdoneになったら終了
    for (int r = 0; r < n-1; ++r) {
        int next_node = -1;
        int next_cost = 1000000;
        for (int i = 0; i < n; ++i) {
            if (done[i] == -1) {
                // 未確定であれば何もしない
                continue;
            }

            for (int j = 0; j < n; ++j) {
                // 到達済みは見ない
                if (done[j] != -1) {
                    continue;
                }

                // コストが-1なら到達できない
                if (a[i][j] == -1) {
                    continue;
                }

                // 行けるところのコスト (iまでの距離+そこからの距離) を探す
//                cout << "src:" << i << " dst:" << j << " cost:" << a[i][j] << " total:" << done[i] + a[i][j] << endl;

                // 今のnext_costよりもtotalcostが小さければ、次のノードにしたい
                if ((done[i] + a[i][j]) < next_cost) {
                    next_cost = done[i] + a[i][j];
                    next_node = j;
                }
            }
        }
//        cout << "iter:" << r << endl;
//        cout << "next_node:" << next_node << endl;
//        cout << "next_cost:" << next_cost << endl;
        done[next_node] = next_cost;
    }

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

    return 0;
}

C++: #include<stdio.h>
#define MAX 100
#define INFTY (2<<21)
#define WHITE 0
#define GRAY 1
#define BLACK 2

int M[MAX][MAX];
int d[MAX];
int color[MAX];

void dijkstra(int n) {
    int u, mincost;
    d[0] = 0;
    color[0] = GRAY;

    while (true) {
        mincost = INFTY;
        u = -1;

        for (int i=0; i<n; i++) {
            if (color[i]!=BLACK && d[i]<mincost) {
                u = i;
                mincost = d[i];
            }
        }

        if (u==-1) break;
        color[u] = BLACK;

        for (int i=0; i<n; i++) {
            if (color[i]!=BLACK && M[u][i]<INFTY) {
                color[i] = GRAY;
                if (d[u]+M[u][i]<d[i]) {
                    d[i] = d[u] + M[u][i];
                }
            }
        }
    }
}

int main() {
    int n, k, v, c, u;
    scanf("%d", &n);

    for (int i=0; i<n; i++) {
        d[i] = INFTY;
        color[i] = WHITE;
        for (int j=0; j<n; j++) {
            M[i][j] = INFTY;
        }
    }

    for (int i=0; i<n; i++) {
        scanf("%d %d", &u, &k);
        for (int j=0; j<k; j++) {
            scanf("%d %d", &v, &c);
            M[u][v] = c;
        }
    }

    dijkstra(n);

    for (int i=0; i<n; i++) {
        printf("%d %d\n", i, d[i]);
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int map[100][100];
int n;
int d[100];
int color[100];
int INF = 1000000;
void solve() {
	int all = 1;
	d[0] = 0;
	color[0] = 1;
	vector<int>v;
	v.push_back(0);
	while (all != n)
	{
		int min = INF;
		int k;
		for (int i = 0; i < v.size(); i++) {
			for (int j = 0; j < n; j++) {
				if (!color[j] && d[v[i]] + map[v[i]][j] <= d[j]) {
					d[j] = d[v[i]] + map[v[i]][j];
					if (d[j] < min) {
						min = d[j];
						k = j;
					}
				}
			}
		}
		color[k] = 1;
		v.push_back(k);
		all++;
	}
}
int main(void) {
	cin >> n;
	for (int i = 0; i < n; i++) {
		d[i] = INF;
		for (int j = 0; j < n; j++) map[i][j] = INF;
	}
	for (int i = 0; i < n; i++) {
		int k,all;
		cin >> k>>all;
		for (int j = 0; j < all; j++) {
			int a, b;
			cin >> a >> b;
			map[k][a] = b;
		}
	}
	solve();
	for (int i = 0; i < n; i++) {
		cout << i<<" "<<d[i]<<endl;
	}
}

C++: //
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define NIL -1
#define INFTY 1000000
#define TOTAL_INFTY INFTY*MAX_N

using namespace std;

enum Status {
    White,
    Gray,
    Black,
};

int n;
int w[MAX_N][MAX_N];
int d[MAX_N], parent[MAX_N];
Status st[MAX_N];

int min(int x, int y) {
    if(x < y) return x;
    return y;
}

void dijkstra() {
    for(int i=0; i<n; i++) {
        d[i] = TOTAL_INFTY;
        st[i] = White;
        parent[i] = NIL;
    }

    int p = 0;
    d[p] = 0;

    while(true) {
        if(p == -1) break;
        st[p] = Black;

        for(int i=0; i<n; i++) {
            if(w[p][i] != TOTAL_INFTY && st[i] != Black) {
                d[i] = min(d[i], d[p] + w[p][i]);
                st[i] = Gray;
            }
        }

        p = -1;
        int c = TOTAL_INFTY;
        for(int i=0; i<n; i++) {
            if(st[i] == Gray && d[i] < c) {
                p = i;
                c = d[i];
            }
        }
    }
}

int main() {
    for(int i=0; i<MAX_N; i++) {
        for (int j = 0; j < MAX_N; j++) {
            w[i][j] = TOTAL_INFTY;
        }
    }

    cin >> n;

    int u, k, v, c;
    for(int i=0; i<n; i++) {
        cin >> u >> k;
        for(int j=0; j<k; j++) {
            cin >> v >> c;
            w[u][v] = c;
        }
    }

    dijkstra();

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