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++: /*???????????????????????¢?????´???????????§?????????????°???¨??????????????????????????¢?´¢??????*/
/*????????????????????\????????¨???????????¨??§???????????????O((|E|+|V|)log|V|)?????§??????*/
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep0(i,i0,n) for(int i = i0;i<=n;i++)
using namespace std;
#define inf (1<<20)
#define MAX 100000

static const int White=0;
static const int Gray=1;
static const int Black=2;

int n;
vector<pair<int, int> > adj[MAX]; //??£??\?????????

void Dijkstra(){
    priority_queue<pair<int, int> > PQ;
    int color[MAX];
    int D[MAX];
    rep(i, n){
        D[i]=inf;
        color[i]=inf;
    }
    D[0]=0;
    PQ.push(make_pair(0, 0));
    color[0]=Gray;

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

        //????°??????????????????????????????§???????????°????????????
        if(D[u] < f.first * (-1) ) continue;

        rep(j, adj[u].size()){
            int v =adj[u][j].first;
            if(color[v]==Black) 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]=Gray;
            }
        }
    }

    rep (i,n) cout << i << " " << ( D[i] == inf ? -1 : D[i]) << endl;
}

int main(void){
    int k,u,v,c;
    cin >> n;

    rep(i, n){
        cin >> u >>k;
        rep(j, k){
            cin >> v >> c;
            adj[u].push_back(make_pair(v, c));
        }
    }

    Dijkstra();

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1000 * 100000;

int main(void){
  int n;
  cin >> n;
  vector< vector<pair<int, int> > > g(n);
  for(int i=0; i < n; i++){
    int v; cin >> v;
    int k; cin >> k;
    for(int j=0; j < k; j++){
      int v0, c; cin >> v0 >> c;
      g[v].push_back(make_pair(v0, c));
    }
  }
  vector<ll> d(n, INF);
  vector<bool> used(n, false);
  priority_queue<pair <int, int> > pq;
  d[0] = 0;

  pq.push(make_pair(0, 0));

  while(!pq.empty()){
    pair<int, int> f = pq.top(); pq.pop();
    int u = f.second;
    used[u] = true;
    if(d[u] < f.first * (-1)) continue;

    for(int i=0; i < g[u].size(); i++){
      int v = g[u][i].first;
      if(used[v] == true) continue;
      if(d[v] > d[u] + g[u][i].second){
        d[v] = d[u] + g[u][i].second;
        pq.push(make_pair(d[v] * (-1), v));
      }
    }
  }
  for(int i=0; i < n; i++){
    cout << i << " " << d[i] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define MAX 100
#define INF (1<<30)

using namespace std;
enum state{
  BLACK,
  WHITE
};

int main(){
  vector<int> v[MAX];
  vector<int> c[MAX];
  state s[MAX];
  int d[MAX];
  int n; cin>>n;

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

  for(int i=0;i<n;i++){
    s[i] = WHITE;
    d[i] = INF;
  }
  s[0]=BLACK;
  d[0]=0;

  vector<int> blacklist;
  blacklist.push_back(0);

  while(1){
    int& last = blacklist.back();
    for(int i = 0;i<v[last].size();i++){
      if(d[v[last][i]]>d[last]+c[last][i]){
        d[v[last][i]]=d[last]+c[last][i];
      }
    }

    int minv=-1;
    int mind=INF;
    for(int b:blacklist){
      for(int i=0;i<v[b].size();i++){
        if(s[v[b][i]]!=BLACK && d[v[b][i]]<mind){
          minv=v[b][i];
          mind=d[v[b][i]];
        }
      }
    }
    if(minv!=-1){
      s[minv]=BLACK;
      blacklist.push_back(minv);
    }else{
      break;
    }
  }

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