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;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int V;
int d[100001];
vector<edge> es[100001];

void dijkstra(int s)
{
	fill(d, d + V, INF);
	d[s] = 0;
	priority_queue<P, vector<P>, greater<P>> que;
	que.push(P(0, s));
	while (!que.empty())
	{
		P p = que.top(); que.pop();
		int v = p.second;
		if (d[v] < p.first) continue;
		REP(i, es[v].size())
		{
			edge e = es[v][i];
			if (d[e.to] > d[v] + e.cost)
			{
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main()
{
	cin >> V;
	REP(i, V)
	{
		int u, k; cin >> u >> k;
		REP(i, k)
		{
			int c, v; cin >> v >> c;
			es[u].push_back(edge{ u, v, c });
		}
	}
	dijkstra(0);
	REP(i, V)
	{
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <queue>
using namespace std;

#define MAX_N (105)
#define MAX_D (100010)
#define INF (MAX_N * MAX_D)

int n;

int G[MAX_N][MAX_N];
int dist[MAX_N];
bool arrived[MAX_N];

void dijkstra(int s)
{
    for ( int i = 0; i < n; i++ ) dist[i] = INF;

    dist[s] = 0;

    while ( true ) {
	int min_dist = INF;
	int index = -1;
	for ( int i = 0; i < n; i++ ) {
	    if ( !arrived[i] && dist[i] < min_dist ) {
		min_dist = dist[i];
		index = i;
	    }
	}
	
	if ( index == -1 ) break;
	arrived[index] = true;

	for ( int i = 0; i < n; i++ ) {
	    if ( !arrived[i] && G[index][i] != INF ) {
		if ( dist[i] > (dist[index] + G[index][i]) ) {
		    dist[i] = dist[index] + G[index][i];
		}
	    }
	}
    }
}


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

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

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

    dijkstra(0);

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

    return 0;
}

C++: #include <iostream>
#define MAX 100
#define INF (1<<21)
#define WHITE 0
#define GRAY  1
#define BLACK 2 
using namespace std;

int n, adj_M[MAX][MAX];

void Dijkstra()
{
    int next, min_cost;
    int min_dist[n+1], color[n+1], parent[n+1];

    for (int i = 0; i < n; ++i) 
    {
        color[i]  = WHITE;
        parent[i] = -1;
        min_dist[i] = INF;
    }

    min_dist[0] = 0;

    while (true) 
    {
        min_cost = INF;
        next = -1;
        for (int i = 0; i < n; ++i) 
        {
            if (color[i] != BLACK && min_dist[i] < min_cost) 
            {
                min_cost = min_dist[i];
                next = i;
            }
        }
        if (next == -1) break;
        color[next] = BLACK;

        for (int i = 0; i < n; ++i) 
        {
            if (color[i] != BLACK && adj_M[next][i] != INF)
            {
                if (min_dist[next] + adj_M[next][i] < min_dist[i])
                {
                    color[i] = GRAY;
                    min_dist[i] = min_dist[next] + adj_M[next][i];
                    parent[i] = next;
                }
            }
        }
    }

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

int main() 
{
    int id, deg, n_id, n_w;
    cin >> n; 

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

    for (int i = 0; i < n; ++i) 
    {
        cin >> id >> deg;
        for (int j = 0; j < deg; ++j) 
        {
            cin >> n_id >> n_w;
            adj_M[id][n_id] = n_w;
        }
    }

    Dijkstra();
}

C++: #include <iostream>
#include <vector>
#include<climits>

using namespace std;

int n;
vector<int> g[100];
vector<int> w[100];
int d[100];
char color[100];

void dijkstra() {
    d[0] = 0;
    color[0] = 'b';

    for (int k = 1; k < n; ++k) {
        int next;
        int mine = INT_MAX;
        for (int i = 0; i < n; ++i) {
            if (color[i] == 'b') {
                for (int j = 0; j < g[i].size(); ++j) {
                    if (d[i] + w[i].at(j) <= d[g[i].at(j)] && color[g[i].at(j)] == 'w') {
                        d[g[i].at(j)] = d[i] + w[i].at(j);
                        if (d[g[i].at(j)] <= mine) {
                            next = g[i].at(j);
                            mine = d[next];
                        }
                    }
                }
            }
        }
        color[next] = 'b';
        d[next] = mine;
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int u;
        int k;
        cin >> u >> k;
        int v, c;
        for (int j = 0; j < k; ++j) {
            cin >> v >> c;
            g[u].push_back(v);
            w[u].push_back(c);
        }
    }
    for (int m = 0; m < n; ++m) {
        color[m] = 'w';
        d[m] = INT_MAX;
    }

    dijkstra();

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

    return 0;
}