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++: #include <iostream>
using namespace std;

#define WHITE 0 
#define GRAY 1
#define BLACK 2
#define INFTY 10000000
int n,color[100],M[100][100],d[100],p[100];
int i,j,u,k,v,c;

void dijkstra(){
  for( i = 0; i < n; i++ ){
    color[i] = WHITE;
    d[i] = INFTY;
  }
  d[0] = 0;
  p[0] = -1;
  int cnt = 0;
  // color[0] = GRAY;
  while(1){
    cnt++;
    //cout << cnt << endl;
    int u = -1;
    int mincost = INFTY;
    for( i = 0; i < n; i++ ){
      if( color[i] != BLACK && d[i] < mincost ){
	mincost = d[i];
	u = i;
      }
    }
    if( u == -1 ) break;
    //    if( mincost == INFTY ) break;
    color[u] = BLACK;

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

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

int main(){
 
  cin >> n;

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

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

  return 0;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static 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 ( minv > d[i] && color[i] != BLACK ) {
        u = i;
        minv = d[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;
  for ( int i = 0; i < n; i++ )
    for ( int j = 0; j < n; j++ )
      M[i][j] = INFTY;

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

  dijkstra();

  return 0;
}

C++: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
int main() {
    int N; scanf("%d", &N);
    vector< vector< pair<int, int> > > G(N);
    for(int i=0; i<N; i++) {
        int u, k; scanf("%d%d", &u, &k);
        for(int j=0; j<k; j++) {
            int v, c; scanf("%d%d", &v, &c);
            G[u].emplace_back(v, c);
        }
    }

    vector<int> dist(N, INF), checked(N);
    dist[0] = 0, checked[0] = true;

    int u = 0;
    for(int i=0; i<N; i++) {
        for(auto e : G[u]) {
            int v, c; tie(v, c) = e;
            chmin(dist[v], dist[u] + c);
        }

        int ma = INF, v = 0;
        for(int k=0; k<N; k++) {
            if(checked[k]) continue;
            if(ma > dist[k]) {
                ma = dist[k];
                v = k;
            }
        }
        checked[v] = true;
        swap(u, v);
    }

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

C++: #include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
#define MAX 10000000

int b[110];

int solve(int a[110][110], int n){
	b[0] = 0;
	for(int i = 0;i < n;i++){
		for(int j = 1;j < n;j++){
			//if(j == 4)cout << b[4] << i << " " << endl;
			if(a[i][j] != -1)b[j] = min(b[j], a[i][j]+b[i]);
			//if(j == 4)cout << b[4] << i << " " << endl;
		}
	}
	//cout << endl;
	return 0;
}

int main(){
	int a[110][110], n;
	fill((int*)a, (int*)(a+110), -1);
	fill(b, b+110, MAX);
	cin >> n;
	for(int i = 0;i < n;i++){
		int u, k, c, d;
		cin >> u >> k;
		for(int j = 0;j < k;j++){
			cin >> c >> d;
			a[u][c] = d;
		}
	}
	
	for(int i = 0;i < n;i++){
		for(int j = 0;j < n;j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	
	for(int i = 0;i < 2*n;i++)solve(a, n);
	//solve(a, n);
	for(int i = 0;i < n;i++){
		cout << i << " " << b[i] << endl;
	}
	
	return 0;
}
/*
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<stdio.h>
#define MAX 100
#define INF 999999
#define WHITE 0
#define BLACK 2

void dijkstra();

int n, G[MAX][MAX];

int main()
{
  int i, j, u, v, c, k;

  scanf("%d", &n);
  for(i=0; i<n; i++)
    {
      for(j=0; j<n; j++) G[i][j] = INF;
    }

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

void dijkstra()
{
  int d[n], color[n], i, u, v, mincost;

  for(i=0; i<n; i++)
    {
      d[i] = INF;
      color[i] = WHITE;
    }

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

      if(mincost == INF)break;

      color[u] = BLACK;

      for(v=0; v<=n; v++)
	{
	  if(G[u][v] == INF) continue;

	  if(d[v] > d[u] + G[u][v]) 
	    {
	      d[v] = d[u] + G[u][v];
	    }
	}
    }
  
  for(i=0; i<n; i++)
    {
      printf("%d %d\n", i, d[i]);
    }
}