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<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 <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;

#define INF 999999

int n, k;
int color[10010], d[10010];
vector<pair<int, int> > adj[INF];

int dijkstra(int s)
{
    priority_queue<pair<int, int> > q;
    q.push(make_pair(0, s) );
    for(int i = 0; i < n; i ++)
    {
        d[i] = INF;     //距离初始化
    }
    d[s] = 0; //第一个点到它本身距离是零
    
    while(!q.empty() )
    {
        pair<int, int> t = q.top();
        q.pop();
        int u = t.second;
        color[u] = 1;
        
        
        for(int i = 0; i < adj[u].size(); i ++)
        {
            int v = adj[u][i].first;
            if(color[v] == 1)//被染过色了，跳过
                continue;
            if(d[v] > d[u] + adj[u][i].second)
            {
                d[v] = d[u] + adj[u][i].second;
                q.push(make_pair(-d[v], v) );
            }
        }
    }
    return 0;
}

int main(void)
{
    scanf("%d", &n);
    int u, k, v, c;
    for(int i = 0; i < n; i ++)
    {
        scanf("%d%d", &u, &k);
        for(int j = 0; j < k; j ++)
        {
            scanf("%d%d", &c, &v);
            adj[u].push_back(make_pair(c, v));
        }
    }
    dijkstra(0);
    
    for(int i = 0; i < n; i ++)
    {
        if(d[i] != INF)
            printf("%d %d", i, d[i]);
        printf("\n");
    }
    return 0;
}