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;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

void search() {
	int d[MAX], p[MAX], color[MAX];
	for (int i = 0; i < n; i++) {
		p[i] = -1;
		d[i] = INFTY;
		color[i] = WHITE;
	}
	d[0] = 0;
	while (true) {
		int minv = INFTY;
		int u = -1;
		for (int i = 0; i < n; i++) {
			if (minv > d[i] && color[i] != BLACK) {
				minv = d[i];
				u = i;
			}
		}
		if (u == -1)break;
		color[u] = BLACK;
		for (int i = 0; i < n; i++) {
			if (color[i] != BLACK && M[u][i] != INFTY) {
				if (d[u] + M[u][i] < d[i]) {
					d[i] = d[u] + M[u][i];
					p[i] = u;
					color[i] = GRAY;
				}
			}
		}
	}

	for (int i = 0; i < n; i++) {
		cout << i << " " << (d[i] == INFTY ? -1 : 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;
		}
	}
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			cin >> M[u][v];
		}
	}

	search();
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define INF 1<<20
using namespace std;
typedef unsigned long long ll;
int n;
ll A[1000][1000];
ll d[1000];
bool used[1000];
ll D[1000][1000];
 
void Init(int f){
  for(int i=0;i<f;i++){
    for(int j=0;j<f;j++)D[i][j] = INF;
  }
 
}
 
int main(){
  scanf("%d",&n);
  int u,k,C,V;
  Init(n);
  
  for(int i=0;i<n;i++){
      scanf("%d %d",&u,&k);     
      for(int j=0;j<k;j++){
    scanf("%d %d",&V,&C);
    D[u][V] = C;
    D[V][u] = C;
      }     
  }
   
  for(int i=0;i<n;i++)D[i][i] = 0;
  for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j] = min(D[i][j],D[i][k] + D[k][j]); 
  for(int i=0;i<n;i++){
    printf("%lld %lld\n",i,D[0][i]);
  }
 
  return 0;
}

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

int main()
{
	int n, m, i, j, a, w;
	int* adj[100];
	int* dist[100];
	cin >> n;
	for(i = 0; i < n; i++){
		cin >> i >> m;
		adj[i] = new int [m + 1];
		dist[i] = new int [m + 1];
		for(j = 0; j < m; j++){
			cin >> a >> w;
			adj[i][j] = a;
			dist[i][j] = w;
		}
		adj[i][m] = -1;
		dist[i][m] = -1;
	}
	int *d, *prev, *Q;
	d = new int [n];
	prev = new int [n];
	Q = new int [n];
	for(i = 0; i < n; i++){ d[i] = 4000; prev[i] = -1; Q[i] = i; }
	int rest = n; // 0????????£???????????????Q???????????????

	int u, v, min;
	d[0] = 0;  // 0??????????????????
	while(rest > 0){
		min = 4000;
		for(i = 0; i < n; i++){
			if(min > d[i] && Q[i] >= 0){ min = d[i]; u = i; }
		}
		Q[u] = -1;  // d[u]???Q???????????????????°????d??????????????????
		rest--;	    // Q????????????-1?????????rest?????????????????????
		// u?????£??\???????????????d?????´??°???
		for(j = 0; adj[u][j] >= 0; j++){
			v = adj[u][j];
			if(d[v] > d[u] + dist[u][j]){
				d[v] = d[u] + dist[u][j];
				prev[v] = u;
			}
		}
	};
	for(i = 0; i < n; i++){ cout << i << " " << d[i] << endl; }
	for(i = 0; i < n; i++){
		delete adj[i]; delete dist[i]; }
	delete d;
	delete prev;
	delete Q;
	cin >> n;

	return 0;
}

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

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define print_array(ary, size) cout << ary[0]; rep1(i, 1, size - 1) cout << ' ' << ary[i]; cout << endl;
typedef long long ll;
typedef unsigned long long ull;

int M[100][100];
int MinCost[100];
int done[100];

int main()
{
	int n, u, k, v, c;
	int next;
	int cost, sum = 0;

	cin >> n;

	rep(i, n) {
		MinCost[i] = 10000001;
		done[i] = 0;
		rep(j, n) M[i][j] = -1;
	}

	rep(i, n) {
		cin >> u >> k;
		rep(j, k) {
			cin >> v >> c;
			M[u][v] = c;
		}
	}

	MinCost[0] = 0;
	next = 0;
	done[0] = 1;
	sum = 0;
	
	rep(loop, n - 1) {
		rep(i, n) {
			if (M[next][i] != -1) {
				MinCost[i] = min(MinCost[i], MinCost[next] + M[next][i]);
			}
		}

		cost = 10000001;
		rep(i, n) {
			if (done[i] == 0 && cost > MinCost[i]) {
				cost = MinCost[i];
				next = i;
			}
		}

		done[next] = 1;
		sum += cost;
	}

	rep(i, n) {
		cout << i << ' ' << MinCost[i] << endl;
	}

	return 0;
}