Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

var infty = 1000000000

type DirectedGraphNodeID int

type DirectedGraphNode struct {
	key         DirectedGraphNodeID
	adjacents   []*DirectedGraphNode
	costs       []int
	distance    int
	predecessor *DirectedGraphNode
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]*DirectedGraphNode, nNodes)
	for i := 0; i < nNodes; i++ {
		node := DirectedGraphNode{}
		node.key = DirectedGraphNodeID(i)
		node.distance = infty
		graph.nodes[i] = &node
	}
	return &graph
}

type DirectedGraph struct {
	nodes []*DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(i, j DirectedGraphNodeID, cost int) {
	g.nodes[i].adjacents = append(g.nodes[i].adjacents, g.nodes[j])
	g.nodes[i].costs = append(g.nodes[i].costs, cost)
}

func (g *DirectedGraph) print() {
	for i := 0; i < len(g.nodes); i++ {
		node := g.nodes[i]
		key := node.key
		strings := []string{strconv.Itoa(int(key)), strconv.Itoa(node.distance)}
		for j := 0; j < len(node.adjacents); j++ {
			nodeJ := fmt.Sprintf("(%v, %v)", node.adjacents[j].key, node.costs[j])
			strings = append(strings, nodeJ)
		}
		fmt.Printf("%v\n", strings)
	}
}

func (g *DirectedGraph) relaxEdge(l, r *DirectedGraphNode, w int) {
	if r.distance > l.distance+w {
		// fmt.Printf("%v -> %v, %v\n", l.key, r.key, l.distance+w)
		r.distance = l.distance + w
		r.predecessor = l
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	graph := newDirectedGraph(nNodes)

	for i := 0; i < nNodes; i++ {
		iNode := DirectedGraphNodeID(nextInt())
		nAdjacent := nextInt()
		for j := 0; j < nAdjacent; j++ {
			jNode := DirectedGraphNodeID(nextInt())
			cost := nextInt()
			graph.setAdjacency(iNode, jNode, cost)
		}
	}

	graph.nodes[0].distance = 0
	for iNode := 0; iNode < nNodes; iNode++ {
		for iLeft := 0; iLeft < len(graph.nodes); iLeft++ {
			nodeLeft := graph.nodes[iLeft]
			for iRight := 0; iRight < len(nodeLeft.adjacents); iRight++ {
				nodeRight := nodeLeft.adjacents[iRight]
				cost := nodeLeft.costs[iRight]
				graph.relaxEdge(nodeLeft, nodeRight, cost)
			}
		}
	}

	// graph.print()

	for iNode := 0; iNode < len(graph.nodes); iNode++ {
		node := graph.nodes[iNode]
		fmt.Println(node.key, node.distance)
	}
}

C++: #include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);

enum Color {
  WHITE, GRAY, BLACK
};

int M[MAX][MAX];
int d[MAX];
int p[MAX];
Color color[MAX];
int n;

void init() {
  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    d[i] = INFTY;
  }
}

void dijkstra(int s) {
  init();
  d[s] = 0;
  p[s] = -1;

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

    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (M[u][v] != INFTY && color[v] != BLACK) {
        if (d[u] + M[u][v] < d[v]) {
          d[v] = d[u] + M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
}

int main() {
  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++) {
    int elem, index, v;
    cin >> v >> elem;
    for (int j = 0; j < elem; j++) {
      int u;
      cin >> u;
      cin >> M[v][u];
    }
  }
  dijkstra(0);
  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
  return 0;
}

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