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 <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define reptr(i,n) for(int (i)=(n);(i)>=0;--(i))
#define All(x) (x).begin(),(x).end()
#define mp make_pair
const ll MOD=1e9+7;
const int MAX = 100;
const int BLACK = 2;
const int GRAY = 1;
const int WHITE = 0;
using namespace std;

int n, M[MAX][MAX];

void dijkstra(){
  int minv;
  int color[MAX];
  //d[i] : 自集合からの点iへの最短距離
  int d[MAX];

  rep(i,n){
    color[i] = WHITE;
    d[i] = INT_MAX;
  }
  d[0] = 0;
  color[0] = GRAY;

  while(1){
    minv = INT_MAX;
    int u = -1;
    rep(i,n){
      if(color[i] != BLACK && minv > d[i]){
        u = i;
        minv = d[i];
      }
    }
    if(u == -1) break;
    color[u] = BLACK;
    rep(v,n){
      //uとvがつながっている、かつvが未探索
      if(M[u][v] != INT_MAX && color[v] != BLACK){
        if(d[v] > d[u] + M[u][v]){
          d[v] = d[u] + M[u][v];
          color[v] = GRAY;
        }
      }
    }
  }
  rep(i,n){
    cout << i << " " << (d[i] == INT_MAX ? -1 : d[i]) << endl;
  }
}

int main(){
  cin >> n;
  rep(i,n){
    rep(j,n){
      M[i][j] = INT_MAX;
    }
  }

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

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <climits>

class Graph{
public:
	std::vector< std::vector<int> > A;
	int n;

	Graph(int n);

	void SetEdge(int a, int b, int weight);
	void SortEdge();
	void SortEdge_Rec1(int left, int right);
	void SortEdge_Rec2(int left, int mid, int right);

	std::vector<int> Dijkstra();

};

Graph::Graph(int n){
	A.resize(n);
	for (int i = 0; i < n; i++) A[i].resize(n, -1);
	this->n = n;
}

void Graph::SetEdge(int a, int b, int weight){
	A[a][b] = weight;
	return;
}
/*
void Graph::SortEdge(){
	SortEdge_Rec1(0, A.size());
}

void Graph::SortEdge_Rec1(int left, int right){
	if (left + 1 < right){
		int mid = (left + right) / 2;
		SortEdge_Rec1(left, mid);
		SortEdge_Rec1(mid, right);
		SortEdge_Rec2(left, mid, right);
	}
}

void Graph::SortEdge_Rec2(int left, int mid, int right){
	int n1 = mid - left;
	int n2 = right - mid;
	std::vector< std::vector<int> > L(n1 + 1);
	for (int i = 0; i < n1; i++) L[i] = A[left + i];
	std::vector< std::vector<int> > R(n2 + 1);
	for (int i = 0; i < n2; i++) R[i] = A[mid + i];
	std::vector<int> x;
	x.push_back(0); x.push_back(0); x.push_back(INFTY);
	L[n1] = x;
	R[n2] = x;
	int i = 0;
	int j = 0;
	for (int k = left; k < right; k++){
		if (L[i][2] <= R[j][2]){
			A[k] = L[i];
			i = i + 1;
		}
		else{
			A[k] = R[j];
			j = j + 1;
		}
	}
}
*/
std::vector<int> Graph::Dijkstra(){
	std::vector<int> d(n, INT_MAX), prev(n, -1), Q(n, 0);
	d[0] = 0;
	for (int i = 0; i < n; i++) Q[i] = i;

	while (!Q.empty()){
		int mini, minu;
		for (int i = 0; i < Q.size(); i++){
			if (i == 0 || d[Q[i]] < d[minu]){
				minu = Q[i];
				mini = i;
			}
		}
		Q.erase(Q.begin() + mini);
		for (int i = 0; i < n; i++){
			if (A[minu][i] != -1){
				if (d[i] > d[minu] + A[minu][i]){
					d[i] = d[minu] + A[minu][i];
					prev[i] = minu;
				}
			}
		}
	}
	return d;
}

int main(){
	int n, u, k, v, c;
	scanf("%d", &n);
	Graph G(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);
			G.SetEdge(u, v, c);
		}
	}
	std::vector<int> result = G.Dijkstra();
	for (int i = 0; i < n; i++) printf("%d %d\n", i, result[i]);

	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i< (n);i++)
using namespace std;
typedef long long ll;
const int INF = 1e+7;
typedef pair<int,int> P;

struct dijkstra{
    vector<int> color;
    vector<vector<P>> adj;
    vector<int> d;
    vector<int> p;
    int n_;
    dijkstra(int n):color(n,0),adj(n),d(n,INF),p(n,-1){
        n_ = n;
    }
    void init(){
        rep(i,n_){
            int u,k;cin>>u>>k;
            rep(j,k){
                int v,c;cin>>v>>c;
                P tmp = make_pair(v,c);
                adj[i].push_back(tmp);
            }
        }
    }
    void solve(){
        init();
        d[0] = 0;
        p[0] = -1;
        while(true){
            int mincost = INF;
            int u;
            rep(i,n_){
                if(color[i]!=2 && d[i]<mincost){
                    mincost = d[i];
                    u = i;
                }
            }
            if(mincost == INF)break;
            color[u] = 2;
            for(P v:adj[u]){    
                if(color[v.first]!=2 && d[u]+v.second < d[v.first]){
                    d[v.first] = d[u] + v.second;
                    p[v.first] = u;
                    color[v.first] = 1;
                }
            }
        }
        rep(i,n_){
            cout<<i<<" "<<d[i]<<endl;
        }
    }
};


int main(){
    int n;cin>>n;
    dijkstra dj(n);
    dj.solve();
    return 0;
}