Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	route := make([][]Node, n)
	weight := make([]int, n)
	for i := 0; i < n; i++ {
		weight[i] = NIL
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		route[i] = make([]Node, k)
		for j := 0; j < k; j++ {
			route[u][j] = Node{id: scanInt(), weight: scanInt()}
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for _, nextNode := range route[node.id] {
			if weight[nextNode.id] != NIL {
				continue
			}
			nextNode.weight += node.weight
			heap.Push(h, nextNode)
		}
	}

	for i, w := range weight {
		fmt.Println(i, w)
	}
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define MAX 10000
#define INFTY (1<<20)
#define WHITE 0//未発見
#define GRAY 1//発見済み未確定
#define BLACK 2//確定済み

int n;//num of vertexes
vector<pair<int,int> > v_list[MAX+1];//頂点の隣接リスト(vector)を要素として持つ配列(添え字が頂点番号)

//http://homepage2.nifty.com/well/Operator.html#PAREN
class greaterInt{
public:
	bool operator() (pair<int,int>& a,pair<int,int>& b){
		return a.second > b.second;
	}
};

void dijkstra(){
	priority_queue<pair<int,int>,vector<pair<int,int> >,greaterInt> PQ;//<頂点番号,挿入時点での最短距離>
	int color[MAX];//各頂点の状態を格納する配列
	int d[MAX];//各頂点までの現在の最短距離を格納する変数

	for(int i=0;i<n;i++){
		d[i] = INFTY;
		color[i] = WHITE;
	}

	d[0] = 0;
	PQ.push(make_pair(0,0));//make_pairを使うと勝手にpairで宣言した型に直してくれる
	color[0] = GRAY;

	//PQ.empty() == trueのときこれ以上更新はなくこれまであった更新に関してはすべて処理
	//が終わっている状態
	while( !PQ.empty() ){
		pair<int,int> f = PQ.top();PQ.pop();
		int u = f.first;//今回確定する頂点番号
		
		if( color[u] == BLACK ) continue;//continue break http://kotazi.com/archives/1383
		//以下ではf.second == d[u]が保障される

		color[u] = BLACK;

		for(int j = 0;j < v_list[u].size();j++ ){
			int v = v_list[u][j].first;//はじめの[]は配列要素番号，二つ目の[]はベクトルの要素番号
			if(color[v] == BLACK) continue;
			if( d[v] > d[u] + v_list[u][j].second){
				d[v] = d[u] + v_list[u][j].second;
				PQ.push(make_pair(v,d[v]));
				color[v] = GRAY;
			}
		}
	}

	for(int i = 0;i < n;i++){
		cout <<i <<" " <<(d[i] == INFTY ? -1 : d[i]) <<endl;//http://msmori.blogspot.jp/2011/01/3.html
		//cout <<i <<" " <<d[i] <<endl;
	}
}

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;
			v_list[u].push_back(make_pair(v,c));
		}
	}
	dijkstra();


	return 0;
}

C++: #include <cstdio>
#include <cctype>
#include <climits>
#include <vector>
using namespace std;

#define si static inline
#define gcu getchar_unlocked
si int in() {
	int c, n = 0;
	// bool minus = false; if ((c = gcu()) == '-') minus = true; else ungetc(c, stdin);
	while ((c = gcu()) >= '0' && c <= '9') n = 10 * n + (c - '0');
	return n; }
	// return minus ? -n : n; }
si void scan(char *s) {while (!isspace(*s++ = gcu()));}
#define pcu putchar_unlocked
#define svo si void out
template <typename T>
svo(T n) {
	static char buf[20];
	char *p = buf;
	//if(n < 0) pcu('-'), n *= -1;
	if (!n) *p++ = '0';
	else while (n) *p++ = n % 10 + '0', n /= 10;
	while (p != buf) pcu(*--p); }
svo(const char *s){while(*s)pcu(*s++);}
svo(char *s){while(*s)pcu(*s++);}
svo(char c){pcu(c);}
template <typename T>
//svo(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');}
svo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
template <typename head, typename... tail>
svo(head&& h, tail&&... t){out(h);out(move(t)...);}
#undef svo
#undef si

template <typename T>
struct heap{
	vector<T> h = {T()};

	void insert(T x) {
		h.push_back(x);
		int c = h.size() - 1;
		for (int p = c / 2; p; c = p, p /= 2)
			if (h[p] < x)
				h[c] = move(h[p]);
			else
				break;
		h[c] = move(x);
	}
	T extract() {
		T res = move(h[1]);
		h[1] = h.back();
		T v = move(h[1]);
		h.pop_back();

		int p = 1, H = h.size();
		for (int c = 2; c < H; p = c, c = p * 2) {
			if (c + 1 < H && h[c] < h[c + 1])
				c++;
			if (h[c] < v)
				break;
			h[p] = move(h[c]);
		}
		h[p] = move(v);

		return move(res);
	}

	bool empty() {
		return h.size() == 1;
	}
};

#define v vector
struct graph {
	struct node {int n, w;};
	v<v<node>> nodes;

	void read(int n) {
		nodes.resize(n);
		for (auto &x: nodes) {
			in();
			for (int k = in(); k--;) {
				int c = in();
				x.push_back({c, in()});
			}
		}
	}

	v<node> dks() {
		v<node> w(nodes.size(), {0, INT_MAX});
		w[0].w = 0;
		heap<node> q;
		q.insert({0, 0});
		while (!q.empty()) {
			node n = q.extract();
			if (w[n.n].n)
				continue;
			w[n.n].n = 1;
			for (node &x: nodes[n.n]) {
				int a = n.w + x.w;
				if (w[x.n].w > a) {
					w[x.n].w = a;
					q.insert({x.n, a});
				}
			}
		}
		return move(w);
	}
};

bool operator < (const graph::node &a, const graph::node &b) {
	return a.w > b.w;
}

int main() {
	graph g;
	g.read(in());
	int i = 0;
	for (auto &x: g.dks())
		out(i++, ' ', x.w, '\n');
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

const int MAX_V = 10000;
const int INF=1e9;

int cost[MAX_V][MAX_V];
int d[MAX_V];
bool used[MAX_V];
int n;

void dijkstra(int s){
  REP(i,n) d[i]=INF;
  REP(i,n) used[i]=false;
  d[s]=0;
  while(true){
    int v=-1;
    REP(u,n)
      if(!used[u] && (v==-1 || d[u]<d[v]))
	v=u;
    if(v==-1) break;
    used[v]=true;
    REP(u,n) d[u]=min(d[u],d[v]+cost[v][u]);
  }
}

int main(){
  cin>>n;
  REP(i,n)REP(j,n) cost[i][j]=INF;
  REP(i,n){
    int u, k; cin>>u>>k;
    REP(j,k){
      int v,c;
      cin>>v>>c;
      cost[u][v]=c;
    }
  }
  dijkstra(0);
  REP(i,n){
    cout<<i<<" "<<d[i]<<endl;
  }
}

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

int n;
vector <pair<int, int> > adj[10000];

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

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

    for ( int j = 0; j < adj[u].size(); j++ ) {
      int v = adj[u][j].first;
      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));
      }
    }
  }

  //出力
  for ( int i = 0; i < n; i++ ) {
    cout << i << " " << d[i] << endl;
  }
  
}

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;
      adj[u].push_back(make_pair(v,c));
    }
  }

  dijkstra();

  return 0;
}