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++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_B&lang=jp
//
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;

#define NUM 100
#define BLACK 0
#define WHITE 1
#define INF 100000000
#define NIL -1

vector<pair<int,int>> A[NUM];
int V[NUM];
int color[NUM];

void init(){
    for(int i = 0; i < NUM; i++){
        V[i] = INF;
        color[i] = WHITE;
    }
}

int main(void){
    init();

    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        int u, k;
        scanf("%d", &u);
        scanf("%d", &k);

        for(int j = 0; j < k; j++){
            int v, c;
            scanf("%d", &v);
            scanf("%d", &c);
            A[u].push_back(make_pair(v, c));
        }
    }

    int next_node = 0;
    V[0] = 0;
    while(1){
        color[next_node] = BLACK;
        for(int i = 0; i < A[next_node].size(); i++){
            int v = A[next_node][i].first;
            int c = A[next_node][i].second;
            V[v] = min(V[v], V[next_node] + c);
        }

        next_node = NIL;
        int min_dist = INF;
        for(int i = 0; i < n; i++){
            if(color[i] == BLACK){
                continue;
            }
            if(V[i] < min_dist){
                min_dist = V[i];
                next_node = i;
            }
        }

        if(next_node == NIL){
            break;
        }
    }
    for(int i = 0; i < n; i++){
        printf("%d %d\n", i, V[i]);
    }
}

C++: #include <iostream>
using namespace std;

static const int MAX = 100;
static const int UNVISITED = 0;
static const int VISITING = 1;
static const int VISITED = 2;

static const int INFTY = (1 << 21);


int n, M[MAX][MAX];

void Dijkstra() {
	
   int d[MAX], s[MAX];
   int  minv;

   for( int i = 0; i < n; i++) {
    	d[i] = INFTY;
       	s[i] = UNVISITED;
    }
    
   d[0] = 0;
  int u = 0;
   while( u != -1 ) {
      s[u] = VISITED; 
   	  minv = INFTY;
      int e = -1;
      for( int v = 0; v < n; v++ ) {

      	if( s[v] != VISITED && M[u][v] !=INFTY ) {
      		if( d[v] > d[u] + M[u][v] ) {
      			d[v] = d[u] + M[u][v];
      		}
      	}

      	if( s[v] != VISITED  && minv > d[v]) {
           
        		e = v;
        		minv = d[v];
     		}
      }
        
      u = e;
    }  

  
   for(int i = 0; i < n; i++ ) {
   	
   	cout << i << ' ' << 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 u, k, v, c;
   for( int i = 0; i < n; i++) {
   	cin >> u >> k;
   	while( k-- ) {
   		cin >> v >> c;
   		M[u][v] = c;
   	}
   }

    Dijkstra() ;

   return 0;
}

C++: #include<iostream>
#define MAX 100
#define INFTY 1<<21
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define NIL -1

int M[MAX][MAX];
int d[MAX], color[MAX];
int n;

// ダイクストラのアルゴリズムを用いて、最短経路上の各頂点まで道のりを求めて出力する。
void dijkstra()
{
	for (int i = 0; i < n; i++)
	{
		d[i] = INFTY;
		color[i] = WHITE;
	}

	d[0] = 0;
	color[0] = GRAY;

	while (true)
	{
		int u = NIL;
		int minv = INFTY;
		for (int i = 0; i < n; i++)
		{
			if (color[i] != BLACK && d[i] < minv)
			{
				u = i;
				minv = d[i];
			}
		}

		if (u == NIL) break;

		color[u] = BLACK;

		for (int 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];
					color[v] = GRAY;
				}
			}
		}
	}

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

}

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

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

	int u, k, v, c;
	for (int i = 0; i < n; i++)
	{
		std::cin >> u >> k;

		for (int j = 0; j < k; j++)
		{
			std::cin >> v >> c;

			M[u][v] = c;
		}
	}

	dijkstra();
}

C++: #include<bits/stdc++.h>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
#define rep(i,n) for(UL i=0; i<(n); i++)

template<class E>
using nega_queue=priority_queue<E,vector<E>,greater<E>>;

UL N;
ULL D[100];
vector<pair<UL,ULL>> E[100];

int main() {
 scanf("%u",&N);
 rep(i,N){
  UL u,k; scanf("%u%u",&u,&k);
  rep(j,k){
   UL v,c; scanf("%u%u",&v,&c);
   E[u].push_back({v,c});
  }
 }
 rep(i,N) D[i]=~0u;
 nega_queue<pair<ULL,UL>> G;
 G.push({0,0});
 int ans=0;
 while(G.size()){
  ULL d=G.top().first;
  UL p=G.top().second;
  G.pop();
  if(D[p]!=~0u) continue;
  D[p]=d;
  for(auto e:E[p]) G.push({d+e.second,e.first});
 }
 rep(i,N) printf("%u %llu\n",i,D[i]);
 return 0;
}