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;
#define MAX_V 100
#define INF (1 << 21)

struct edge { int to, cost; };
typedef pair<int, int> P;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

// int cost[MAX_V][MAX_V];
// bool used[MAX_V];

int s = 0; // start

void dijkstra()
{
    priority_queue<P, vector<P>, greater<P> > que;
    for (int i = 0; i < V; i++) {
        d[i] = INF;
        // used[i] = false;
    }
    d[s] = 0;
    que.push(P(0, s));

    for (; !que.empty();) {
        P p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[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));
            }
        }
    }

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

}
int main()
{
    cin >> V;

    // for (int i = 0; i < V; i++) {
    //     for (int j = 0; j < V; j++) {
    //         cost[i][j] = INF;
    //     }
    // }

    for (int i = 0; i < V; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v, c;
            cin >> v >> c;
            G[u].push_back({v, c});
            // cost[u][v] = c;
        }
    }

    dijkstra();

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxx=110;
const int infinity=(1<<21);

int A[maxx][maxx];
int d[maxx];
bool flag[maxx];

int main (){
	int n,a,b,c,minv,mini;
	cin>>n;
	for(int i=0;i<maxx;i++){
		flag[i]=false;
		d[i]=infinity;
		for(int j=0;j<maxx;j++){
			A[i][j]=infinity;
		}
	}
	for(int i=0;i<n;i++){
		cin>>a>>b;
		for(int j=0;j<b;j++){
			cin>>c>>A[a][c];
		}
	}
	
	d[0]=0;
	while(1){
		minv=infinity,mini=-1;
		for(int i=0;i<n;i++){
			if(flag[i]==false && d[i]<minv){
				minv=d[i];
				mini=i;
			}
		}
		if(mini==-1 || minv==infinity) break;
		flag[mini]=true;
		for(int i =0;i<n;i++){
			if(A[mini][i]!=infinity && flag[i]==false && d[mini]+A[mini][i]<d[i]){
					d[i]=d[mini]+A[mini][i];
			}
		}
		
	}
	cout<<n<<endl;
	int nn=n;
	for(int i=0;i<nn;i++) cout<<i<<" "<<d[i]<<endl;
	
	return 0;

}

C++: #include <iostream>

using namespace std;

int n;
int a[124][124] = {{}};
static const int INFTY = 1000000;

void dijkstra(){
  int flag[124] = {};
  int d[124] = {};
  int p[124] = {};
  int minv;
  int u;

  for (int i = 0;i < n;i++){
    d[i] = INFTY;
    p[i] = -1;
  }
  d[0] = 0;
  while(1){
    minv = INFTY;
    u = -1;

    for(int i = 0;i < n;i++){
      if(flag[i] == 0 && minv > d[i]){
        minv = d[i];
        u = i;
      }
    }
  //  cout << "u = " << u << endl;
  //  cout << "minv = " << minv << endl;
    if(u == -1) break;
    flag[u] = 1;

    for(int i = 0;i < n;i++){
      if(d[i] > d[u] + a[u][i] && a[u][i] != INFTY && flag[i] == 0){
        d[i] = d[u] + a[u][i];
        p[i] = u;
      }
    }
  }

  for(int i = 0;i < n;i++){
    if(d[i] != -1){
      cout << i <<  " " << d[i] << endl;
    }
    else cout << i << " " << -1 << endl;
  }
}



int main(){

  cin >> n;

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


  int u;
  int k;
  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for (int j = 0;j < k;j++){
      int v;
      cin >> v;
      cin >> a[u][v];
    }
  }
  dijkstra();
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 200001 
using namespace std;
int n;
int A[1000][1000];
int D[1000][1000];
 
void Init(int f){
  for(int i=0;i<f;i++){
    for(int j=0;j<f;j++)D[i][j] = INF;
  } 
}
 
int main(){
  scanf("%d",&n);
  int u,k,C,V;
  Init(n);
  
  for(int i=0;i<n;i++){
      scanf("%d %d",&u,&k);    
      for(int j=0;j<k;j++){
    scanf("%d %d",&V,&C);
    D[u][V] = C;
   
      }    
  }
  for(int i=0;i<n;i++)D[i][i] = 0;

  for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j]= min(D[i][j],D[i][k] + D[k][j]);

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

C++: #include <iostream>
#include <queue>
#include <utility>
#include <vector>
#include <functional>
using namespace std;
int cost[100][100];
const int INF = 10000000;
int dijkstra(int n,int s,int g){
	typedef pair<int,int> P;
	int mincost;
	{priority_queue<P,vector<P>,greater<P> > pq;
	P p,tp;
	for(int i = 0;i < n;++i){
		p.first = cost[s][i];
		p.second = i;
		if(i == s) p.first = 0;
		pq.push(p);
	}
	while(!pq.empty()){
		p = pq.top();
		pq.pop();
		if(p.second == g) break;
		for(int i = 0;i < n;++i){
			if(cost[p.second][i] != INF && p.first != INF){
				tp.first = p.first + cost[p.second][i];
				tp.second = i;
				pq.push(tp);
			}
		}
	}
	mincost = p.first;
	}
	return mincost;
}
int main(){
	int n,m,a,b,c;
	cin >> n;
	for(int i = 0;i < 100;++i){
		for(int j = 0;j < 100;++j){
			cost[i][j] = INF;
		}
	}
	for(int i = 0;i < n;++i){
		cin >> a >> m;
		for(int j = 0;j < m;++j){
			cin >> b >> c;
			cost[a][b] = c;
		}
	}
	for(int i = 0;i < n;++i){
		cout << i << " " << dijkstra(n,0,i) << endl;
	}
	return 0;
}