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>
#define INF 10000000
#define NIL -1
#define BLACK 2
#define WHITE 0
#define GRAY 1
#define MAX 100
using namespace std;

int color[MAX]={},d[MAX]={},pi[MAX]={},n;

void initializeSingleSource(int w[MAX][MAX]){
    for(int v=0;v<n;v++){
        d[v]=INF;
        pi[v]=NIL;
    }
    d[0]=0;
}

void relax(int u,int v,int w[MAX][MAX]){
    if(d[v]>d[u]+w[u][v]){
        d[v]=d[u]+w[u][v];
        pi[v]=u;
    }
}

void dijkstra(int w[MAX][MAX]){
    int mincost,i,v,u;
    initializeSingleSource(w);
    while(1){
        mincost=INF;
        u=NIL;
        for(i=0;i<n;i++){
            if(color[i]!=BLACK&&d[i]<mincost){
                mincost=d[i];
                u=i;
            }
        }
        if(u==NIL)break;
        color[u]=BLACK;
        for(v=0;v<n;v++){
            if(color[v]!=BLACK&&d[u]+w[u][v]<d[v]){
                pi[v]=u;
                d[v]=d[u]+w[u][v];
                color[v]=GRAY;
            }
        }
    }
}

void prim(int w[MAX][MAX]){
    int u,i,v,mincost,j;
    for(u=0;u<n;u++){
        d[u]=INF;
        pi[u]=NIL;
        color[u]=WHITE;
    }
    d[0]=0;
    while(1){
        mincost = INF;
        u=-1;
        for(i=0;i<n;i++){
            if(color[i]!=BLACK&&d[i]<mincost){
                mincost=d[i];
                u=i;
            }
        }
        if(u==-1)break;
        color[u]=BLACK;
        for(v=0;v<n;v++){
            if(color[v]!=BLACK&&w[u][v]!=INF){
                if(d[v]>w[u][v]){
                    pi[v]=u;
                    d[v]=w[u][v];
                    color[v]=GRAY;
                }
            }
        }
    }
    int sum=0;
    for(i=0;i<n;i++){
        if(pi[i]!=NIL)sum+=w[i][pi[i]];
    }

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

    cout<<endl;

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

int main(){
    int w[MAX][MAX],i,j,id,k,weight,gow,ans;
    cin>>n;
    for(i=0;i<MAX;i++){
        for(j=0;j<MAX;j++){
            w[i][j]=INF;
        }
    }
    for(i=0;i<n;i++){
        cin>>id>>k;
        for(j=0;j<k;j++){
            cin>>gow>>weight;
            w[id][gow]=weight;
        }
    }
    dijkstra(w);
    for(i=0;i<n;i++){
        cout<<i<<" "<<d[i]<<endl;
    }
}

C++: #include<iostream>
#include<iomanip>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<sstream>
using namespace std;
 
#define P(p) cout<<(p)<<endl
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(),greater<int>());
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return
#define lb(v,n) lower_bound(v.begin(),v.end(),n)
#define ub(v,n) upper_bound(v.begin(),v.end(),n)
#define mae(v) max_element(v.begin(),v.end())
#define mie(v) min_element(v.begin(),v.end())
#define mod 1000000007
typedef long long ll;
////////////////////////////////////////////////////////////

typedef struct edge_{
	int to,cost;
}edge;

class graph{ 
	typedef long cost_type;
	typedef int edge_type;
	typedef pair<cost_type,edge_type> P; 	//first:最短経路コスト second:頂点番号
	
	
	private:
		vector<vector<edge> > gra; 	//グラフ本体
		priority_queue<P,vector<P>,greater<P> > que; 	//最短経路の値の小さいものから取り出せる
		edge_type V; 	//頂点の個数
	  vector<cost_type> dp; 	//開始地点からの最小コストを格納する配列
	
	public:
		void init(int num){
			V = num;
			dp.assign(V,0x7FFFFFFF);
			gra.assign(V,vector<edge>());
		}


		void setCost(edge_type from , edge_type to , edge_type cost){
			edge e = {to , cost};
			edge e2 = {from , cost};
			gra[from].push_back( e );
			gra[to].push_back(e2);
		}	

		
		void setDirectCost(edge_type from , edge_type to , edge_type cost){
			edge e = {to , cost};
			gra[from].push_back( e );
		}	


		void dijkstra(edge_type s){
			dp[s] = 0;
			que.push(pair<cost_type,edge_type>(0,s)); 

			while(!que.empty()){
				P p = que.top();
				que.pop();
				edge_type v = p.second;
				if( dp[v] > p.first ) continue;
				for(edge_type i = 0 ; i < gra[v].size() ; i++ ){
					edge e = gra[v][i];
					if( dp[e.to] > dp[v] + e.cost ){
						dp[e.to] = dp[v] + e.cost;
						que.push(pair<cost_type,edge_type>(dp[e.to],e.to));
					}
				}
			}
		}

		cost_type getCost(edge_type to){
			return dp[to];
		} 

};

int main(){
	graph g;
	int n;
	cin >> n;
	g.init(n);
	rep(i,0,n){
		int from , k;
		cin >> from >> k;
		rep(i,0,k){
			int to;
			long cost;
			cin >> to >> cost;
			g.setDirectCost(from,to,cost);
		}

	}

	g.dijkstra(0);

	rep(i,0,n)
		cout << i << " " << g.getCost(i) << endl;

	ret 0;
}