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への重み最小の親
	sum     int         // 始点からの距離
	color   color
}

func (p *node) getMinToMst() int {
	if p.parent == nil {
		return math.MaxInt32
	}
	return p.weights[p.parent.num]
}

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, 0, 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].parent = nodes[0]

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

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

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

		// MSTに追加する頂点決定
		minNode.color = mst
		// 自分の親の合計値と合算する
		minNode.sum = minNode.parent.sum + min

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みの距離を加算
		for _, v := range nodes {
			if v.color == mst {
				continue
			}
			w, ok := v.weights[minNode.num]
			if !ok {
				// minNodeと接してない
				continue
			}

			if w < v.getMinToMst() {
				// minNodeへの距離がもっとも重みが少ないので親を変更
				v.parent = minNode
			}
		}
	}

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

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;

// 総数を1000000007で割った余り
const long long mod = 1e9 + 7;

using ll  = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
// #define SORT(v, n) stable_sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

const int MAX   = 100;
const int INF   = (1 << 21);
const int white = 0;
const int gray  = 1;
const int black = 2;

int n, m[MAX][MAX];

int dijkstra() {
    int minV;
    int d[MAX], color[MAX];
    rep(i, n) {
        d[i]     = INF;
        color[i] = white;
    }

    d[0]     = 0;
    color[0] = gray;
    while (1) {
        minV  = INF;
        int u = -1;
        rep(i, n) {
            if (minV > d[i] && color[i] != black) {
                u    = i;
                minV = d[i];
            }
        }
        if (u == -1) break;
        color[u] = black;
        rep(v, n) {
            if (color[v] != black && m[u][v] != INF) {
                if (d[v] > d[u] + m[u][v]) {
                    d[v]     = d[u] + m[u][v];
                    color[v] = gray;
                }
            }
        }
    }
    rep(i, n) { cout << i << " " << (d[i] == INF ? -1 : d[i]) << endl; }
}

// 単一始点最短経路
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // cout << fixed << setprecision(5);

    cin >> n;
    rep(i, n) {
        rep(j, n) { m[i][j] = INF; }
    }

    int k, c, u, v;
    rep(i, n) {
        cin >> u >> k;
        rep(j, k) {
            cin >> v >> c;
            m[u][v] = c;
        }
    }

    dijkstra();

    return 0;
}

C++: #include <stdio.h>
#define MAX 100
static const int INFTY = (1<<21);
int n,A[MAX][MAX];

void dikusutora();

int main(){
  int i,j,m,k,d,h;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      A[i][j]=INFTY;
    }
  }
  for(i=0;i<n;i++){
    scanf("%d",&m);
    scanf("%d",&k);
    for(j=0;j<k;j++){
      scanf("%d",&d);
      scanf("%d",&h);
      A[m][d]=h;
    }
  }
  dikusutora();
  return 0;
}
void dikusutora(){
  int min,i,e,v;
  int g[MAX],a[MAX];

  for(i=0;i<n;i++){
    g[i]=INFTY;
    a[i]=0;
  }
  g[0]=0;
  a[0]=1;
  
  while(1){
    min=INFTY;
    e=-1;
    for(i=0;i<n;i++){
      if(min>g[i]&&a[i]!=2){
	e=i;
	min=g[i];
      }
    }
    if(e==-1)
      break;
    a[e]=2;
    for(v=0;v<n;v++){
      if(a[v]!=2&&A[e][v]!=INFTY){
	if(g[v]>g[e]+A[e][v]){
	  g[v]=g[e]+A[e][v];
	  a[v]=1;
	}
      }
    }
  }
  for(i=0;i<n;i++){
    printf("%d",i);
    printf(" ");
    if(g[i]==INFTY)
      printf("-1");
    else printf("%d",g[i]);
    printf("\n");
  }
}

C++: #include "bits/stdc++.h"
using namespace std;
int main()
{
    int n, inf = 1000000000;
    scanf("%d", &n);
    vector<vector<int>> a(n, vector<int>(n, inf));
    for (int i = 0, u, k; i < n; i++)
    {
        scanf("%d %d", &u, &k);
        for (int j = 0, v, c; j < k; j++)
        {
            scanf("%d %d", &v, &c);
            a[u][v] = c;
        }
    }
    vector<int> cost(n, inf);
    vector<bool> vis(n, false);
    cost[0] = 0;
    for (int mi = inf, tip = 0; ; mi = inf)
    {
        for (int i = 0; i < n; i++)
        {
            if (!vis[i] && cost[i] < mi)
            {
                mi = cost[i];
                tip = i;
            }
        }
        
        if (mi == inf)
        {
            break;
        }
        vis[tip] = true;
        for (int i = 0; i < n; i++)
        {
            
            if (!vis[i] && a[tip][i] != inf)
            {
                
                if (cost[tip] + a[tip][i] < cost[i])
                {
                    cost[i] = cost[tip] + a[tip][i];
                }
            }
        }
    }
    
    for (int i = 0; i < n; i++)
    {
        printf("%d %d\n", i, cost[i]);
    }
}