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 ll = long long;
using namespace std;
/* initialize */
int n, u, k, v, c, A[100][100];

void dijkstra(){
    int c[100], d[100], p[100];
    for (int i = 0; i < n; ++i) { c[i] = 0;  d[i] = 100000000;}
    d[0] = 0;
    p[0] = -1;
    int minc;
    while(true){
        minc = INT32_MAX;
        int u = -1;
        for (int i = 0; i < n; ++i) {
            if (c[i] != 2 && d[i] < minc){
                minc = d[i];
                u = i;
            }
        }
        if(u == -1) break;

        c[u] = 2;

        for (int v = 0; v < n; ++v) {
            if (c[v] != 2 && A[u][v] != 100000000){
                if(d[u] + A[u][v] < d[v]){
                    d[v] = d[u] + A[u][v];
                    p[v] = u;
                    c[v] = 1;
                }
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << i << " " << (d[i] == 100000000 ? -1 : d[i]) << "\n";
    }
};
/* main */
int main() {
    cin >> n;
    for (int l = 0; l < n; ++l) {
        for (int i = 0; i < n; ++i) {
            A[l][i] = 100000000;
        }
    }
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &u, &k);
        for (int j = 0; j < k; ++j) {
            scanf("%d %d", &v, &c);
            A[u][v] = c;
        }
    }
    dijkstra();
    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
#define MAX_N 100
#define INF 1e9

int cost[MAX_N][MAX_N];
int d[MAX_N];
bool used[MAX_N];
int n, u, k, c, v;

void dijkstra(int s){
  fill(d, d + n, INF);
  fill(used, used + n, false);
  d[s] = 0;

  while(true){
    int v = -1;

    for(int u = 0; u < n; u++){
      if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
    }

    if(v == -1) break;
    used[v] = true;

    for(int u = 0; u < n; u++){
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}

int main(void){
  cin >> n;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cost[i][j] = INF;
    }
  }
  for(int i = 0; i < n; i++){
    cin >> u >> k;
    for(int j = 0; j < k; j++){
      cin >> v >> c;
      cost[u][v] = c;
    }
  }
  dijkstra(0);
  for(int i = 0; i < n; i++){
    cout << i << " " << d[i] << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<queue>
#include<vector>
#define MA 10005
#define INF (1<<30)
using namespace std;
struct Node{
    short id;//根据题意，id最大在short有效范围内
    int weight;
};

short n;
vector<pair<int, int> >adj[MA];//加权有向图的邻接表
priority_queue<Node> PQ;//优先级队列
bool state[MA];//节点状态，false为未访问
int dis[MA];//节点到0点的权值总和

/**重载'<'以自定义优先级队列的优先级，这里定义为权值小优先，与默认的大优先相反*/
bool operator < (const Node node1, const Node node2){
    if(node1.weight==node2.weight)
        return node1.id > node2.id;//权值相同比id，意义不大
    return node1.weight > node2.weight;//返回与正常比较相反的结果
}

/**初始化*/
void init(){
    for(short i=0;i<n;i++)
        dis[i] = INF;//初始权值置为无穷
    dis[0] = 0;//0点权值为0
    Node ini;//0点
    ini.id=0;ini.weight = 0;//0点初始值
    PQ.push(ini);//将0点推入队列
}

/**狄克斯特拉算法*/
void dijkstra(){
    while(!PQ.empty()){//执行至队列为空
        Node nowNode = PQ.top();//从队列中取出元素
        PQ.pop();//清除取出的元素
        short nowId = nowNode.id;//当前元素的ID
        state[nowId] = true;//该节点已从队列中清除，设为访问完毕
        if(dis[nowId]>nowNode.weight)continue;//取出最小值，如果不是最短路径则忽略
        int nowSize = adj[nowId].size();//空间换时间，减少部分运算量
        for(int i=0; i<nowSize; i++){//遍历当前元素的邻接节点
            short nextId = adj[nowId][i].first;//下一个节点的ID
            if(state[nextId]) continue;//如果该节点已被读取则跳过
            int nextWeight = dis[nowId] + adj[nowId][i].second;//0到下一个节点的权值
            if(dis[nextId] > nextWeight){//发现了更短的路径
                dis[nextId] = nextWeight;//更新权值数组对应的数据
                Node nextNode;//即将推入队列的节点
                nextNode.id=nextId; nextNode.weight = nextWeight;//将数据存入节点
                PQ.push(nextNode);//将存有数据的节点推入队列
            }
        }
    }
}

int main(){
    int id, flag, nextId, weight;
    scanf("%d", &n);
    for(int i=0;i<n;i++){
        scanf("%d%d",&id, &flag);
        while(flag--){
            scanf("%d%d", &nextId, &weight);//读取邻接表
            adj[id].push_back(make_pair(nextId, weight));
        }
    }
    init();//执行初始化
    dijkstra();//执行狄克斯特拉算法
    for(short i=0;i<n;i++){//输出全部节点数据
        printf("%d %d\n", i, ((dis[i]==INF)?(-1):(dis[i])));
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int n;
const int INF=123456789;
const int MAX=100;
int color[MAX];
const int BLACK=2;
const int WHITE=0;
const int GRAY=1;
int d[MAX],M[MAX][MAX];

void ds(){
  int minv,u;

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

    if(u==-1) break;
    color[u]=BLACK;

    for(int i=0;i<n;i++){
      if(M[u][i]!=INF&&color[i]!=BLACK){
        if(d[i]>d[u]+M[u][i]){
          d[i]=d[u]+M[u][i];
          color[i]=GRAY;
        }
      }
    }
  }
}

int main(){
  int u,k,v,c;
  cin>>n;
  for(int i=0;i<n;i++){
    color[i]=WHITE; d[i]=INF;
    for(int j=0;j<n;j++){
      M[i][j]=INF;
    }
  }

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

  ds();
  for(int i=0;i<n;i++){
  if(d[i]!=INF)  cout<<i<<" "<<d[i]<<endl;
  else cout<<i<<" "<<-1<<endl;
  }
  return 0;
}