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>
#define INF 200001
int main(){
  int u,k,C,V,A[1000][1000],D[1000][1000],n;
  std::cin >> n;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j] = INF;
  for(int i=0;i<n;i++){  
    std::cin >> u >> k;
    for(int j=0;j<k;j++){
      std::cin >> V >> C;
      D[u][V] = C;
    }  
  }
  for(int i=0;i<n;i++)D[i][i] = 0;
  for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j]= std::min(D[i][j],D[i][k] + D[k][j]);
  for(int i=0;i<n;i++)std::cout << i << " " << D[0][i] << std::endl;
  return 0;
}

C++: //include
//------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <limits>
using namespace std;

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>(b);i--)
#define REP(i,n)  FOR(i,0,n)

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//debug
//-------------------------------------------
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define INF (1<<21)
#define NOT_VISITED 0
#define FOUND 1
#define VISITED 2

//typedef
//-------------------------------------------
typedef long long ll;
typedef pair<int, int> Pair;

int n;
vector<Pair> g[101];
int d[101];
int p[101];
int visited[101];

int dijkstra(int start) {
    d[start] = 0;
    p[start] = -1;

    while(1) {
        int mincost = INF;
        int u = -1;
        REP(i, n) {
           if (visited[i] != VISITED && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        if (u == -1) break;
        visited[u] = VISITED;

        REP(j, n) {
            REP(k, g[u].size()) {
                if (visited[j] != VISITED && g[u][k].first == j) {
                    if (d[u] + g[u][k].second < d[j]) {
                        d[j] = d[u] + g[u][k].second;
                        p[j] = u;
                        visited[j] = FOUND;
                    }
                }
            }
        }
    }
    return 1;
}

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

    REP(i, 101) {
        visited[i] = NOT_VISITED;
        d[i] = INF; 
        p[i] = -1;
    }

    cin >> n;
    REP(i, n) {
        int u, k;
        cin >> u >> k;
        REP(j, k) {
            int v, c;
            cin >> v >> c;
            Pair e(v, c);
            g[u].push_back(e);
        }
    }

    dijkstra(0);

    REP(i, n)  {
        cout << i << " " << d[i] << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

const int INF=1e9;

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

    int N; cin>>N;
    vector<vector<int>> dist(N,vector<int>(N,INF));
    for(int i=0;i<N;i++) dist[i][i]=0;
    for(int i=0;i<N;i++){
        int u,k; cin>>u>>k;
        while(k--){
            int v,c; cin>>v>>c;
            chmin(dist[u][v],c);
        }
    }
    for(int k=0;k<N;k++) for(int i=0;i<N;i++) for(int j=0;j<N;j++){
        chmin(dist[i][j],dist[i][k]+dist[k][j]);
    }
    for(int i=0;i<N;i++){
        cout<<i<<' '<<dist[0][i]<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
static const int INFTY=(1<<21);
#define WHITE 0
#define GRAY 1
#define BLACK 2
int n,M[102][102],color[102],d[102];
vector <int> searched;
void search(){
    int minp=0,min;
    while(1){
        min=INFTY;
        for(int i=0;i<n;i++){
            if(M[minp][i]!=INFTY&&color[i]==WHITE) color[i]=GRAY;
        }
        for(int i=0;i<searched.size();i++){
            int a=searched[i];
            for(int j=0;j<n;j++){
                if(color[j]==GRAY&&M[a][j]+d[a]<min){
                    min=M[a][j]+d[a];
                    minp=j;
                }
            }
        }
        if(min==INFTY) break;
        else{
            d[minp]=min;
            color[minp]=BLACK;
            searched.push_back(minp);
        }
    }
    for(int i=0;i<n;i++) cout<<i<<" "<<d[i]<<endl;
}
int main(){
    int u,k,v,c;
    cin>>n;
    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++){
        color[i]=WHITE;
        d[i]=INFTY;
        cin>>u>>k;
        for(int p=0;p<k;p++){
            cin>>v>>c;
            M[u][v]=c;
        }
    }
    searched.push_back(0);
    color[0]=BLACK;
    d[0]=0;
    search();
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main(){
	
	int n,aaa;
	cin>>n;
	vector<vector<int>> v(n),c(n);
	vector<int> k(n),ans(n);
	rep(i,n){
		cin>>aaa>>k[i];
		v[i].resize(k[i]);
		c[i].resize(k[i]);
		rep(j,k[i]){
			cin>>v[i][j]>>c[i][j];
		}
		ans[i]=-1;
	}
	ans[0]=0;
	int m=INF,mi,mj;
	rep(aaaa,n-1){
		m=INF;
		rep(i,n){
			if(ans[i]!=-1){
				rep(j,k[i]){
					if(ans[v[i][j]]==-1 && m>ans[i]+c[i][j]){
						m=ans[i]+c[i][j];
						mi=i;
						mj=j;
					}
				}
			}
		}
		ans[v[mi][mj]]=m;
	}
	rep(i,n){
		cout<<i<<" "<<ans[i]<<endl;
	}
	
}