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>
#define INF 100000000
using namespace std;
int main(){
  int n;cin>>n;
  int u,k,v,c;
  int mp[101][101],d[101];
  for(int i=0;i<=100;i++){
    d[i]=INF;
    for(int j=0;j<=100;j++)mp[i][j]=INF;
  }
    while(u!=n-1){
    cin>>u>>k;
    for(int i=0;i<k;i++){
      cin>>v>>c;
      mp[u][v]=c;
    }
  }
    
    int mn,s,t[101]={},cn=n-1;
    d[0]=0,t[0]=1;
    while(cn){
      mn=INF;
      for(int i=0;i<n;i++){
	if(t[i]==0){
	  if(d[i]>d[s]+mp[s][i])d[i]=d[s]+mp[s][i];
	  if(mn>d[i])mn=d[i],k=i;
	}
      }
      if(mn!=INF)cn--,t[k]=1,s=k;
    }
    for(int i=0;i<n;i++)cout<<i<<" "<<d[i]<<endl;
    
  return 0;
}

C++: #include <cstdio>
#include <queue>
#include <utility>
using namespace std;
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>> adj[100];
int *d;

void dijkstra() {
    priority_queue<pair<int, int>> PQ;
    int d[n], color[n];

    for (int i = 0; i < n; i++) {
        d[i] = INFTY;
        color[n] = WHITE;
    }
    d[0] = 0;
    PQ.push(make_pair(0, 0));

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

        int 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) { continue; }
            if (d[u] + adj[u][j].second < d[v]) {
                d[v] = d[u] + adj[u][j].second;
                PQ.push(make_pair(d[v] * (-1), v));
                color[v] = GRAY;
            }
        }
    }
    
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i, d[i]);
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int u, k;
        scanf("%d %d", &u, &k);
        for (int j = 0; j < k; j++) {
            int v, c;
            scanf("%d %d", &v, &c);
            adj[u].push_back(make_pair(v, c));
        }
    }

    dijkstra();

    return 0;
}

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

const int NMAX = 100;
const int AMAX = 9999999;

const int BLACK = 2;
const int GRAY = 1;
const int WHITE = 0;

int A[NMAX][NMAX];
int c[NMAX];
int d[NMAX];
int n;

void dijkstra() {
	while (true) {
		int minv, minu;
		minv = AMAX;
		minu = -1;
		for (int i = 0; i < n; i++) {
			if (d[i] == AMAX || c[i] == BLACK) continue;
			minv = min(d[i], minv);	
			if (d[i] == minv) minu = i;
		}
		if (minu == -1 || minv == AMAX) return;

		c[minu] = BLACK;

		for (int i = 0; i < n; i++) {
			if (A[minu][i] <= 0 || c[i] == BLACK) continue;

			if (d[i] > d[minu] + A[minu][i]) {
				d[i] = d[minu] + A[minu][i];
				c[i] = GRAY;
			}
		}
	}
}

int main() {
	scanf("%d", &n);

	for (int i = 0; i < n; i++) {
		c[i] = WHITE;
		d[i] = AMAX;
		int u, k;
		scanf("%d %d", &u, &k);

		for (int j = 0; j < k; j++) {
			int v, c;
			scanf("%d %d", &v, &c);
			A[u][v] = c;
		}
	}

	d[0] = 0;
	dijkstra();

	for (int i = 0; i < n; i++) {
		printf("%d %d\n", i, d[i]);
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
#define MAX 100
#define white 0
#define gray 1
#define black 2
#define INFTY (1 << 21)

int n,M[MAX][MAX];

void route(){
    int minv,d[MAX],color[MAX];
    
    for(int i = 0;i < n;i++){
        d[i] = INFTY;
        color[i] = white;
    }
    
    d[0] = 0;
    color[0] = gray;
    while(1){
        minv = INFTY;
        int 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 < n;v++){
            if(color[v] != black&&M[u][v] != INFTY){
                if(d[v] > d[u] + M[u][v]){
                    d[v] = d[u] + M[u][v];
                    color[v] = gray;
                }
            }
        }
    }
    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++){
        for(int j = 0;j < n;j++){
            M[i][j] = INFTY;
        }
    }
    
    int k,c,v,u;
    for(int i = 0;i < n;i++){
        cin >> u >> k;
        for(int j = 0;j < k;j++){
            cin >> v >> c;
            M[u][v] = c;
        }
    }
    route();
    return 0;
}

C++: #include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
#include<fstream>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)

#define mp make_pair
#define P pair<int,int>
#define ll __int64
vector<P>G[1000];
int n,u,k;
const int INF=100000000;
int ans[1000];

int main(){
	int from,cost;
	cin>>n;

	rep(i,n){
		cin>>u>>k;
		rep(i,k){
				cin>>from>>cost;
				G[u].push_back(mp(from,cost));
		}
	}
	rep(i,n)ans[i]=INF;
	//ans[0]=0;
	priority_queue<P,vector<P>,greater<P> >que;//firstスタートからの重みsecond頂点
	que.push(mp(0,0));
	
	while(!que.empty()){
		P now=que.top();
		que.pop();
		if(ans[now.second]!=INF)continue;
		int index=now.second;
		ans[index]=now.first;
		//cout<<now.first<<endl;
		for(int i=0;i<G[now.second].size();i++)que.push(mp(now.first+G[index][i].second,G[index][i].first));//次への経路長、頂点番号
	}
	for(int i=0;i<n;i++)cout<<i<<" "<<ans[i]<<endl;
	return 0;
}