Go: // ダイクストラのアルゴリズム
package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	num       int         // 頂点の番号
	weights   map[int]int // 重みMAP
	parent    *node       // MSTへの重み最小の親
	distFromS int         // 始点からの距離
	color     color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]*node, n)
	for i := 0; i < n; i++ {
		weights := eAtoi((strings.Fields(eGetline(scanner))))
		v := &node{weights[0], make(map[int]int, weights[1]), nil, math.MaxInt32, white}
		for j := 0; j < weights[1]; j++ {
			k := weights[j*2+2] // 相手の頂点名
			w := weights[j*2+3] // 重み
			v.weights[k] = w
		}
		nodes[i] = v
	}

	// 初回はnode[0]がMSTに入るように
	nodes[0].distFromS = 0

	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか

		var minNode *node
		min := math.MaxInt32
		for _, v := range nodes {
			if v.color == mst {
				continue
			}
			if v.distFromS < min {
				min = v.distFromS
				minNode = v
			}
		}
		if minNode == nil {
			// すべて完了している.
			break
		}

		// MSTに追加する頂点決定
		minNode.color = mst

		// fmt.Printf(">[%v]\n", minNode)
		// MSTに属していない頂点それぞれについて,Startからの最小の重みの距離を更新
		for i, v := range nodes {
			if v.color == mst {
				continue
			}
			w, ok := minNode.weights[i]
			if !ok {
				// minNodeと接してない
				continue
			}

			if minNode.distFromS+w < v.distFromS {
				// minNode経由の距離がもっとも重みが少ないので親を変更
				v.distFromS = minNode.distFromS + w
				v.parent = minNode
				// fmt.Printf("[%v %v]\n", v, minNode)
			}
		}
		// fmt.Printf("<\n")
	}

	for i, v := range nodes {
		fmt.Printf("%d %d\n", i, v.distFromS)
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX=100;
static const int INFTY=1000000000;
static const int WHITE=-1;
static const int BLACK=1;

int A[MAX][MAX],dk[MAX],p[MAX];

void SSSP(int n){
    int C[n];
    for(int i=0;i<n;i++){
        C[i]=WHITE;
        dk[i]=INFTY;
    }
    dk[0]=0;
    while(true){
        int u=-1;int minv=INFTY;
        for(int i=0;i<n;i++){
            if(C[i]==WHITE&&minv>dk[i]){
                minv=dk[i];
                u=i;
            }
        }
        if(u==-1)break;
        C[u]=BLACK;
        for(int i=0;i<n;i++){
            if(C[i]==WHITE&&A[u][i]!=INFTY){
                if(dk[i]>dk[u]+A[u][i]){
                    dk[i]=dk[u]+A[u][i];
                }
            }
        }
    }
}

int main(){
    int n,v,k,u,d;
    cin>>n;
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)A[i][j]=INFTY;

    for(int i=0;i<n;i++){
        scanf("%d %d",&v,&k);
        for(int j=0;j<k;j++){
            scanf("%d %d",&u,&d);
            A[v][u]=d;
        }
    }
    SSSP(n);
    for(int i=0;i<n;i++){
        printf("%d %d\n",i,dk[i]);
    }

}

C++: #include <cstdio>
#include <map>
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;
map<int, int> *M;
int *d;

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

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

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

        if (u == -1) { break; }

        color[u] = BLACK;
        for (map<int, int>::iterator it = M[u].begin(); it != M[u].end(); it++) {
            if (color[it->first] != BLACK) {
                if (d[u] + it->second < d[it->first]) {
                    d[it->first] = d[u] + it->second;
                    color[it->first] = GRAY;
                }
            }
        }
    }
    
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i, d[i]);
    }
}

int main() {
    scanf("%d", &n);

    M = new map<int, int>[n];

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

    dijkstra();

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <bitset>
#include <cmath>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <stack>
#include <cmath>
using namespace std;

#define INF 1e18
#define int long long
static const int MAX = 110;
static const int NOTFIXED = 0;
static const int FIXED = 1;

int n, m[MAX][MAX];

void dijkstra() {
    int d[n], status[n];
    for(int i = 0; i < n; i++) {
        d[i] = INF;
        status[i] = NOTFIXED;
    }
    d[0] = 0;
    status[0] = NOTFIXED;

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

        if(u == -1) break;
        status[u] = FIXED;
        for(int v = 0; v < n; v++) {
            if(m[u][v] != INF && status[v] != FIXED) {
                if(d[v] > d[u] + m[u][v]) {
                    d[v] = d[u] + m[u][v];
                    status[v] = NOTFIXED;
                }
            }
        }
    }

    for(int i = 0; i < n; i++) {
        cout << i << " " << (d[i] == INF ? -1 : d[i]) << endl;
    }
}

signed main() {
    cin >> n;

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            m[i][j] = INF;
        }
    }

    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;
            m[u][v] = c;
        }
    }

    dijkstra();

    return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int INF = 1e9 ;
const int MAXN = 10100 ;
vector<pair<int, int> > edge[MAXN];
int n;
int d[MAXN], vis[MAXN];
priority_queue<pair<int, int> > q;
void Dij()
{
	for (int i = 0; i < n; ++i) d[i] = INF;
	d[0] = 0;
	q.push(make_pair(0, 0)) ;
	vis[0] = 1;
	while (! q.empty())
	{
		pair<int, int> cur = q.top();
		int u = cur.second;
		q.pop();
		vis[u] = 2;
		if (d[u] < cur.first * (-1)) continue ;
		for (int i = 0; i < edge[u].size(); ++i)
		{
			int to = edge[u][i].first;
			if (vis[to] == 2) continue ;
			if (d[to] > d[u] + edge[u][i].second)
			{
				d[to] = d[u] + edge[u][i].second;
				q.push(make_pair(d[to] * (-1), to));
				vis[to] = 1;
			}
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		int u, k;
		cin >> u >> k;
		for (int j = 1; j <= k; ++j)
		{
			int v, c;
			cin >> v >> c;
			edge[u].push_back(make_pair(v, c));
		}
	}
	Dij();
	for (int i = 0; i < n; ++i)
		cout << i << " " << (d[i] == INF ? -1 : d[i]) << endl;
	return 0;
}