Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := makeGraph(n)
	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			c := getNextInt(scanner)
			g[u][v] = c
		}
	}

	ans := make([]int, n)
	im := make(IntMap, 1)
	im.increment(0)
	for len(im) < n {
		min := 1 << 30
		x := -1
		for u := range im {
			for v := 0; v < n; v++ {
				if im.get(v) != 0 {
					continue
				}
				if min > ans[u]+g[u][v] {
					min = ans[u] + g[u][v]
					x = v
				}
			}
		}
		im.increment(x)
		ans[x] = min
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i, ans[i]))
	}
	writer.Flush()
}

func makeGraph(n int) [][]int {
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = 1 << 30
		}
	}

	return g
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

C++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int main()
{
    int n;
    cin >> n;
    int m[n][n];
    rep(i, n) rep(j, n) m[i][j] = (i == j ? 0 : 1e8);
    rep(i, n)
    {
        int u, k;
        cin >> u >> k;
        rep(i, k)
        {
            int v, c;
            cin >> v >> c;
            m[u][v] = c;
        }
    }

    rep(k, n) rep(i, n) rep(j, n) m[i][j] = min(m[i][j], m[i][k] + m[k][j]);

    rep(i, n) cout << i << " " << m[0][i] << endl;

    return 0;
}

C++: //
// Created by 李青坪 on 2018/11/7.
//

#include <cstdio>
#include <algorithm>
#include <set>

#define MAXN 100
#define INFINITY 1<<21
#define NIL -1
using namespace std;

int n;
int graph[MAXN][MAXN];
int d[MAXN];
int parent[MAXN];
set<int> S;
set<int> V_S;

void init(){
    S.insert(0);
    for (int j = 1; j < n; ++j) {
        V_S.insert(j);
    }
    fill(d, d+n, INFINITY);
    d[0] = 0;
    for (int k = 1; k < n; ++k) {
        d[k] = graph[0][k];
        parent[k] = 0;
    }
    fill(parent, parent+n, NIL);
}

int findMinEdge(){
    int min = INFINITY;
    int u = NIL;
    set<int>::iterator it;
    for (it = V_S.begin(); it != V_S.end(); ++it){
        if (min > d[*it]){
            min = d[*it];
            u = *it;
        }
    }
    return u;
}

void updateMinWeight(int u){
    set<int>::iterator it;
    for (it = V_S.begin(); it != V_S.end(); ++it){
        if (d[u] + graph[u][*it] < d[*it]){
            d[*it] = d[u]  + graph[u][*it];
            parent[*it] = u;
        }
    }
}

void dijkstra(){
    while (!V_S.empty()){
        int u = findMinEdge();
        S.insert(u);
        V_S.erase(V_S.find(u));
        updateMinWeight(u);
    }
}

int main(){
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        fill(graph[i], graph[i]+n, INFINITY);
    }
    int u, k;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &u, &k);
        int v, w;
        for (int j = 0; j < k; ++j) {
            scanf("%d %d", &v, &w);
            graph[u][v] = w;
        }
    }
    init();
    dijkstra();
    for (int l = 0; l < n; ++l) {
        printf("%d %d\n", l, d[l]);
    }
}
/*
5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3
 */

C++: # include<iostream>
using namespace std;
const int INFTY = (1 << 21);
const int MAX = 100;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n, M[MAX][MAX];

void dijkstra() {
	int minv;
	int d[MAX], color[MAX];
	for (int i = 0; i < n; i++) {
		d[i] = INFTY;
		color[i] = WHITE;
	}

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

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

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

	for (int i = 0; i < n; i++) {
		cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
	}
}

int main() {
    cin >> n;
	int u, k, v, d;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			M[i][j] = INFTY;
		}
	}
	
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v >> d;
			M[u][v] = d;
		}
	}

	dijkstra();
	return 0;
}

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

#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vvi g(n, vi(n, INT_MAX));

    rep(i, 0, n) {
        int u, k;
        cin >> u >> k;

        rep(j, 0, k) {
            int v, c;
            cin >> v >> c;
            g[u][v] = c;
        }
    }

    vector<bool> seen(n, false);
    vi ds(n, INT_MAX);

    ds[0] = 0;

    while (true) {
        int minv = INT_MAX;
        int u = -1;

        rep(i, 0, n) {
            if (!seen[i] && ds[i] < minv) {
                minv = ds[i];
                u = i;
            }
        }
        if (u == -1) { break; }
        seen[u] = true;

        rep(v, 0, n) {
            if (!seen[v] && g[u][v] != INT_MAX && ds[v] > minv + g[u][v]) {
                ds[v] = minv + g[u][v];
            }
        }
    }

    rep(i, 0, n) {
        cout << i << " " << ds[i] << endl;
    }

    return 0;
}

C++: #include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
#define rep(i,n) for(int i=0; i<(n); ++i)
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
typedef pair<int,int> pii;
template<class A, class B>inline bool chmax(A &a, B b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, B b){return b<a ? a=b,1 : 0;}
#define INF 0x3f3f3f3f

int N;
vector <pii> G[101];
int mcos[101];

void dijkstra(int s)
{
   priority_queue < pii, vector<pii>, greater<pii> > pq;
   pq.push( mkp(0, s) );
   memset( mcos, INF, sizeof(mcos) );
   mcos[s] = 0;
   while( !pq.empty() ){
      const pii tmp = pq.top(); pq.pop();
      if( mcos[tmp.se] < tmp.fi )continue;
      for( const auto& v : G[tmp.se] ){
         const int nc = v.fi + tmp.fi;
         if( chmin( mcos[v.se], nc )) pq.push( mkp(nc, v.se) );
      }
   }
}

signed main()
{
   FAST();
   cin >> N;

   rep(i, N){
      int u, k, to, c;
      cin >> u >> k;
      while(k--){
         cin >> to >> c;
         G[i].pb( mkp(c,to) );
      }
   }
   dijkstra(0);
   rep(i, N) cout << i << ' ' << mcos[i] << '\n';
}