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 <iostream>
#include <vector>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2
static const int INF = 1<<21;
int main(int argc, const char * argv[]) {
    int n;
    cin >> n;
    int G[n][n];
    int d[n];
    int p[n];
    int color[n];
    for(int i=0;i<n;i++){
        for (int j=0; j<n; j++) {
            G[i][j] = -1;
        }
    }
    for(int i=0;i<n;i++){
        int u,k;
        cin >> u >> k;
        for(int j=0;j<k;j++){
            int v,c;
            cin >> v >> c;
            G[u][v] = c;
        }
    }
    
    for (int i=0; i<n; i++) {
        color[i] = WHITE;
        p[i] = -1;
        d[i] = INF;
    }
    
    while(true){
        d[0] = 0;
        p[0] = 0;//?
        int u;
        int mincost = INF;
        //???????????????????±????
        for(int i=0;i<n;i++){
            if(d[i]<mincost && color[i]!=BLACK){
                mincost = d[i];
                u = i;
            }
        }
        color[u] = BLACK;
        if(mincost==INF) break;//?¶????????????????????????°??±?????£????????¨???
        
        for(int v=0;v<n;v++){
            if(G[u][v] != -1 && d[v]>d[u]+G[u][v]){
               d[v] = d[u] + G[u][v];
               color[v] = GRAY;//??????????????????
               p[v] = u;
            }
        }
    }
    for(int i=0;i<n;i++){
        cout <<  i << " " << d[i] << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define INF 1 << 30
typedef pair<int, int> P;

int n, M[100][100], d[100];

void dijkstra(void) {
    int i, u, v;
    P t;
    priority_queue<P, vector<P>, greater<P> > pq;

    pq.push(make_pair(0, 0));
    while (!pq.empty()) {
        t = pq.top(); pq.pop();
        v = t.first;
        u = t.second;

        if (d[u] != INF) continue;
        d[u] = v;

        for (i = 0; i < n; i++) {
            if (d[i] != INF || M[u][i] == INF) continue;
            pq.push(make_pair(v + M[u][i], i));
        }
    }
}

int main(void) {
    int i, j, u, k, v, c;
    scanf("%d", &n);

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

    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        for (j = 0; j < k; j++) {
            scanf("%d %d", &v, &c);
            M[u][v] = c;
        }
    }

    dijkstra();

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

    return 0;
}

C++: #include <iostream>
using namespace std;
int n,m[100][100],d[100];
void mins(){
    int u,min,color[100];
    for(int i=0;i<n;i++){
        d[i]=1000000000;
        color[i]=1;
    }
    d[0]=0;
    while(1){
        min=1000000000;
        u=-1;
        for(int i=0;i<n;i++){
            if(color[i]!=3&&d[i]<min){
                u=i;
                min=d[i];
            }
        }
        if(u==-1){
            break;
        }
        color[u]=3;
        for(int i=0;i<n;i++){
            if(m[u][i]!=-1){
                if(d[i]>min+m[u][i]){
                    d[i]=min+m[u][i];
                }
            }
        }
    }
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            m[i][j]=-1;
        }
    }
    int a,k,c,v;
    for(int i=0;i<n;i++){
        cin>>a>>k;
        for(int j=0;j<k;j++){
            cin>>c>>v;
            m[a][c]=v;
        }
    }
    mins();
    for(int i=0;i<n;i++){
        cout<<i<<" "<<d[i]<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int x[1000][1000];
int y[1000];

int main() {
	memset(x, 127, sizeof(x));
	memset(y, 127, sizeof(y));
	int n, a, b, c, d;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a >> b;
		for (int i = 0; i < b; i++) {
			cin >> c >> d;
			x[a][c] = d;
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < n; k++) {
				y[k] = min(y[k], y[j] + x[j][k]);
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << i << ' ' << y[i] << endl;
	}
	return 0;
}

C++: #define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#include <bits/stdc++.h>
using namespace std;

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 1000000010

int n;
int M[100][100];
int color[100],d[100],p[100];

void dik(int s){
  
  rep(i,n){
    color[i]=WHITE;
    d[i]=INF;
    p[i]=-1;
  }
  d[s]=0;
  
  int minc,u;
  while(1){
    minc=INF; 
    rep(i,n){
      if(color[i] != BLACK && d[i]<minc){
        minc=d[i];
        u=i;
      }
    }
    
    if(minc==INF) break;
    color[u]=BLACK;
    
    rep(v,n){
      if(color[v] != BLACK && M[u][v] != -1){
        if(d[u]+M[u][v]<d[v]){
          d[v]=d[u]+M[u][v];
          p[v]=u;
          color[v]=GRAY;
        }
      }
    }    
  }
  
}

int main(){
  cin>>n;
  
  rep(i,n){
    rep(j,n) M[i][j]=INF;
  }
  
  int x,y,z;
  rep(i,n){
    cin>>x>>y;
    rep(j,y){
      cin>>z;
      cin>>M[x][z];
    }
  }
  
  dik(0);
  rep(i,n) cout<<i<<" "<<d[i]<<endl;
  
  return 0;
}