Go: package main

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

const BIG_COST = 200000

type Path struct {
	node int
	cost int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)

	// 各頂点につながる頂点一覧を得るための隣接リスト
	graph := make([][]Path, nNodes)

	for i := 0; i < nNodes; i++ {
		u := scanInt(sc)
		k := scanInt(sc)
		graph[i] = make([]Path, k)
		for d := 0; d < k; d++ {
			graph[u][d] = Path{
				node: scanInt(sc),
				cost: scanInt(sc),
			}
		}
	}

	// 0 から各頂点への最小コストを保存する配列
	minCosts := make([]int, nNodes)

	minCosts[0] = 0
	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	// 各頂点が調査済みかどうかを保持する配列
	checked := make([]bool, nNodes)

	// 最小コストのパスを高速に取得するためのヒープ。
	// コストが小さいものほどルートに来るようにする。
	heap := NewHeap(func(a, b Path) bool {
		return a.cost < b.cost
	})

	heap.insert(Path{node: 0, cost: 0})
	for heap.size > 0 {
		u := heap.extractMax()

		// 隣接行列を使っていた時とは違い、ヒープには
		// 同じノードへのパスが複数含まれうる。そのため既によりコストの低い
		// パスが見つかっている場合は無視する。
		if minCosts[u.node] < u.cost {
			continue
		}

		checked[u.node] = true

		for _, v := range graph[u.node] {
			cost := v.cost + u.cost
			if !checked[v.node] && cost < minCosts[v.node] {
				minCosts[v.node] = cost
				heap.insert(Path{node: v.node, cost: cost})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i, cost := range minCosts {
		fmt.Fprintf(w, "%d %d\n", i, cost)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

// --- From ALDS1_9_C: Priority Queue

type Heap struct {
	size   int
	heap   []Path
	isHigh func(a, b Path) bool
}

func NewHeap(isHigh func(a, b Path) bool) Heap {
	heap := make([]Path, 1) // 先頭は不使用
	return Heap{size: 0, heap: heap, isHigh: isHigh}
}

func (h *Heap) insert(v Path) {
	h.size += 1
	if h.size > len(h.heap)-1 {
		h.heap = append(h.heap, v)
	} else {
		h.heap[h.size] = v
	}

	for i := h.size; i > 1; {
		parent := i / 2
		if h.isHigh(h.heap[parent], h.heap[i]) {
			break
		}
		h.heap[i], h.heap[parent] = h.heap[parent], h.heap[i]
		i = parent
	}
}

func (h *Heap) extractMax() Path {
	if h.size <= 0 {
		panic("cannot extract from empty heap")
	}

	max := h.heap[1]
	h.heap[1] = h.heap[h.size]
	h.size -= 1
	h.maxHeapify(1)

	return max
}

func (h *Heap) maxHeapify(idx int) {
	left := idx * 2
	right := left + 1
	largest := idx

	if left <= h.size {
		if h.isHigh(h.heap[left], h.heap[idx]) {
			largest = left
		}
		if right <= h.size && h.isHigh(h.heap[right], h.heap[largest]) {
			largest = right
		}
	}

	if largest != idx {
		h.heap[idx], h.heap[largest] = h.heap[largest], h.heap[idx]
		h.maxHeapify(largest)
	}
}

C++: #include <stdio.h>
#include <iostream>
#include <queue>
#define N 10010
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int, int> ii;
vector<ii> m[N];
int n;
int used[N];
int d[N];
priority_queue<ii, vector<ii>, greater<ii> > td;
void dijk(){
    d[0] = 0;
    used[0] = 1;
    td.push(make_pair(0, 0));
    while(!td.empty()){
        ii temp = td.top(); td.pop();
        if(temp.first == INF) break;
        int du = temp.first;
        int u = temp.second;
        for(int j = 0; j < m[u].size(); j++){
            int v = m[u][j].second;
            if(!used[v] && d[u]+m[u][j].first < d[v]){
                d[v] = d[u] + m[u][j].first;
                td.push(make_pair(d[v], v));
            }
        }
    }
    for(int i = 0; i < n; i++){
        printf("%d %d\n", i, d[i]);
    }
}
int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        d[i] = INF;
        used[i] = 0;
    }
    for(int p = 0; p < n; p++){
        int node, k;
        scanf("%d %d", &node, &k);
        for(int t = 0; t < k; t++){
            int a, b;
            scanf("%d %d", &a, &b);
            m[node].push_back(make_pair(b, a));
        }
    }
    dijk();
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int main() {

	int INF = 10000000;

	int n;
	scanf("%d", &n);

	vector<pair<int, int>> G[n];
	for (int i = 0; i < n; ++i) {
		int from;
		scanf("%d", &from);

		int v;
		scanf("%d", &v);

		for (int j = 0; j < v; ++j) {
			int to;
			int w;
			scanf("%d %d", &to, &w);
			G[from].push_back(make_pair(to, w));
		}
	}

	int C[n];
	for (int i = 0; i < n; ++i)
		C[i] = INF;

	int color[n];
	int WHITE = 0;
	int BLACK = 1;
	for (int i = 0; i < n; ++i)
		color[i] = WHITE;

	priority_queue<pair<int, int>> PQ;
	PQ.push(make_pair(0, 0));
	C[0] = 0;
	while (!PQ.empty()) {
		int u = PQ.top().second;
		PQ.pop();
		color[u] = BLACK;
		for (int i = 0; i < (int) G[u].size(); ++i) {
			auto c = G[u][i];
			if (color[c.first] != BLACK && c.second + C[u] < C[c.first]) {
				C[c.first] = c.second + C[u];
				PQ.push(make_pair(C[c.first] * -1, c.first));
			}
		}
	}

	for (int i = 0; i < n; ++i) {
		cout << i << " " << C[i] << endl;
	}

	return 0;
}

C++: #define ll long long
#define MAX 10000

#inlude <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
static const int INF = (1 << 21);

int n;
priority_queue<pair<int,int>> PQ;
vector<pair<int, int>> adj[MAX];
int color[MAX];
int d[MAX];

void dj(int s){
	for (int i = 0;i < n;i++) {
		d[i]=INF;
		color[i] = 0;
	}
	
	d[s] = 0;
	PQ.push(make_pair(0, 0));
	color[0] = 1;

	while (!PQ.empty()) {
		pair<int, int> f = PQ.top(); 
		PQ.pop();
		int u = f.second;
		color[u] = 2;

		if (d[u] < f.first*(-1)) continue;

		for(int j=0;j<adj[u].size();j++){
			int v = adj[u][j].first;
			if (color[v] == 2) continue;
			if (d[v] > d[u] + adj[u][j].second) {
				d[v] = d[u] + adj[u][j].second;
				PQ.push(make_pair(d[v] * (-1), v));
				color[v] = 1;
			}
		}
	}

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

}

int main() {
	cin >> n;
	for (int i = 0;i < n;i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0;j < k;j++) {
			int v, c;
			cin >> v >> c;
			adj[u].push_back(make_pair(v,c));
		}
	}

	dj(0);

}

C++: // ConanYu's https://vjudge.net/solution/13220749
// semon0x29a's https://vjudge.net/solution/13218148

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using pi = pair<int, int>;

const int INF = 0x3F3F3F3F;
const int maxN = 10010;

int n;
bool mark[maxN];
LL dis[maxN];
map<pi, LL> path;

void init() {
	memset(dis, 0x3F, sizeof(dis));
}

void dijkstra() {
	dis[0] = 0;
	for (int i=0; i<n; ++i) {
		int marker = 0, amin = INF;
		for (int i=0; i<n; ++i) if (!mark[i]) {
			if (dis[i] < amin) {
				amin = dis[i];
				marker = i;
			}
			mark[marker] = true;
			for (int i=0; i<n; ++i) if (!mark[i]) {
				if (path.count({marker, i}))
				if (dis[i] > dis[marker] + path[{marker, i}])	
					dis[i] = dis[marker] + path[{marker, i}];
			}
		}
	}
}

int main()
{
	init();
	
	scanf("%d", &n);
	for (int i=0; i<n; ++i) 
	{
		int u, k; scanf("%d%d", &u, &k);
		while (k--) {
			int v; LL c; scanf("%d%lld", &v, &c);
			if (path.count({u, v}))
				path[{u, v}] = min(path[{u, v}], c);
			else path[{u, v}] = c;
			path[{v, u}] = path[{u, v}];
		}
	}
	
	dijkstra();
	for (int i=0; i<n; ++i) {
		printf("%d %lld\n", i, dis[i]);
	}
}