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<vector>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
vector<pair<int, int> > M[MAX];

void dijkstra(int s)
{
    int u,  minv;
    int d[MAX], color[MAX];

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

    d[s] = 0;
    while(1)
    {
        minv = INFTY;
        u = -1;
        for(int i = 0; i < n; i++)
        {
            if(minv > d[i] && color[i] != BLACK)
            {
                u = i;
                minv = d[i];
            }
        }

        if(u == -1) break;
        color[u] = BLACK;
        for(int v = 0; v < M[u].size(); v++)
            if(color[M[u][v].first] != BLACK)
            {
                if(d[M[u][v].first] > M[u][v].second + d[u])
                {
                    d[M[u][v].first] = M[u][v].second + d[u];
                }
            }
    }
    for(int i = 0; i < n; i++)
        cout << i << " " << d[i] << endl;
}

int main()
{
    cin >> n;

    for(int i = 0; i < n; i++)
    {
        int x, k;
        cin >> x >> k;
        for(int j = 0; j < k; j++)
        {
            int v, w;
            cin >> v >> w;
            M[x].push_back(make_pair(v, w));
        }
    }
    dijkstra(0);

    return 0;
}

C++: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005

struct Edge{
	Edge(int arg_to,int arg_dist){
		to = arg_to;
		dist = arg_dist;
	}
	int to,dist;
};

struct Info{
	Info(int arg_node_id,int arg_sum_dist){
		node_id = arg_node_id;
		sum_dist = arg_sum_dist;
	}
	bool operator<(const struct Info &arg) const{

		return sum_dist > arg.sum_dist; //総距離の昇順(PQ)
	}

	int node_id,sum_dist;
};

int V;
int min_dist[SIZE];
vector<Edge> G[SIZE];

int main(){

	scanf("%d",&V);

	int node_id,num_node,to,dist;

	for(int i = 0; i < V; i++){

		scanf("%d %d",&node_id,&num_node);
		for(int k = 0; k < num_node; k++){

			scanf("%d %d",&to,&dist);
			G[node_id].push_back(Edge(to,dist));
		}
	}

	for(int i = 0; i < V; i++){

		min_dist[i] = BIG_NUM;
	}

	priority_queue<Info> Q;
	min_dist[0] = 0;

	Q.push(Info(0,0));

	while(!Q.empty()){

		if(Q.top().sum_dist > min_dist[Q.top().node_id]){

			Q.pop();

		}else{

			for(int i = 0; i < G[Q.top().node_id].size(); i++){

				int next_node = G[Q.top().node_id][i].to;
				int next_dist = Q.top().sum_dist+G[Q.top().node_id][i].dist;

				if(min_dist[next_node] > next_dist){
					min_dist[next_node] = next_dist;
					Q.push(Info(next_node,next_dist));
				}
			}
			Q.pop();
		}
	}

	for(int i = 0; i < V; i++){

		printf("%d %d\n",i,min_dist[i]);
	}

	return 0;
}

C++: //dijkstra算法
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

//INFTY要足够大
const int INFTY = 1 << 21;

const int N = 10000;

vector<pair<int,int> >Graph[N];

int n;

void dijkstra(int u0) {
	int *d = new int[n];
	char *color = new char[n];
	for (int i=0; i<n; i++) {
		d[i] = INFTY;
		color[i] = 'W';
	}

	d[u0] = 0;
	color[u0] = 'G';
	priority_queue<pair<int,int> > PQ;
	PQ.push(make_pair(0,u0));

	while (!PQ.empty()) {
		pair<int,int> f = PQ.top();
		PQ.pop();
		int u = f.second;

		color[u] = 'B';

		//if (d[u] < f.first * -1) continue;

		for (int i=0; i<Graph[u].size(); i++) {
			pair<int,int> p = Graph[u][i];
			int v = p.first;
			if (color[v] != 'B'&& d[v] > d[u] + p.second) {
				d[v] = d[u] +p.second;
				color[v] = 'G';
				PQ.push(make_pair(d[v] * -1,v));
			}
		}
	}
	for (int i=0; i<n; i++) {
		cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
	}
}

int main() {

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

C++: //
//  main.cpp
//
//
//  Created by ???????????? on 2017/10/23.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <queue>
#include <algorithm>


#define n_max 10000
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1<<20

using namespace std;

int n, color[n_max], d[n_max], p[n_max];

vector<pair<int, int> > adj[n_max];


void init(){
    int i, j, id, num, node, weight;
    scanf("%d", &n);
    
    for(i = 0; i < n; i++){
        scanf("%d %d", &id, &num);
        for(j = 0; j < num; j++){
            scanf("%d", &node);
            scanf("%d", &weight);
            adj[id].push_back(make_pair(node, weight));
        }
        color[i] = WHITE;
        d[i] = INFTY;
    }
}

void dijkstra(){
    //initializing
    d[0] = 0;
    p[0] = -1;
    
    priority_queue<pair<int,int> > PQ;
    
    PQ.push(make_pair(0, 0));
    
    int u;
    while(!PQ.empty()){
        //first weight, second idx
        pair<int, int> f= PQ.top(); PQ.pop();
        u = f.second;
        color[u] = BLACK;
        
        if(d[u] < f.first*(-1)) continue;
        
        for(int j = 0; j < adj[u].size(); j++){
            int v = adj[u][j].first;
            if(color[v] != BLACK){
                if((d[u] + adj[u][j].second) < d[v]){
                    d[v] = adj[u][j].second + d[u];
                    PQ.push(make_pair(d[v]*(-1), v));
                    color[v] = GRAY;
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        cout<<i<<" "<<d[i]<<endl;
    }
}

int main(int argc, const char * argv[]) {
    init();
    dijkstra();
    return 0;
}