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 <iostream>
#include <queue>
#include <vector>
#define INF 1<<30
#define WHITE 0
#define BLACK 1
using namespace std;

int main(int argc, char* argv[]) {
    int n, u, k, v, c;
    cin >> n;

    int color[n];
    int d[n];
    vector <pair<int, int>> Adj[n];
    priority_queue <pair<int, int>> que;
    pair<int, int> top;

    for (int i = 0; i < n; i++){
        cin >> u >> k;
        for (int j = 0; j < k; j++){
            cin >> v >> c;
            Adj[u].push_back(make_pair(v, c));
        }
        d[i] = INF;
        color[i] = WHITE;
    }

    d[0] = 0;
    que.push(make_pair(0, 0));

    while (!que.empty()){
        top = que.top();
        que.pop();
        c = (-1)*top.first;
        u = top.second;

        color[u] = BLACK;
        for (int j = 0; j < Adj[u].size(); j++){
            int v = Adj[u][j].first;
            int w = Adj[u][j].second;

            if (color[v]) continue;
            if (d[u] + w < d[v]){
                d[v] = d[u] + w;
                que.push(make_pair((-1)*d[v], v));
            }
        }
    }
    for (int i = 0; i < n; i++)
        cout << i << " " << (d[i] == INF? -1: d[i]) << endl;

    return 0;
}

C++: #include<iostream>
using namespace std;

static const int MAX = 100000;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static 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 (minv  > d[i] && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1 ) {
            break;
        }
        color[u] = BLACK;
        for (int v = 0; v < n; v++) {
            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(int argc, char const* argv[])
{
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            M[i][j] = INFTY;
        }
    }

    int k, c, u, v;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            cin >> v >> c;
            M[u][v] = c;
        }
    }

    dijkstra();
    return 0;
}

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

using namespace std;

vector<pair<int, int>> adja[10000];

#define INF (1LL << 30)

int main() {
	int n, id, k, a, b;

	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> id >> k;
		for (int j = 0; j < k; ++j) {
			cin >> a >> b;
			adja[id].push_back(make_pair(a, b));
		}
	}

	priority_queue<pair<int, int>> que;
	que.push(make_pair(0, 0)); //cost, node
	long long cost[10000];
	fill(cost, cost + n, INF);
	cost[0] = 0;

	while (!que.empty()) {
		for (int i = 0, size = que.size(); i < size; ++i) {
			pair<int, int> top = que.top();
			que.pop();

			for (int j = 0, len = adja[top.second].size(); j < len; ++j) {
				if (cost[adja[top.second][j].first] > top.first + adja[top.second][j].second) {
					cost[adja[top.second][j].first] = top.first + adja[top.second][j].second;
					que.push(make_pair(top.first + adja[top.second][j].second, adja[top.second][j].first));
				}
			}
		}
	}

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

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn=10010;
const int Max=10000001;
int n;
int dis[maxn];
bool vis[maxn];

struct edge
{
	int u,v,w;
	edge(int u=0,int v=0,int w=Max):u(u),v(v),w(w){}
};
vector<edge> G[maxn];

struct node
{
	int d,id;
	bool operator < (const node& rhs) const
	{
		return d>rhs.d;
	}
	node(int d=0,int id=0):d(d),id(id){}
};
priority_queue<node> Q;

void dijkstra(int s)
{
	memset(dis,0x7f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	dis[s]=0;
	Q.push(node(0,s));
	while(!Q.empty())
	{
		node u=Q.top();
		Q.pop();
		if(vis[u.id]) continue;
		vis[u.id]=1;
		for(unsigned int i=0;i<G[u.id].size();++i)
		{
			edge e=G[u.id][i];
			if(dis[e.v]>dis[u.id]+e.w)
			{
				dis[e.v]=dis[u.id]+e.w;
				Q.push(node(dis[e.v],e.v));
			}
		}
	}
}

void print()
{
	for(int i=0;i<n;++i)
	  printf("%d %d\n",i,dis[i]);
	return;
}

int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;++i)
	{
		int id,k;
		scanf("%d%d",&id,&k);
		for(int j=0;j<k;j++)
		{
			int v,w;
			scanf("%d%d",&v,&w);
			G[id].push_back(edge(id,v,w));
		}
	}
	dijkstra(0);
	print();
	return 0;
}