Go: package main

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

//make priority queue
type Item struct {
	id       int
	priority int // this state is priority and distans from start
	index    int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

// we want the ascending-priority-queue.
// So we use less than here.
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = j
	pq[j].index = i
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(dist int, id int) {
	for _, i := range *pq {
		if i.id == id {
			if i.priority > dist {
				i.priority = dist
			}
		}
	}
}

// the main code
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	return n
}

func dikstra(dist [][]int, n int) []int {
	// preparate priority queue
	inf := 1 << 30
	pq := make(PriorityQueue, n)
	shortests := make([]int, n)
	for i := 0; i < n; i++ {
		pq[i] = &Item{
			id:       i,
			priority: inf,
			index:    i,
		}

		shortests[i] = inf
	}
	pq[0].priority = 0
	shortests[0] = 0

	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		shortests[item.id] = item.priority
		for id, d := range dist[item.id] {
			if d != -1 {
				pq.update(item.priority+d, id)
			}
		}
		heap.Init(&pq)
	}
	return shortests
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// init
	n := nextInt(sc)
	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dist[i][j] = -1
		}
	}

	// std Input
	for i := 0; i < n; i++ {
		u := nextInt(sc)
		k := nextInt(sc)
		for j := 0; j < k; j++ {
			v, c := nextInt(sc), nextInt(sc)
			dist[u][v] = c
		}
	}

	shortests := dikstra(dist, n)
	for i, d := range shortests {
		fmt.Printf("%d %d\n", i, d)
	}
}

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;
  }
}