Go: package main

import (
	"fmt"
	"container/heap"
)

type Node struct {
	Id int
	Dis int
}

type PriorityQueue []*Node

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].Dis < pq[j].Dis
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PriorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.(*Node))
}

func (pq *PriorityQueue) Pop() interface{} {
	n := len(*pq)
	pop := (*pq)[n-1]
	*pq = (*pq)[0:n-1]
	return pop
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	c := make([][]int, n)
	pq := make(PriorityQueue, 0, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		c[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
			fmt.Scan(&c[u][j])
		}

		if u == 0 {
			d[u] = 0
		} else {
			d[u] = 1 << 30
		}
		pq.Push(&Node{u, d[u]})
	}
	heap.Init(&pq)

	for pq.Len() > 0 {
		node := heap.Pop(&pq).(*Node)
		for i := 0; i < len(g[node.Id]); i++ {
			nextId := g[node.Id][i]
			nextDis := node.Dis + c[node.Id][i]
			if nextDis < d[nextId] {
				d[nextId] = nextDis
				heap.Push(&pq, &Node{nextId, nextDis})
			}
		}
	}

	for i := 0; i < n; i++ {
		fmt.Println(i, d[i])
	}
}

C++: //
// Created by 李青坪 on 2018/11/7.
//

#include <cstdio>
#include <algorithm>
#include <set>

#define MAXN 100
#define INFINITY 1<<21
#define NIL -1
using namespace std;

int n;
int graph[MAXN][MAXN];
int d[MAXN];
int parent[MAXN];
set<int> S;
set<int> V_S;

void init(){
    S.insert(0);
    for (int j = 1; j < n; ++j) {
        V_S.insert(j);
    }
    fill(d, d+n, INFINITY);
    d[0] = 0;
    for (int k = 1; k < n; ++k) {
        d[k] = graph[0][k];
        parent[k] = 0;
    }
    fill(parent, parent+n, NIL);
}

int findMinEdge(){
    int min = INFINITY;
    int u = NIL;
    set<int>::iterator it;
    for (it = V_S.begin(); it != V_S.end(); ++it){
        if (min > d[*it]){
            min = d[*it];
            u = *it;
        }
    }
    return u;
}

void updateMinWeight(int u){
    set<int>::iterator it;
    for (it = V_S.begin(); it != V_S.end(); ++it){
        if (d[u] + graph[u][*it] < d[*it]){
            d[*it] = d[u]  + graph[u][*it];
            parent[*it] = u;
        }
    }
}

void dijkstra(){
    while (!V_S.empty()){
        int u = findMinEdge();
        S.insert(u);
        V_S.erase(V_S.find(u));
        updateMinWeight(u);
    }
}

int main(){
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        fill(graph[i], graph[i]+n, INFINITY);
    }
    int u, k;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &u, &k);
        int v, w;
        for (int j = 0; j < k; ++j) {
            scanf("%d %d", &v, &w);
            graph[u][v] = w;
        }
    }
    init();
    dijkstra();
    for (int l = 0; l < n; ++l) {
        printf("%d %d\n", l, d[l]);
    }
}
/*
5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3
 */

C++: # include<iostream>
using namespace std;
const int INFTY = (1 << 21);
const int MAX = 100;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n, M[MAX][MAX];

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

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

		if (u == -1) break;
		color[u] = BLACK;

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

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

int main() {
    cin >> n;
	int u, k, v, d;
	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++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v >> d;
			M[u][v] = d;
		}
	}

	dijkstra();
	return 0;
}

C++: #include <bits/stdc++.h>

#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vvi g(n, vi(n, INT_MAX));

    rep(i, 0, n) {
        int u, k;
        cin >> u >> k;

        rep(j, 0, k) {
            int v, c;
            cin >> v >> c;
            g[u][v] = c;
        }
    }

    vector<bool> seen(n, false);
    vi ds(n, INT_MAX);

    ds[0] = 0;

    while (true) {
        int minv = INT_MAX;
        int u = -1;

        rep(i, 0, n) {
            if (!seen[i] && ds[i] < minv) {
                minv = ds[i];
                u = i;
            }
        }
        if (u == -1) { break; }
        seen[u] = true;

        rep(v, 0, n) {
            if (!seen[v] && g[u][v] != INT_MAX && ds[v] > minv + g[u][v]) {
                ds[v] = minv + g[u][v];
            }
        }
    }

    rep(i, 0, n) {
        cout << i << " " << ds[i] << endl;
    }

    return 0;
}

C++: #include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
#define rep(i,n) for(int i=0; i<(n); ++i)
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
typedef pair<int,int> pii;
template<class A, class B>inline bool chmax(A &a, B b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, B b){return b<a ? a=b,1 : 0;}
#define INF 0x3f3f3f3f

int N;
vector <pii> G[101];
int mcos[101];

void dijkstra(int s)
{
   priority_queue < pii, vector<pii>, greater<pii> > pq;
   pq.push( mkp(0, s) );
   memset( mcos, INF, sizeof(mcos) );
   mcos[s] = 0;
   while( !pq.empty() ){
      const pii tmp = pq.top(); pq.pop();
      if( mcos[tmp.se] < tmp.fi )continue;
      for( const auto& v : G[tmp.se] ){
         const int nc = v.fi + tmp.fi;
         if( chmin( mcos[v.se], nc )) pq.push( mkp(nc, v.se) );
      }
   }
}

signed main()
{
   FAST();
   cin >> N;

   rep(i, N){
      int u, k, to, c;
      cin >> u >> k;
      while(k--){
         cin >> to >> c;
         G[i].pb( mkp(c,to) );
      }
   }
   dijkstra(0);
   rep(i, N) cout << i << ' ' << mcos[i] << '\n';
}