Go: package main

import (
	"strconv"
	"fmt"
	"bufio"
	"os"
)
const INF = 1 << 21
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}


func main() {
	scanner.Split(bufio.ScanWords)
	n := nextInt()
	adj := make([][]int, n)
	for i := range adj {
		adj[i] = make([]int, n)
	}

	for i := 0;i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			v := nextInt()
			c := nextInt()
			adj[u][v] = c
		}
	}

	// for i := range adj {
	// 	fmt.Println(adj[i])
	// }

	dist := make([]int, n)
	prev := make([]int, n)
	visited := make([]bool, n)
	dijkstra := func(start int, size int) {
		for i := 0; i < size; i++ {
			dist[i] = INF
			prev[i] = -1
			visited[i] = false
		}
		dist[start] = 0
		for {
			u := -1
			tmp_min := INF
			for i := 0; i < size; i++ {
				if visited[i] == false && dist[i] < tmp_min {
					tmp_min = dist[i]
					u = i
				}
			}
			if u == -1 {
				break
			}
			visited[u] = true
			for i := 0; i < size; i++ {
				if adj[u][i] > 0 {
					w := adj[u][i]
					newlen := dist[u] + w
					if newlen < dist[i] {
						dist[i] = newlen
						prev[i] = u
					}
				}
			}
		}
	}
	dijkstra(0, n)
	for i, cost := range dist {
		fmt.Printf("%d %d\n", i, cost)
	}
}

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';
}