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

static const int MAX_NUM   = 101;
static const int NIL       = 100001;
static const int NO_VISIT  = 0;
static const int NOW_VISIT = 1;
static const int END_VISIT = 2;

void SSSP(int *pMatrix, int num)
{
    int i;
    int mincost;
    int target;
    int lcost[MAX_NUM];
    int parent[MAX_NUM];
    int state[MAX_NUM];
    int *pList;

    for (i = 0; i < num; i++) {
        lcost[i]  = NIL;
        parent[i] = -1;
        state[i]  = NO_VISIT;
    }

    lcost[0]  = 0;
    parent[0] = 0;
    state[0]  = NOW_VISIT;

    for (;;) {
        mincost   = NIL;
        for (i = 0; i < num; i++) {
            if ((lcost[i] < mincost) && (state[i] != END_VISIT)) {
                target = i;
                mincost = lcost[i];
            }
        }
        if (mincost == NIL) {
            break;
        }

        state[target] = END_VISIT;
        pList = pMatrix + MAX_NUM * target;
        for (i = 0; i < num; i++) {
            if ((*(pList + i) + lcost[target]) < lcost[i]) {
                lcost[i]  = *(pList + i) + lcost[target];
                state[i]  = NOW_VISIT;
                parent[i] = target;
            }
        }
    }

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

    return;
}

int main(void)
{
    int i, j;
    int num;
    int tmp;
    int target, elem, cost;
    int matrix[MAX_NUM][MAX_NUM];

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        for (j = 0; j < num; j++) {
            matrix[i][j] = NIL;
        }
    }

    for (i = 0; i < num; i++) {
        scanf("%d %d", &target, &tmp);
        for (j = 0; j < tmp; j++) {
            scanf("%d %d", &elem, &cost);
            matrix[target][elem] = cost;
        }
    }

    SSSP(&matrix[0][0], num);

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int INF   = (1 << 21);
static const int WHITE = 0;
static const int GRAY  = 1;
static const int BLACK = 2;
static const int NIL   = -1;
static const int MAX   = 100;

int n;
vector<vector<int>> M(MAX, vector<int>(MAX, NIL));

void dijstra(int s) {
  vector<int> color(n, WHITE);
  vector<int> d(n, INF);
  vector<int> p(n);
  d[s] = 0;
  p[s] = NIL;

  while (true) {
    int u       = 0;
    int mincost = INF;
    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u       = i;
      }
    }
    if (mincost == INF) { break; }

    color[u] = BLACK;

    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && M[u][v] != NIL) {
        if (d[u] + M[u][v] < d[v]) {
          d[v]     = d[u] + M[u][v];
          p[v]     = u;
          color[v] = GRAY;
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int id, k;
    cin >> id >> k;
    for (int j = 0; j < k * 2; j += 2) {
      int v, c;
      cin >> v >> c;
      M[id][v] = c;
    }
  }
  dijstra(0);
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int inf=0x3f3f3f3f;
int map1[109][109];
int flag[109];
int result[109];
int main(){
    int n;
    int u,v,w,k;
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(i==j)
                    map1[i][j]=0;
                else
                    map1[i][j]=inf;
        for(int i=0;i<n;i++){
            scanf("%d%d",&u,&k);
            for(int j=0;j<k;j++){
                scanf("%d%d",&v,&w);
                if(map1[u][v]>w)
                    map1[u][v]=w;
            }
        }
        u=108;
        memset(flag,0,sizeof(flag));
        flag[0]=1;
        for(int i=0;i<n;i++)
            result[i]=map1[0][i];
        for(int i=0;i<n;i++){
            int minn=inf;
            for(int j=0;j<n;j++)
                if(!flag[j]&&result[j]<minn){
                    minn=result[j];
                    u=j;
                }
                flag[u]=1;
                for(int k=0;k<n;k++)
                    if(map1[u][k]!=inf&&result[k]>result[u]+map1[u][k])
                        result[k]=result[u]+map1[u][k];
        }
        for(int i=0;i<n;i++)
            printf("%d %d\n",i,result[i]);



    }
    return 0;
}