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 <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n + 1); ++i)
using namespace std;
using ll = long long;
const int INF = +100100100;
// typedef pair<int,int> P;
const int  WHITE = 0;
const int GRAY =1;
const int BLACK =2;
const int NIL = -1;
const int MAX = 100;

// 配列のサイズを大きく取りすぎるとエラーが出ないが、実行できなくなる.
vector<vector<int>> M(MAX,vector<int>(MAX,INF));
vector<int> color(MAX,WHITE), P(MAX,NIL),D(MAX,INF);
int n;

void dijkstra(){
    int u = 0;
    D[u] = 0;
    while(1){
        int mini =INF;
        rep(i,n){
            if(mini>D[i] && color[i]!=BLACK){
                u = i;
                mini = D[i];
            }
        }
        if(mini==INF)break;
        color[u]=BLACK;
        rep(i,n){
            if(color[i]==BLACK)continue;
            if(D[u]+M[u][i]<D[i]){
                D[i] = D[u]+M[u][i];
            }
            color[i]=GRAY;
        }

    }
}

int main(){
    cin >> n;
    rep(i,n){
        int id, m;
        cin >> id>>m;
        rep(j,m){
            int opp;cin>>opp;
            int cost; cin >> cost;
            M[i][opp]=cost;
        }
    }
    dijkstra();
    rep(i,n){
        cout<<i << " " << D[i]<<endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#define  INF  1e9 
using namespace std;
typedef long long ll;
ll q[210][210];
int  p[210];
ll  dis[210];

void Dijkstra(int N){
	fill(dis,dis+210,INF);
	dis[0]=0;
	int x=0;
	while(p[N-1]==0){
		ll mini=INF;
		p[x]=1;
		for(int y=0;y<N;y++){
			if(p[y]==1)  continue;
			dis[y]=min(dis[y],dis[x]+q[x][y]);
		}
		for(int i=0;i<N;i++){
			if(p[i]==1) continue;
			if(mini>dis[i]){
				mini=dis[i];
				x=i;
			}
		}
	}
}
 
int main()
{
    int u,N,k,v;
	ll C;
	while(~scanf("%d",&N))
	{
		fill(q[0],q[0]+210*210,INF);
		fill(p,p+210,0);
		int M=N;
		while(N--)
		{
		    scanf("%d%d%",&u,&k);
		    for(int i=0;i<k;i++){
		    	scanf("%d%lld",&v,&C);
                q[u][v]=min(q[u][v],C);
			}
		}
		Dijkstra(M);
		for(int i=0;i<M;i++){
			printf("%d %lld\n",i,dis[i]);
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

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

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF (1 << 21)
typedef long long ll;

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

  vector<int> V[n];
  int E[n][n];
  int d[n];
  int visited[n];

  REP(x, n) {
    REP(y, n) {
      E[x][y] = INF;
    }
    d[x] = INF;
    visited[x] = 0;
  }

  REP(_, n) {
    int u, k;
    cin >> u >> k;
    REP(__, k) {
      int v, c;
      cin >> v >> c;
      V[u].push_back(v);
      E[u][v] = c;
      E[v][u] = c;
    }
  }

  d[0] = 0;
  REP(_, n) {
    int minv = -1;
    int mincost = INF;
    REP(v, n) {
      if (!visited[v] && d[v] < mincost) {
        minv = v;
        mincost = d[v];
      }
    }
    visited[minv] = 1;
    for (auto it = V[minv].begin(); it != V[minv].end(); it++) {
      if (E[minv][*it] < INF && d[minv] + E[minv][*it] < d[*it]) {
        d[*it] = d[minv] + E[minv][*it];
      }
    }
  }

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

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
static const int INFTY = (1 << 30);
int n;
void search(vector<vector<int> > &G1, vector<vector<int> > &G2, int *d, bool *v)
{
    int mini, minv;
    while (1)
    {
        bool iscomp = true;
        minv = INFTY;
        rep(i, 0, n)
        {
            if (!v[i] && d[i] < minv)
            {
                mini = i;
                minv = d[i];
                iscomp = false;
            }
        }
        if (iscomp)
            break;
        v[mini] = true;
        rep(i, 0, G1[mini].size())
        {
            int tmp = G1[mini][i];
            if (!v[tmp])
            {
                int tmptime = minv + G2[mini][i];
                if (tmptime < d[tmp])
                    d[tmp] = tmptime;
            }
        }
    }
}
int main()
{
    cin >> n;
    vector<vector<int> > G1(n, vector<int>(0)), G2(n, vector<int>(0));
    int d[n];
    bool v[n] = {};
    rep(i, 0, n) d[i] = INFTY;
    int utmp, ktmp, vtmp, ctmp;
    rep(i, 0, n)
    {
        cin >> utmp >> ktmp;
        rep(j, 0, ktmp)
        {
            cin >> vtmp >> ctmp;
            G1[utmp].push_back(vtmp);
            G2[utmp].push_back(ctmp);
        }
    }
    d[0] = 0;
    search(G1, G2, d, v);
    rep(i, 0, n)
    {
        printf("%d %d\n", i, d[i]);
    }
}

C++: #include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { -1, 0, 1, 0 };

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

	int n;
	cin >> n;

	vector<vector<pair<int, int>>> m(n);

	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;
			m[u].emplace_back(v, c);
		}
	}

	vector<int> d(n, INF), color(n);

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

		if (u == -1) {
			break;
		}

		color[u] = 2;
		for (auto e : m[u]) {
			if (color[e.first] != 2 && d[e.first] > d[u] + e.second) {
				d[e.first] = d[u] + e.second;
				color[e.first] = 1;
			}
		}
	}

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