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<bits/stdc++.h>
#include<limits.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)

#define INF INT_MAX

#define MAX 10000
vector<vector<pair<int, int>>> adj_list(MAX);

void dijkstra(int start_node, int node_size){
    vector<int> visited(node_size, 0);
    vector<int> cost_table(node_size, INF);

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
    
    visited.at(start_node) = 1;
    cost_table.at(start_node) = 0;
    //queueの初期化 startノードに隣接するノードとそのコストをpush
    // cout << "queue pushed : ";
    int i = 0;
    while(adj_list.at(start_node).at(i).first != -1){
        pair<int, int> x = make_pair(adj_list.at(start_node).at(i).second, adj_list.at(start_node).at(i).first);
        q.push(x);
        // cout << x.first << " " << x.second << "  ";
        cost_table.at(x.second) = x.first;
        i++;
    }
    // cout << endl;

    // cout << "cost_table ====" << endl;
    // rep(i, node_size) cout << i << " " << cost_table.at(i) << endl;
    // cout << endl;


    while(q.size() != 1){
        pair<int, int> Nnode = q.top(); q.pop();
        int nearestNode = Nnode.second;

        visited.at(nearestNode) = 1;

        int i = 0;

        while(adj_list.at(nearestNode).at(i).first != -1){
            int v = adj_list.at(nearestNode).at(i).first; // 構築中の木に隣接するノードvと、追加したノード(nearestNode)からのコストc
            int c = adj_list.at(nearestNode).at(i).second;// 

            // cout << "nearest node : " << nearestNode << " " << v << " " << c << endl;

            if(visited.at(v) == 1){
                // cout << "old" << endl;
                i++;
                continue;
            }else{
                // cout << "new : cost_table " << cost_table.at(v) << " " <<  cost_table.at(nearestNode) << endl;

                if(cost_table.at(v) > cost_table.at(nearestNode) + c){
                    cost_table.at(v) = cost_table.at(nearestNode) + c;
                    q.push(make_pair(cost_table.at(v), v));
                    // cout << "queue pushed : cost " << cost_table.at(v) << " node " << v << endl; 
                }
            }
            i++;
        }
    }

    rep(i, node_size){
        cout << i << " " << cost_table.at(i) << endl;
    }
}



int main(){
    int n; cin >> n;

    //　隣接リストの作成
    rep(i, n){
        int from, k; cin >> from >> k;

        rep(j, k){
            int to, weight; cin >> to >> weight;
            adj_list.at(from).push_back(make_pair(to, weight));
        }
    }

    //番兵の挿入
    rep(i, n){
        adj_list.at(i).push_back(make_pair(-1, -1));
    }

    // cout << endl;
    // rep(i, n){
    //     int j = 0;
    //     while(adj_list.at(i).at(j).first != -1){
    //         cout << adj_list.at(i).at(j).first << " " << adj_list.at(i).at(j).second << " ";
    //         j++;
    //     }
    //     cout << endl;
    // }

    dijkstra(0, n);

}

C++: #include<iostream>
using namespace std;
#include<cstdio>
#include<vector>
#include<utility>
#include<queue>

typedef vector<pair<int, int> > adj;

struct current_cost
{
	int index, cost;
	bool operator < (const current_cost &cur) const
	{
		return cost < cur.cost;
	}
};

current_cost init(int i, int c)
{
	current_cost cur;
	cur.index = i, cur.cost = c;
	return cur;
}

int main()
{
	int i, j, k, n, m, next, cost;
	scanf("%d", &n);
	adj *adlist;
	adlist = new adj [n];

	for(k = 0; k < n; k++){
		scanf("%d %d", &i, &m);
		for(j = 0; j < m; j++){
			scanf("%d %d", &next, &cost);
			adlist[i].push_back(make_pair(next, cost));
		}
	}
	
	int *d;
	d = new int [n];
	d[0] = 0;
	for(i = 1; i < n; i++){
		d[i] = 200000;
	}

	priority_queue<current_cost> C;
	current_cost cur = init(0, 0);
	C.push(cur);

	int size;
	bool *check;
	check = new bool [n];
	for(i = 0; i < n; i++){ check[i] = false; }

	while(!C.empty())
	{
		cur = C.top(); C.pop();
		if(check[cur.index]) continue;
		check[cur.index] = true;

		i = cur.index;
		size = adlist[i].size();
		for(j = 0; j < size; j++){
			k = adlist[i][j].first;
			if(d[i] + adlist[i][j].second < d[k]){
				d[k] = d[i] + adlist[i][j].second;
				C.push(init(k, d[k]));
			}
		}
	};
	for(i = 0; i < n; i++){ printf("%d %d\n", i, d[i]); }

	return 0;
}