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
	heap.Init(&pq)

	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<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

#include<queue>

using namespace std;

static const int MAX_N = 10000;
static const int INFTY = (1 <<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

class q_element
{
    public:
        // 各頂点のNo.と経路の値
        int vertex_num;
        int vertex_val;

        // コンストラクタ
        q_element(int a, int b)
        {
            vertex_num = a;
            vertex_val = b;
        };
};

// ">" のオーバーロード [ receive_val ] を基準に大小比較を行う
bool operator> (const q_element &q_element_1, const q_element &q_element_2)
{
    return q_element_1.vertex_val > q_element_2.vertex_val;
};

// q_elementクラスのオブジェクトを昇順にするためのキューの定義
std::priority_queue< q_element,
                     std::vector<q_element>,
                     std::greater<q_element> > pqEle;

// 隣接リストを作成する ( 頂点と重みの情報を格納する )
vector<q_element> v_Link[MAX_N + 1];

int n;

// ダイクストラ法を実施するメソッド( 優先度付きキュー )
int Dijkstra_priority_queue();

int main(void)
{

    cin >> n;

    int k, c, u, v;
    q_element q_entity(0, 0);

    for(int i = 0; i < n; i++)
    {
        cin >> u >> k;

        for(int j = 0; j < k; j++)
        {
            cin >> v >> c;

            // 隣接リストの作成
            q_entity.vertex_num = v;
            q_entity.vertex_val = c;

            v_Link[u].push_back(q_entity);
        }
    }

    // 優先度付きキューを用いたダイクストラ法
    Dijkstra_priority_queue();

    return 0;

}

// 優先度付きキューを用いたダイクストラ法を実施するメソッド
int Dijkstra_priority_queue()
{
    int u, v, minv;
    int d[MAX_N], p[MAX_N], color[MAX_N];

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

    // 初期値の設定
    d[0] = 0;
    color[0] = GRAY;
    // キューに要素を入れる
    pqEle.push( q_element(0, 0) );

    while( !pqEle.empty() )
    {
        //minv = INFTY;
        // 次に移動するべき頂点
        //u = -1;

        // 最小の値を持つ要素を取り出す ( キューの先頭 )
        u = pqEle.top().vertex_num;
        minv = pqEle.top().vertex_val;
        pqEle.pop();

        // 訪問完了
        color[u] = BLACK;

        for(int v_num = 0; v_num < v_Link[u].size(); v_num++)
        {
            // 隣接リストから要素を取得する
            v = v_Link[u][v_num].vertex_num;

            if( color[v] != BLACK )
            {
                // u → v の経路の中で, vのコストが最小のものが見つかれば更新する
                if( d[v] > d[u] + v_Link[u][v_num].vertex_val )
                {
                    // コストの記録
                    d[v] = d[u] + v_Link[u][v_num].vertex_val;
                    // 頂点 v の親を記録する
                    p[v] = u;
                    // 訪問状態の記録
                    color[v] = GRAY;

                    // キューに要素を入れる
                    pqEle.push( q_element(v, d[v]) );
                }
            }
        }
    }

    // 結果の表示
    for(int i = 0; i < n; i++)
    {
        cout << i << " " << ( d[i] == INFTY ? -1 : d[i] ) << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define MAX 10000000000
#define INF INT_MAX

pair<int, int> A[2000000];
int size;

void heapIncreaseKey(int i, pair<int, int> key){
  if(key.first < A[i].first)
    assert(false);
  A[i] = key;
  while(i > 1 && A[i/2].first < A[i].first){
    swap(A[i].first, A[i/2].first);
    i = i/2;
  }
}

void maxHeapInsert(pair<int, int> key){
  size++;
  A[size].first = -INF;
  heapIncreaseKey(size, key);
}
void maxHeapify(ll i){
  int l = i*2;
  int r = i*2+1;
  int largest;
  if(l <= size && A[l].first > A[i].first)
    largest = l;
  else
    largest = i;
  if(r <= size && A[r].first > A[largest].first)
    largest = r;
  if(largest != i){
    swap(A[i], A[largest]);
    maxHeapify(largest);
  }
}
P heapExtractMax(){
  if(size < 1){
    assert(false);
  }
  pair<int, int> maxv = A[1];
  A[1] = A[size];
  size--;
  maxHeapify(1);
  return maxv;
}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n; cin >> n;

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

  //priority_queue< P, vector<P>, greater<P> > que;
  vector<ll> d(n, MAX);
  d[0] = 0;
  //que.push(make_pair(0, 0));
  maxHeapInsert(make_pair(0, 0));

  while(size != 0){
    P q = heapExtractMax();
    if(q.first > d[q.second]) continue;
    
    for(int i = 0; i < f[q.second].size(); i++){
      if(d[ f[q.second][i].first ] > d[q.second] + f[q.second][i].second){
        d[ f[q.second][i].first ] = d[q.second] + f[q.second][i].second;
        maxHeapInsert(make_pair(d[ f[q.second][i].first ], f[q.second][i].first));
      }
    }
  }

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

  return 0;
}