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 <cmath>
#include <cstdio>
using namespace std;
#define MAX 10000000

int b[110];

int solve(int a[110][110], int n){
	b[0] = 0;
	for(int i = 0;i < n;i++)
		for(int j = 1;j < n;j++)
			if(a[i][j] != -1)b[j] = min(b[j], a[i][j]+b[i]);
	return 0;
}

int main(){
	int a[110][110], n;
	fill((int*)a, (int*)(a+110), -1);
	fill(b, b+110, MAX);
	cin >> n;
	for(int i = 0;i < n;i++){
		int u, k, c, d;
		cin >> u >> k;
		for(int j = 0;j < k;j++){
			cin >> c >> d;
			a[u][c] = d;
		}
	}
	
	
	
	for(int i = 0;i < 3;i++)solve(a, n);
	for(int i = 0;i < n;i++){
		cout << i << " " << b[i] << endl;
	}
	
	return 0;
}
/*
5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3
*/

C++: #include <iostream>
#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;
                // printf("d[%d] = %d\n", v, d[v]);
                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() {
    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;
            adj[u].push_back(make_pair(v, c));
        }
    }

    dijkstra();

    return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1e9
using namespace std;
int n,mp[101][101],D[101];

void dijkstra(){
  bool used[101]={};
  for(int i=0;i<101;i++)D[i]=INF;
  D[0]=0;

  while(1){
    int cost=INF,pos=-1;
    for(int i=0;i<n;i++) if(!used[i]&&D[i]<cost) pos=i,cost=D[i];
    if(pos==-1)return ;
    used[pos]=1;    
    for(int i=0;i<n;i++)D[i]=min(D[i],cost+mp[pos][i]);
  }
}


int main(){

  cin>>n;
  for(int i=0;i<101;i++)
    for(int j=0;j<101;j++) mp[i][j]=INF;
  
  for(int i=0,a,m;i<n;i++){
    cin>>a>>m;
    for(int j=0,b,c;j<m;j++)cin>>b>>c,mp[a][b]=c;
  }
  dijkstra();
  for(int i=0;i<n;i++)cout <<i<<" "<<D[i]<<endl;
  
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using ll =long long;

static const int inf = (1<<21);
static const int white = 0;
static const int gray =  1;
static const int black = 2;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//Write From this Line

int n, M[110][110];

void dijkstra(){ 
	int minv;
	int d[110], color[110];

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

	d[0] = 0;
	color[0] = gray;
	while(1) {
		minv = inf;
		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] != inf){
				chmin(d[v],d[u]+M[u][v]);
				color[v] = gray;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << i << " " << (d[i] ==inf ? -1 : d[i]) << endl;
	}
}

int main()
{
	cin >> n;
	rep(i,110){
		rep(j,110){
			M[i][j] = inf;
		}
	}
	rep(i,n){
		int u, k;
		cin >> u >> k;
		rep(j,k){
			int a, b;
			cin >> a >> b;
			M[u][a] = b;
		}
	}
	dijkstra();
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define MAX_N 100
#define MAX_M 100000
int n, c, v;
int dis[MAX_N + 1];
int Adjacency_matrix[MAX_N + 1][MAX_N + 1];
const int INF = 100000000;
void Dijkstra(int s, int g){
  queue<int> que;
  for(int i = 0; i <= MAX_N; i++){
    dis[i] = INF;
  }
  que.push(s);
  dis[s] = 0;
  while(que.size()){
    int p = que.front();
    que.pop();
    for( int i = 0; i < n; i++ ){
      if(Adjacency_matrix[p][i] + dis[p] < dis[i] ){
        que.push(i);
        dis[i] = Adjacency_matrix[p][i] + dis[p];
      }
    }
  }
}

int main(){
  int k_1, u;
  for(int i = 0; i <= MAX_N; i++){
    for( int k = 0; k <= MAX_N; k++){
      Adjacency_matrix[i][k] = INF;
    }
  }
  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> u >> k_1;
    for(int j = 1; j <= k_1; j++){
      cin >> v >> c;
      Adjacency_matrix[i][v] = c;
    }
  }
  for(int i = 0; i < n; i++){
    Dijkstra(0, i);
    cout<<i<<" "<<dis[i]<<endl;
  }
}