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++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <cstring>
#define maxn 101
#define INF 0x3f3f3f3f
using namespace std;

int m[maxn][maxn] = {}, d[maxn] = {}, color[maxn] = {};
int n;

void Dijkstra()
{
	for (int i = 1; i < n; i++)
		d[i] = INF;
	d[0] = 0;
	while (true)
	{
		int mincost = INF,u;
		for(int i=0;i<n;i++)
			if (d[i] < mincost&&color[i]==0)
			{
				mincost = d[i];
				u = i;
			}
		if (mincost == INF)break;
		color[u] = 1;
		for (int i = 0; i < n; i++)
			if (color[i] == 0 && m[u][i] != -1)
				if (d[i] > d[u] + m[u][i])
					d[i] = d[u] + m[u][i];
	}
}
int main()
{
	scanf("%d", &n);
	memset(m, -1, sizeof(m));
	for (int i = 0; i < n; i++)
	{
		int u, k;
		scanf("%d %d", &u, &k);
		for (int i = 0; i < k;i++)
		{
			int v, c;
			scanf("%d%d", &v, &c);
			m[u][v] = c;
		}
	}
	Dijkstra();
	for (int i = 0; i < n; i++)
	{
		printf("%d %d\n", i, d[i]);
	}
	return 0;
}

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

using namespace std;

const int INF = 1 << 30;

struct edge {
	int cost; //重み(距離)
	int to; //行先の頂点
};

void dijkstra(const int n, const vector<vector<edge>> &G, const int stpoint) {

	//優先キューは最短距離と頂点番号のペア、 距離を降順に並べる
	priority_queue<pair<int, int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;

	vector<bool> seen(n,false); //訪問完了でtrue
	vector<int> cost(n,INF); //頂点nまでの距離

	cost[stpoint] = 0;
	pq.push(make_pair(0, 0));

	while (pq.empty() == false) {

		//ターゲット頂点設定（すでに決定したものの中のどれか）
		pair<int, int> target = pq.top();
		int tnum = target.second; //ターゲット頂点番号
		pq.pop();
		
		seen[tnum] = true;

		if (cost[tnum] < target.first) { continue; } //これの意味が分からない保留

		for (unsigned int j = 0; j < G[tnum].size(); j++) { //ターゲットから繋がる先を全部探す

			int v = G[tnum][j].to; //繋がる先の頂点をvにする

			if (seen[v] == true) { continue; } //もう見たとこ（仲間に入ってる）ならスキップ

			if (cost[v] > cost[tnum] + G[tnum][j].cost) {
				cost[v] = cost[tnum] + G[tnum][j].cost;
				pq.push(make_pair(cost[v], v));
			}
		}
	}

	for (int i = 0; i < n; i++) {
		cout << i << " " << cost[i] << endl;
	}

}

int main() {

	int n;
	cin >> n;

	int stpoint = 0; //この問題では0起点

	vector<vector<edge>> G(n);

	int in1, in2, in3, in4;
	for (int i = 0; i < n; i++) {
		cin >> in1 >> in2;
		for (int j = 0; j < in2; j++) {
			cin >> in3 >> in4;
			G[in1].push_back({ in4, in3 });
		}
	}

	dijkstra(n,G,stpoint);

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

const int maxn=10000;
const int inf=(1<<20);

int n;
vector<pair<int,int> > adj[maxn];

void Dijkstra(){
    priority_queue<pair<int,int> > PQ;
 //   int color[maxn];
    bool vis[maxn];
    int d[maxn];
    for(int i=0;i<n;i++){
        d[i]=inf;
     //   color[i]=white;
        vis[i]=false;
    }

    d[0]=0;
    PQ.push(make_pair(0,0));
 //   color[0]=gray;
  //  vis[0]=1;

    while(!PQ.empty()){
        pair<int,int> f=PQ.top();PQ.pop();
        int u=f.second;

      //  color[u]=black;
        vis[u]=true;

        if(d[u]<f.first*(-1))   continue;

        for(int j=0;j<adj[u].size();j++){
            int v=adj[u][j].first;
            if(vis[v]==true) continue;
            if(d[v]>d[u]+adj[u][j].second){
                d[v]=d[u]+adj[u][j].second;
    // priority_queue priority_queue 默认优先较大值， 因此要乘以-1。
                PQ.push(make_pair(d[v]*(-1),v));
              //  vis[v]=true;这句不能加
              //  color[v]=gray;
            }
        }
    }

    for(int i=0;i<n;i++){
        printf("%d %d\n",i,(d[i]==inf?-1:d[i]));
    }
}

int main(){
    int k,u,v,c;

    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d%d",&u,&k);
        for(int j=0;j<k;j++){
            scanf("%d%d",&v,&c);
            adj[u].push_back(make_pair(v,c));
        }
    }

    Dijkstra();

    return 0;
}

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

typedef int Weight;
struct Edge {  
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : 
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
const int INF=1e8;

bool shortestPath(const Graph &g,int s,vector<Weight> &dist, vector<int> &prev) {
  int n = g.size();
  dist.assign(n, INF+INF); dist[s] = 0;
  prev.assign(n, -2);
  bool negative_cycle = true;
  for(int k=0;k<n;k++){
    for(int i=0;i<n;i++){
      for(auto e : g[i]){
        if(dist[e.dst] > dist[e.src] + e.weight){
          dist[e.dst] = dist[e.src] + e.weight;
          prev[e.dst] = e.src;
          if(k == n-1){
            dist[e.dst] = -INF;
            negative_cycle = false;
          }
        }
      }
    }
  }
  return negative_cycle;
}

int main(){
	int n; cin >> n;
	Graph g(n);
    int u, k, v, c;
    for(int i=0; i<n; i++){
        cin >> u >> k;
        for(int j=0; j<k; j++){
            cin >> v >> c;
            g[u].push_back({u, v, c});
        }
    }

    vector<Weight> dist;
    vector<int> prev; 
    shortestPath(g, 0, dist, prev);
    
    for(int i=0; i<n; i++){
        cout << i << " " << dist[i] << endl;
    }
}