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 <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;
}