Go: package main

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

type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }
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 = i
	pq[j].index = j
}
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
}

type Q struct {
	pq *PriorityQueue
	ds map[int]int // It has always same value as pq. For random access.
}

func newQ() *Q {
	pq := new(PriorityQueue)
	heap.Init(pq)
	return &Q{
		pq: pq,
		ds: make(map[int]int),
	}
}
func (q *Q) pop() (u, d int) {
	item := heap.Pop(q.pq).(*Item)
	delete(q.ds, item.value)
	return item.value, item.priority
}
func (q *Q) get(u int) (int, bool) { // returns d
	d, ok := q.ds[u]
	return d, ok
}
func (q *Q) set(u, d int) {
	q.ds[u] = d
	for _, item := range *q.pq {
		if item.value != u {
			continue
		}

		item.priority = d
		heap.Fix(q.pq, item.index)
		return
	}
	heap.Push(q.pq, &Item{
		priority: d,
		value:    u,
	})
}
func (q *Q) len() int {
	return len(q.ds)
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := newQ()
	q.set(0, 0)
	visited := make(map[int]struct{})
	out := make([]int, n)

	for q.len() > 0 {
		u, d := q.pop()
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			dd := d + c
			if d, ok := q.get(uu); !ok || d > dd {
				q.set(uu, dd)
			}
		}

		out[u] = d
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, 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<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;

#define MAX 10001
#define INF 5000001
#define NIL -1

int a[MAX][MAX];
int n;
int d[MAX];
int pi[MAX];

void Relax(int u, int v){
  if(d[v]> d[u]+a[u][v]){
    d[v]=d[u]+a[u][v];
    pi[v]=u;
  }
}

void ISS(int s){
  int v;
  for(v=0; v<n; v++){
    d[v]=INF;
    pi[v]=NIL;
  }
  d[s]=0;
}

void Dijkstra(int s){
  int i, v1, v, minv, u; 
  int flag[MAX];
  ISS(s);
  
  for(i=0; i<n; i++) flag[i]=0;

  for(v1=0; v1<n; v1++){
    
    minv = INF;
    // decide u
    for ( i = 0; i < n; i++ ){
      if(flag[i]==1) continue; 
      if(d[i]<=minv){
    u=i;
    minv=d[i];
      }
    }
    
    for(v=0; v<n; v++){
      if(a[u][v]==-1) continue;
      if(flag[v]==1) continue;
      /* if(a[u][v]<d[v]){
    pi[v]=u;
    d[v]=a[u][v];
    }*/
      Relax(u, v);
    }
    flag[u]=1;
  }
}

int main(){

  int i, j ,u ,k,v,c;
  // int a[MAX][MAX];
  cin>>n;

  for(i=0; i<n; i++){
    for(j=0; j<n; j++){
      a[i][j]=-1;
    }
  }

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

  /*for(i=0; i<n; i++){
    for(j=0; j<n; j++){
    cout<<a[i][j]<<endl;
    }
    cout<<endl;
    }*/
  Dijkstra(0);
  for(i=0; i<n; i++){
      cout<<i<<" "<<d[i]<<endl;
  }

  return 0;
}