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>

#define MAX 100
#define INF 1000000000
#define w 0
#define x 1
#define z 2

int n, m[MAX][MAX];

void data(){
  int min,d[MAX],a[MAX],i,u;
  for(i = 0 ;i< n;i++){
    d[i] = INF;
    a[i] = INF;
  }
  d[0]=0;
  a[0]=x;
  while(1){
    min = INF;
    u = -1;
    for(i = 0;i<n;i++){
      if(min > d[i] && a[i] != z){
        u = i;
        min = d[i];
      }
    }
    if(u == -1) break;
    a[u] = z;
    for(i = 0;i<n;i++){
      if(a[i]!= z &&m[u][i] != INF){
        if(d[i] > d[u] + m[u][i]){
          d[i] = d[u] + m[u][i];
        }
      }
    }
  }
  int v;
  for(i = 0 ;i< n;i++){
    v = d[i];
    if(d[i] == INF) v = -1;
    printf("%d %d\n",i,v);
  }
}

int main(){
  scanf("%d",&n);
  int i,j,k,c,u,v;
  for(i = 0;i<n;i++){
    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;
    }
  }
  data();
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define INF 1<<21
int main()
{
	long long int min,n,u,k,v,c,dist[100][100],s,cost[100],t[100],l=1;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			dist[i][j]=INF;
			cost[i]=INF;
			t[i]=-1;
		}
	}
	min=INF;
	dist[0][0]=0;
	for(int i=1;i<=5;i++)
	{
		cin>>u;
		cin>>k;
		for(int i=1;i<=k;i++)
		{
			cin>>v>>c;
			dist[u][v]=c;
		}
	}
	s=0;
	cost[s]=0;
	while(l!=n)
	{
		for(int i=0;i<n;i++)
		{
			if(dist[s][i]!=INF)
			{
				if(cost[s]+dist[s][i]<cost[i])
				{
					cost[i]=cost[s]+dist[s][i];
				}
				
			}
		}
		t[s]=1;
		for(int i=1;i<n;i++)
		{
			if(cost[i]<min&&t[i]==-1)
			{
				min=cost[i];
				s=i;
			}
		}
		min=INF+1;
		l++;
	}
	for(int i=0;i<n;i++)
	{
		cout<<i<<" "<<cost[i]<<endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
const int inf = 1e9;
// ダイクストラO(elogv)
struct edge{
  int to, cost;
  edge(int t, int c) {
    to = t, cost = c;
  }
};
typedef pair<int, int> P;
int N;
vector<edge> G[111];
int d[111];
void dijkstra(int s){
  priority_queue<P, vector<P>, greater<P> >que;
  FOR(i,1,N+1) d[i] = 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(auto &e : G[v]){
      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()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin>>N;
  FOR(i,0,N){
    int u, k;
    cin >> u >> k;
    FOR(j,0,k){
      int v, c;
      cin >> v >> c;
      G[u].push_back(edge(v, c));
    }
  }
  dijkstra(0);
  FOR(i,0,N){
    cout << i << " " << d[i] << endl;
  }
}

C++: #include<iostream>
#include<vector>
#include<deque>
#include<queue> 
#include<functional>
#include<climits>

//#define INF 2147483647
#define INF INT_MAX

using namespace std;

int n;
priority_queue<pair<int, int>,  deque<pair<int,int>>, greater<pair<int,int>>> q;
vector<vector<int>> c;
vector<int> d;


int input(){
  cin >> n;
  int u = 0, k = 0, v = 0;
  c.resize(n);
  for( int i = 0; i < n; i++ ){
    cin >> u >> k;
    c[u].resize(n, -1);
    for( int j = 0; j < k; j++ ){
      cin >> v;
      cin >> c[u][v];
    }
  }
  d.resize(n, INF);
  return 0;
}

int checkContinue(){
  for( int i = 0; i < n; i++ ){
    if( d[i] == INF ){ return 1; }
  }
  return 0;
}

int run(){
  q.emplace(pair<int,int>(0,0));
  while( checkContinue() ){
    pair<int, int> p = q.top();
    q.pop();
    if( d[p.second] < p.first ){ continue; }
    d[p.second] = p.first;
    for( int i = 0; i < n; i++ ){
      if( c[p.second][i] < 0 ){ continue; }
      q.push(pair<int,int>(p.first+c[p.second][i], i));
      // cout << p.first+c[p.second][i] << "," << i << endl;
    }
  }
  for( int i = 0; i < n; i++ ){
    cout << i << " " << d[i] << endl;
  }
  return 0;
}

int main(){
  input();
  run();
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#define WHITE 0
#define BLACK 1
#define GRAY 2
using namespace std;
const int MAXN = 10000 + 4;
const int INF = 0x3f3f3f3f;
int status[MAXN], d[MAXN];
int N;
vector< pair<int, int> > ADJ[MAXN];
void Dijkstra (  )
{
    priority_queue< pair<int, int> > PQ;
    memset( status, WHITE, sizeof(status) );
    for ( int i = 0; i < N; i++ ) d[i] = INF;
    d[0] = 0;
    status[0] = GRAY;
    PQ.push(make_pair(0, 0));

    while ( !PQ.empty() ) {
        pair<int, int> f = PQ.top(); PQ.pop();
        int u = f.second;
        status[u] = BLACK;

        if ( d[u] < f.first * (-1) ) continue;

        for ( int j = 0; j < ADJ[u].size(); j++ ) {
            int v = ADJ[u][j].first;
            if ( status[v] == BLACK ) continue;
            if ( d[v] > d[u] + ADJ[u][j].second ){
                d[v] = d[u] + ADJ[u][j].second;
                PQ.push(make_pair(d[v] * (-1), v));
                status[v] = GRAY;
            }
        }
    }
    for ( int i = 0; i < N; i++ ) {
        cout<<i<<' '<<( d[i] == INF ? -1 : d[i] )<<endl;
    }
}

int main()
{
    int u, D, c, v;
    cin>>N;
    for ( int i = 0; i < N; i++ ) {
        cin>>u>>D;
        for ( int j = 0; j < D; j++ ) {
            cin>>v>>c;
            ADJ[u].push_back(make_pair(v, c));
        }
    }

    Dijkstra();
    return 0;
}