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<stdio.h>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<queue>
#include<functional>
using namespace std;
int main(){
	int a;
	int b;
	int s;
	int t;
	vector<int> n[10000];
	int nn[10000];
	vector<int> x[10000];
	priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >y;
	vector<pair<int, int> >::iterator yy;
	int z;
	int d[10000];
	int e;
	int c[10000];
	int zz;
	pair<int, int>pai;
	for (int i = 0; i < 10000; i++){
		d[i] = 1000000009;
		c[i] = 0;
	}
	scanf("%d", &a);
	for (int i = 0; i < a; i++){
		scanf("%d %d", &s, &b);
		for (int j = 0; j < b; j++){
			scanf("%d %d", &t, &e);
			n[s].push_back(t);
			nn[s]++;
			x[s].push_back(e);

		}
	}
	d[0] = 0;
	y.push(std::make_pair(d[0], 0));
	for (int i = 0; y.empty() == 0; i++){
		zz = 0;
		for (int j = 0; y.empty() == 0 && zz == 0; j++){
			pai = y.top();
			z = pai.second;
			if (c[z] == 0)zz++;
			else y.pop();
		}
		if (zz == 0)break;
		c[z] = 1;
		for (int j = 0; j < nn[z]; j++){
			if (d[z] + x[z][j] < d[n[z][j]])
			{
				d[n[z][j]] = d[z] + x[z][j];
				y.push(std::make_pair(d[n[z][j]], n[z][j]));
			}
		}
		y.pop();
	}
	for (int i = 0; i<a; i++)printf("%d %d\n",i,d[i]);
		return 0;
}

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

static const int MAX =100;
static const int INF=(1<<21);
static const int WHITE =0;
static const int GRAY =1;
static const int BRACK =2;

int n,A[MAX][MAX];

void dij(){
  int minv,d[MAX],c[MAX];

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

  d[0]=0;
  c[0]=GRAY;
  
  while(1){
    minv=INF;
    int x=-1;
    for(int i=0;i<n;i++){
      if(minv>d[i] && c[i]!=BRACK){
	x=i;
	minv=d[i];
      }
    }
    if(x==-1)break;
    c[x]=BRACK;
    for(int v=0;v<n;v++){
      if(c[v]!=BRACK && A[x][v]!=INF){
	if(d[v]>d[x]+A[x][v]){
	  d[v]=d[x]+A[x][v];
	 
	  c[v]=GRAY;
	}
      }
    }
  }
  for(int i=0;i<n;i++){
    cout<<i<<" "<<(d[i]==INF ? -1: d[i])<<endl;
  }
}
 

int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      A[i][j]=INF;
    }
  }
  int k,c,u,v;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
      cin >> v >> c;
      A[u][v]=c;
    }
  }
  dij();
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define pb push_back
#define fi first
#define se second

#define MAX_V 110
#define INF 1e9

struct edge{int to,cost;};
typedef pair<int,int> P;
int d[MAX_V];
int V;
vector<edge> G[MAX_V];

edge makeedge(int t,int c){
    edge e;
    e.to=t;
    e.cost=c;
    return e;
}

void addedge(int s,int t,int c){
    G[s].push_back(makeedge(t,c));
    //G[t].push_back(makeedge(s,cost));
}

void dijkstra(int s){
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d,d+V,INF);
    d[s]=0;
    que.push(P(0,s)); 

    while(!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));
            }
        }
    }
}


int main(){
    int i,j,n;
    cin>>n;
    V=n;
    int u,k;
    int x,y;
    for(i=0;i<n;i++){
        cin>>u>>k;
        for(j=0;j<k;j++){
            cin>>x>>y;
            addedge(i,x,y);
        }
    }
    dijkstra(0);
    for(i=0;i<n;i++){
        cout<<i<<" "<<d[i]<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>

using namespace std;

class SSSPath
{
public:
  vector<int> v;
  int solve();
};

int SSSPath::solve()
{
  int n;
  cin >> n;

  const int infty = 20000;
  vector<int> d( n, infty );
  
  d[0] = 0;

  vector<vector<int>> adj( n, vector<int>( n, -1 ) );


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

  vector<int> sv( n, true );

  while ( true )
    {
      int md = infty;
      int u;
      
      for ( auto i = 0; i < n; ++i )
        {
          if ( sv[i] && d[i] < md )
            {
              u = i;
              md = d[i];
            }
        }

      if ( md == infty ) break;

      sv[u] = false;
      
      for ( auto i = 0; i < n; ++i )
        {
          if ( sv[i] && adj[u][i] >= 0 )
            {
              if ( d[u] + adj[u][i] < d[i] )
                {
                  d[i] = d[u] + adj[u][i];
                  
                }
            }
          
        }
    }

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

int main()
{
  SSSPath s;

  s.solve();

  return 0;
}