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>
using namespace std;

const int INF=(1<<20);

int n;
vector<pair<int,int> >l[10000];
 
void dijkstra();

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

  return(0);
}

void dijkstra(){
  priority_queue<pair<int,int> >pq;
  int sn[10000];
  int d[10000];
  for(int i=0;i<n;i++){
    d[i]=INF;
    sn[i]=0;
  }
  
  d[0]=0;
  pq.push(make_pair(0,0));

  while(!pq.empty()){
    pair<int,int> f=pq.top();
    pq.pop();
    int u=f.second;
    sn[u]=1;

    if(d[u]<f.first*(-1))continue;
      
      for(int j=0;j<l[u].size();j++){
	int v=l[u][j].first;
	if(sn[v]==1)continue;

	if(d[v]>d[u]+l[u][j].second){
	  d[v]=d[u]+l[u][j].second;
	  pq.push(make_pair(d[v]*(-1),v));
	}

	
      }
    
  }

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

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const int W = 0, B = 1;
vector<vector<pair<int, int>>>adj;
int n;
void dijkstra() {
	priority_queue<pair<int, int>>pq;
	vector<int>color(n, W), d(n, INF);
	d[0] = 0;
	pq.push(make_pair(0, 0));

	while (!pq.empty()) {
		pair<int, int>f(pq.top()); pq.pop();// d[v], v
		color[f.second] = B;

		if (d[f.second] < (-1)*f.first)continue;
		
		rep(i, 0, adj[f.second].size()) {
			pair<int, int>v(adj[f.second][i]);
			if (color[v.first] != B&&d[f.second] + v.second < d[v.first]) {
				d[v.first] = d[f.second] + v.second;
				pq.push(make_pair(d[v.first] * (-1),v.first));
			}
		}
	}
	rep(i, 0, n) {
		cout << i << " " << d[i] << endl;
	}
}
signed main() {
	cin >> n; adj = vector<vector<pair<int, int>>>(n, vector<pair<int, int>>());
	rep(i, 0, n) {
		int u, k; cin >> u >> k;
		rep(j, 0, k) {
			int v, c; cin >> v >> c;
			adj[u].push_back(make_pair(v, c));

		}
	}
	dijkstra();
	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <queue>
#define MAX_N (10010)
#define MAX_C (100010)
#define INF (MAX_N * MAX_C)

using namespace std;
typedef pair<int, int> P;

vector<P> G[MAX_N];
int d[MAX_N];
int par[MAX_N];
bool arrived[MAX_N];

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

    for ( int i = 0; i < n; i++ ) {
	int u, k;
	scanf("%d %d", &u, &k);
	for ( int j = 0; j < k; j++ ) {
	    int v, c;
	    scanf("%d %d", &v, &c);
	    G[u].push_back( make_pair( v, c ) );
	}
    }

    for ( int i = 0; i < n; i++ ) {
	d[i] = INF;
    }
    d[0] = 0;
    par[0] = -1;

    priority_queue<P, vector<P>, greater<P> > PQ;
    PQ.push( make_pair( 0, 0 ) );

    while ( !PQ.empty() ) {
	P p = PQ.top(); PQ.pop();
	if ( p.first > d[p.second] ) continue;
	int u = p.second;
	arrived[u] = true;

	for ( int i = 0; i < G[u].size(); i++ ) {
	    P v = G[u][i];
	    if ( !arrived[v.first] ) {
		if ( d[v.first] > d[u] + v.second ) {
		    d[v.first] = d[u] + v.second;
		    PQ.push( make_pair( d[v.first], v.first ) );
		    par[v.first] = u;
		}
	    }
	}
    }

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

C++: #include <cstdio>
#include <cstring>
#include <climits>
#include <queue>
#include <map>
using namespace std;

typedef map<int, int>::iterator mitr;

map<int, int> w[10000];
int d[10000];
char pos[10000];

priority_queue<pair<int,int> > others;
pair<int,int> p;

void findPath(){
    p = others.top();
    others.pop();
    while(pos[p.second]){
        p = others.top();
        others.pop();
    }
    pos[p.second]=1;

    int tmpu = p.second;
    int tmpd = -p.first;

    for(mitr i=w[tmpu].begin();i!=w[tmpu].end();i++){
        if(tmpd + i->second < d[i->first]){
            d[i->first] = tmpd + i->second;
            others.push(make_pair(-d[i->first],i->first));
        }
    }
}

int main(){
    int i,j,u,k,n,v,c;

    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d %d",&u,&k);
        for(j=0;j<k;j++){
            scanf("%d %d",&v,&c);
            w[u][v] = c;
        }
    }

    memset(pos,0,sizeof(char)*100);
    for(i=0;i<n;i++)d[i]=INT_MAX;
    d[0] = 0;
    others.push(make_pair(0,0));
    for(i=0;i<n;i++)findPath();
    for(i=0;i<n;i++)printf("%d %d\n",i,d[i]);

    return 0;
}