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>
using namespace std;

#define M 100
#define WHITE 1
#define BLACK 2
#define GRAY 3
#define INFTY (1<<21)

int n,A[M][M];

void dic(){
  int d[M],p[M],min,color[M],u;

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

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

    color[u] = BLACK;

    for(int i = 0;i < n;i++){
      if(color[i] != BLACK && A[u][i] != INFTY){
	if(d[u] + A[u][i] < d[i]){
	  d[i] = d[u] + A[u][i];
	  p[i] = u;
	  color[i] = GRAY;
	}
      }
    }
  }
  for(int i = 0;i < n;i++){
    cout << i << " ";
    if(d[i] != INFTY) cout << d[i];
    else cout << -1;
    cout << endl;
  }
}

int main(){
  int s,t,u,v;
    
  cin >> n;
  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      A[i][j] = INFTY;
    }
  }

  for(int i = 0;i < n;i++){
    cin >> s >> t;
    for(int j = 0;j < t;j++){
      cin >> u >> v;
      A[s][u] = v;
    }
  }

  dic();

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

void dijkstra(void);

int n;
int M[MAX][MAX];

int main(){
  int i,j;
  int k,c,u,v;
  cin >> n;
  for( i = 0; i < n; i++){
    for(j = 0; j < n; j++){
      M[i][j] = INFINITY;
    }
  }
  for(i = 0; i < n; i++){
    cin >> u;
    cin >> k;
    for(j = 0; j < k; j++){
      cin >> v;
      cin >> c;
      M[u][v] = c;
    }
  }
  dijkstra();
  return 0;
}

void dijkstra(){
  int minV;
  int d[MAX],color[MAX];
  int u,v;
  int i;
  for(i = 0; i < n; i++){
    d[i] = INFINITY;
    color[i] = WHITE;
  }
  d[0] = 0;
  color[0] = GRAY;
  while(1){
    minV = INFINITY;
    u = -1;
    for(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(v = 0; v < n; v++){
      if(color[v] != BLACK && M[u][v] != INFINITY){
        if(d[v] > d[u] + M[u][v]){
          d[v] = d[u] + M[u][v];
          color[v] = GRAY;
        }
      }
    }
  }
  for(i = 0; i < n; i++){
    cout << i << " " << (d[i] == INFINITY ? -1 : d[i]) << endl;
  }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)

static const int INFTY = (1 << 29);
static const int WHITE = 0; //?????¢?´¢
static const int GRAY = 1; //??¢?´¢??????
static const int BLACK = 2; //?¨??????????

int n, M[101][101];

void dijkstra(void) {
	int dist[101]; //?§????????????????i????§????????????????????????????
	int color[101]; //?¨??????¶???(???or??°or???)

	//?????????????????§?????????????¨??????¶???????????§?????????
	REP(i, n) {
		dist[i] = INFTY;
		color[i] = WHITE;
	}

	dist[0] = 0; //?§????0??????0?????§???????????????0
	color[0] = GRAY; //0?????¢?´¢??????	

	//?????¢?´¢???????????????????????????????°??????????????§????????????????????????´??°????????????
	int minCost;
	while (true) {
		minCost = INFTY; //????°??????????????????????
		int minNode = -1; //????°?????????????????????????????????????
		REP(i, n) {
			if (minCost > dist[i] && color[i] != BLACK) {
				minNode = i;
				minCost = dist[i];
			}
		}
		if (minNode == -1) break; //-1???????????¨???????????¨????????????????????????????????£?????¨???????????¨

		color[minNode] = BLACK; //????°??????????????????????????§??????????????¨???????????????????

		//??¢?´¢????????????????????????????????´??°??????
		REP(v, n) {
			if (color[v] != BLACK && M[minNode][v] != INFTY) {
				if (dist[v] > dist[minNode] + M[minNode][v]) {
					dist[v] = dist[minNode] + M[minNode][v];
					color[v] = GRAY;
				}
			}
		}
	}
	
	REP(i, n) {
		cout << i << " " << (dist[i] == INFTY ? -1 : dist[i]) << endl;
	}

}

int main() {
	cin >> n;

	//??£??\???????????????????????§?????????
	REP(i, n) {
		REP(j, n) {
			M[i][j] = INFTY;
		}
	}

	//??£??\??????????????????
	int u, k, v, c;
	REP(i, n) {
		cin >> u >> k;
		REP(j, k) {
			cin >> v >> c;
			M[u][v] = c;
		}
	}

	dijkstra();

	
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#define INF 200001 
using namespace std;
int main(){
 int u,k,C,V,A[1000][1000],D[1000][1000],n;
  scanf("%d",&n);
   for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j] = INF;
  for(int i=0;i<n;i++){
      scanf("%d %d",&u,&k);    
      for(int j=0;j<k;j++){
    scanf("%d %d",&V,&C);
    D[u][V] = C;
      }    
  }
  for(int i=0;i<n;i++)D[i][i] = 0;
  for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j]= min(D[i][j],D[i][k] + D[k][j]);
  for(int i=0;i<n;i++)printf("%d %d\n",i,D[0][i]);
  return 0;
}