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 <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
#define Test
const int MAX = 110000;
const int INF = 10000000;
vector<pair<int, int> > G[MAX];
int n;
int dis[MAX];
int vis[MAX];

void dij(int p) {
    for (int i = 0; i < n; i++) dis[i] = INF;
    memset(vis, false, sizeof(vis));
    dis[p] = 0;
    priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
    pq.push(make_pair(0, p));
    while (!pq.empty()) {
        pair<int, int> pr = pq.top();
        pq.pop();
        int point = pr.second;
        if (vis[point]) continue;
        dis[point] = pr.first;
        vis[point] = true;
        for (int i = 0; i < G[point].size(); i++) {
            int pt = G[point][i].second;
            int v = G[point][i].first;
            if (!vis[pt] && dis[pt] > dis[point]+v) {
                dis[pt] = dis[point] + v;
                pq.push(make_pair(dis[pt], pt));
            }
        }
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int p, k;
        scanf("%d %d", &p, &k);
        while (k--) {
            int v, c;
            scanf("%d %d", &v, &c);
            G[p].push_back(make_pair(c, v));
        }
    }
    dij(0);
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i, dis[i]);
    }
    return 0;
}

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

static const int MAX_NUM   = 101;
static const int NIL       = 100001;
static const int NO_VISIT  = 0;
static const int NOW_VISIT = 1;
static const int END_VISIT = 2;

void SSSP(int *pMatrix, int num)
{
    int i;
    int mincost;
    int target;
    int lcost[MAX_NUM];
    int parent[MAX_NUM];
    int state[MAX_NUM];
    int *pList;

    for (i = 0; i < num; i++) {
        lcost[i]  = NIL;
        parent[i] = -1;
        state[i]  = NO_VISIT;
    }

    lcost[0]  = 0;
    parent[0] = 0;
    state[0]  = NOW_VISIT;

    for (;;) {
        mincost   = NIL;
        for (i = 0; i < num; i++) {
            if ((lcost[i] < mincost) && (state[i] != END_VISIT)) {
                target = i;
                mincost = lcost[i];
            }
        }
        if (mincost == NIL) {
            break;
        }

        state[target] = END_VISIT;
        pList = pMatrix + MAX_NUM * target;
        for (i = 0; i < num; i++) {
            if ((*(pList + i) + lcost[target]) < lcost[i]) {
                lcost[i]  = *(pList + i) + lcost[target];
                state[i]  = NOW_VISIT;
                parent[i] = target;
            }
        }
    }

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

    return;
}

int main(void)
{
    int i, j;
    int num;
    int tmp;
    int target, elem, cost;
    int matrix[MAX_NUM][MAX_NUM];

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        for (j = 0; j < num; j++) {
            matrix[i][j] = NIL;
        }
    }

    for (i = 0; i < num; i++) {
        scanf("%d %d", &target, &tmp);
        for (j = 0; j < tmp; j++) {
            scanf("%d %d", &elem, &cost);
            matrix[target][elem] = cost;
        }
    }

    SSSP(&matrix[0][0], num);

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int INF   = (1 << 21);
static const int WHITE = 0;
static const int GRAY  = 1;
static const int BLACK = 2;
static const int NIL   = -1;
static const int MAX   = 100;

int n;
vector<vector<int>> M(MAX, vector<int>(MAX, NIL));

void dijstra(int s) {
  vector<int> color(n, WHITE);
  vector<int> d(n, INF);
  vector<int> p(n);
  d[s] = 0;
  p[s] = NIL;

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

    color[u] = BLACK;

    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && M[u][v] != NIL) {
        if (d[u] + M[u][v] < d[v]) {
          d[v]     = d[u] + M[u][v];
          p[v]     = u;
          color[v] = GRAY;
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int id, k;
    cin >> id >> k;
    for (int j = 0; j < k * 2; j += 2) {
      int v, c;
      cin >> v >> c;
      M[id][v] = c;
    }
  }
  dijstra(0);
}