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>
using namespace std;
int n;
static const int MAX = 100;
static const int INF = (1 << 21);
int M[MAX][MAX];
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;


void dijkstra(){
    int minv;
    int d[MAX], color[MAX];

    for (int i = 0; i < n; i++){
        d[i] = INF;
        color[i] = WHITE;
    }

    d[0] = 0;
    // color[]

    while(1){
        minv = INF;
        int u = -1;

        for (int i = 0; i < n; i++){
            if (minv > d[i] && color[i] != BLACK){
                minv= d[i];
                u = i;
            }
        }

        if (u == -1){ //全部探索済み
            break;
        }

        color[u] = BLACK;
        for (int i = 0; i < n; i++){
            if (color[i] != BLACK && d[i] > M[u][i] != INF){
                if (d[i] > d[u] + M[u][i]){
                    color[i] = GRAY;
                    d[i] = d[u] + M[u][i];
                }
            }
        }
    }
    for (int i = 0; i < n; i++){
            cout << i << " ";
            if (d[i] == INF){
                cout << -1 << endl;
            }else{
                cout << d[i] << endl;
            }
            
        }
}

int main(void){
    int tmp;

    cin >> n;

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

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

    return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

int d[100];

int main(void)
{
    int n;
    cin >> n;

    int graph[n][n];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            graph[i][j] = -1;

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

    for (int i = 0; i < n; i++)
        d[i] = 100000000;
    d[0] = 0;

    vector<int> queue;
    for (int i = 0; i < n; i++)
        queue.push_back(i);

    while (!queue.empty()) {
        int u = queue[0];
        size_t j = 0;
        for (size_t i = 0; i < queue.size(); i++)
            if (d[queue[i]] < d[u]) {
                u = queue[i];
                j = i;
            }
        queue.erase(queue.begin() + j);

        for (int v = 0; v < n; v++)
            if (graph[u][v] > 0)
                if (d[v] > d[u] + graph[u][v])
                    d[v] = d[u] + graph[u][v];
    }

    for (int i = 0; i < n; i++)
        cout << i << " " << d[i] << endl;

    
    return 0;
}

C++: #include<stdio.h>
#include<limits.h>
#define NIL -1
static const int MAX = 1005;
static const int INFTY = (1<<21);

main(){
  int n, i, j, v, c, u, k, minV;
  int M[MAX][MAX], A[MAX], key[MAX], p[MAX];
  /*your code*/

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

  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;
    }
  }

  /*your code*/
  for(i = 0; i < n; i++){
    key[i] = INT_MAX;
    p[i] = NIL;
    A[i] = NIL;
  }
  
  key[0]=0;
  
  while(1){
    
    u = NIL;
    
    minV = INT_MAX;
    
    for(i = 0;  i < n; i++){
      
      if(key[i] < minV && A[i] == NIL){
	
	u = i;
	
	minV = key[i];
	
      }
      
    }  
    if(u == NIL)break;
    
    A[u] = u; 
    
    for(v = 0; v < n; v++){
      if((M[u][v]+key[u]) < key[v] && A[v] == NIL){
	key[v] = (M[u][v]+key[u]);
	p[v] = u;
      }
    }
  }
  
  for ( i = 0; i < n; i++ ){
    printf("%d %d\n", i, (key[i]==INFTY?-1:key[i]));
  }

  return 0;

}

C++: #include <iostream>
using namespace std;
#define MAX 100
#define INFTY (1<<30)
#define rep(i,n) for(int i=0;i<n;i++)
enum{
	white,
	gray,
	black,
};

int n,m[MAX][MAX];

 void dijkstra(){
 	int minv;
 	int d[MAX],color[MAX];
 	rep(i,n){
 		d[i]=INFTY;
 		color[i]=white;
 	}
	d[0]=0;
 	color[0]=gray;
 	while(1){
 		minv=INFTY;
 		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]!=INFTY){
 				if(d[v]>d[u]+m[u][v]){
 					d[v]=d[u]+m[u][v];
 					color[v]=gray;
 				}
 			}
 		}
 	}
 	rep(i,n){
 		cout<<i<<" ";
 		if(d[i]==INFTY)	cout<<-1<<endl;
 		else	cout<<d[i]<<endl;
 	}
 }

 int main(){
 	cin>>n;
 	rep(i,n){
 		rep(j,n)		m[i][j]=INFTY;
 	}
 	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<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
typedef pair<int,int> P;
typedef pair<P,P> PP;

int main(void)
{
	int i,j,k,u,n,flg[10001],v,c,sum,ky[10001],t,min,g;
	PP ke[10001];
	scanf("%d",&n);
	sum=0;
	for(i=0;i<n;i++) ky[i]=M,flg[i]=0;
	ky[0]=0;
	for(i=0;i<n;i++){
		scanf("%d%d",&u,&k);
		for(j=sum;j<k+sum;j++){
			scanf("%d%d",&v,&c);
			ke[j].ff=u , ke[j].fs=v , ke[j].sf=c;
		}
		sum+=k;
	}
	sort(ke,ke+sum);
	//for(i=0;i<sum;i++) printf("ke[%d].ff=%d ke[%d].fs=%d ke[%d].sf=%d\n",i,ke[i].ff,i,ke[i].fs,i,ke[i].sf);
	for(i=0;i<sum;i++){
		/*min=M;
		for(j=0;j<sum;j++) if(flg[j]==0&&min>ky[j]) g=j,min=j;
		flg[g]=1;
		if(min==M) break;*/
		for(j=0;j<sum;j++){
			t=ky[ke[j].ff]+ke[j].sf;
			if(ky[ke[j].fs]>t) ky[ke[j].fs]=t;
		}
	}
	for(i=0;i<n;i++) printf("%d %d\n",i,ky[i]);
	return 0;
}