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>
#include<vector>
#define MAX 1000000
using namespace std;
static const int N = 100;
int A[N][N],d[N],p[N],color[N]={0};
void algo(int);
void print(int);
int main(void){
    int n,u,k,v,c;
    cin >> n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j] = -1;
        }
    }
    for(int i=0;i<n;i++){
        cin >> u >> k;
        if(k!=0){
            for(int j=1;j<=k;j++){
                cin >> v >> c;
                A[u][v] = c;
            }
        }
    }
    for(int i=0;i<n;i++){
        d[i] = MAX;
        p[i] = 0;
    }
    d[0] = 0;
    algo(n);
    print(n);
    return 0;
}
void algo(int n){
    vector<int> S;
    while(S.size()!=n){
        int min = MAX,u =-1;
        for(int i=0;i<n;i++){
            if(min>d[i] && color[i]!=1){
                min= d[i];
                u= i;
            }
        }
        color[u] = 1;
        S.push_back(u);
        for(int v=0;v<n;v++){
            if(d[u]+A[u][v]<d[v] && A[u][v]!=-1){
                d[v] = d[u] + A[u][v];
                p[v] = u;
            }
        }
    }
}
void print(int n){
    for(int i=0;i<n;i++){
        cout << i << ' ' << d[i] << endl;
    }
}

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

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

const int INF = 1 << 21;
const int MAX = 103;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
int n, G[MAX][MAX];

void dijkstra() {
  int minv;
  int d[MAX], color[MAX];

  fill((int*)d, (int*)(d+MAX), INF);
  memset(color, WHITE, sizeof(color));

  d[0] = 0;
  color[0] = GRAY;
  while (true) {
    minv = INF;
    int u = -1;
    rep(i,n) {
      if (minv > d[i] && color[i] != BLACK) {
	u = i;
	minv = d[i];
      }
    }
    if (u == -1) break;
    color[u] = BLACK;
    rep(v,n) {
      if (color[v] != BLACK && G[u][v] != INF) {
	if (d[v] > d[u] + G[u][v]) {
	  d[v] = d[u] + G[u][v];
	  color[v] = GRAY;
	}
      }
    }
  }

  rep(i,n) {
    cout << i << ' ' << (d[i] == INF ? -1 : d[i]) << endl;
  }
}

int main() {
  cin >> n;
  rep(i,n) {
    fill((int*)G[i], (int*)(G[i]+n), INF);
  }
  
  int u, k, v, c;
  rep(i,n) {
    cin >> u >> k;
    rep(j,k) {
      cin >> v >> c;
      G[i][v] = c;
    }
  }

  dijkstra();
  return 0;
}

C++: #include<stdio.h>
#define N 100
#define INFTY (1<<21)
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define NON -1

void dijkstra();

int M[N][N],d[N];
int n;

int main(){
  int i,j,u,k,v;
  
  scanf("%d",&n);

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

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

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



void dijkstra(){
  int u,min;
  int i;
  int color[N];

  //初期化
  for(i=0;i<n;i++){
    d[i]=INFTY;
    color[i]=WHITE;
  }
  
  d[0]=0;
  

  while(1){
    min=INFTY;
    u=NON;
    //進路決め
    for(i=0;i<n;i++){
      if(min>d[i] && color[i]!=BLACK){
        u=i;
        min=d[i];
      }
    }
    if(u==NON) break;
    color[u]=BLACK;
    //次の節点の設定
    for(i=0;i<n;i++){
      if(color[i]!=BLACK && M[u][i]!=INFTY)
      if(d[i]>M[u][i]+d[u]){
        d[i]=M[u][i]+d[u];
        color[i]=GRAY;
      }
    }
  }

}

C++: #include <iostream>

using namespace std;
static const int MAX=100;
static const int INFTY= (1<<21);
int M[MAX][MAX]; //?????\??????
int d[MAX]; //?????????????????¬
bool visited[MAX]; //???????¢??????????

void dijkstra(int n)
{
    int min;
    //init
    for(int i=0;i<n;i++)
    {
        visited[i]=false;
        d[i]=INFTY;
    }
    //select node 0
    d[0]=0;
    while(true)
    {
        min=INFTY;
        int min_point=-1;
        for(int i=0;i<n;i++)  //?????????????????????????????\??¶???
        {
            if(min>d[i]&&visited[i]==false)
            {
                min_point=i;
                min=d[i];
            }
        }
        if(min_point==-1) break;  //??????????????°????????????????????????

        visited[min_point]=true;
        //update
        for(int i=0;i<n;i++)
        {
            if(visited[i]==false&&M[min_point][i]!=INFTY)
                if(d[min_point]+M[min_point][i]<d[i])
                    d[i]=d[min_point]+M[min_point][i];
        }
    }
    //print
    for(int i=0;i<n;i++)
        cout<<i<<' '<<(d[i]==INFTY?-1:d[i])<<endl;
}
int main()
{
    int n;
    cin>>n;
    //init
    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++)
    {
        int j;
        cin>>j;
        int nodes_num;
        cin>>nodes_num;
        for(int k=0;k<nodes_num;k++)
        {
            int v,d;
            cin>>v>>d;
            M[j][v]=d;
        }
    }
    dijkstra(n);
}

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

typedef pair< int, int > Pi;
struct edge{
  int to, cost;
};
typedef vector< vector< edge > > Graph;

int Dijkstra(Graph& info, int s, int g,
             vector< int >& min_cost) {
  priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  min_cost.resize(info.size(), -1);
  que.push( Pi( 0, s));
  min_cost[s] = 0;
  while(!que.empty()){
    Pi p = que.top(); que.pop();
    if(p.second == g) return p.first;
    if(p.first > min_cost[p.second]) continue;
    for(int i = 0; i < info[p.second].size(); i++){
      edge& e = info[p.second][i];
      if(min_cost[e.to] == -1 ||
         p.first + e.cost < min_cost[e.to]){
        min_cost[e.to] = p.first + e.cost;
        que.push( Pi( min_cost[e.to], e.to));
      }
    }
  }
  return -1;
}
int main(){
  int n;
  cin >> n;
  Graph graph(n);
  vector< int > min_cost;
  for(int i = 0; i < n; i++){
    int u, k;
    cin >> u >> k;
    for(int i = 0; i < k; i++){
      int c, v;
      cin >> v >> c;
      graph[u].push_back( (edge){ v, c});
    }
  }
  Dijkstra( graph, 0, n, min_cost);
  for(int i = 0; i < n; i++){
    cout << i << " " << min_cost[i] << endl;
  }
}