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 rep(i,n) for(int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
#define END return 0
typedef int64_t ll;
const int mod7 = 1e9 + 7;
const int mod9 = 1e9 + 9;
const int inf = 1 << 30;
const ll INF = 1LL << 60;
template<typename T> T gcd(T a, T b) {return b ? gcd(b, a % b) : abs(a);}
template<typename T> T lcm(T a, T b) {return a / gcd(a, b) * b;}
template<typename T> bool chmax(T &a, T b) {if (a < b) {a = b; return true;} return false;}
template<typename T> bool chmin(T &a, T b) {if (a > b) {a = b; return true;} return false;}
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) {fill((T*)array, (T*)(array+N), val);}

int n;
using P = pair<int, int>;
int l[100][100];
int d[100];

void dijkstra(int start) {
    fill(d, d + 100, inf);
    d[start] = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push({0, start});
    while(!que.empty()) {
        P p = que.top();
        que.pop();
        rep(i, n)
            if(chmin(d[i], p.first + l[p.second][i]))
                que.push({d[i], i});
    }
}

int main() {
    Fill(l, inf);
    cin >> n;
    rep(i, n) {
        int u, k;
        cin >> u >> k;
        rep(i, k) {
            int v, c;
            cin >> v >> c;
            l[u][v] = c;
        }
    }
    dijkstra(0);
    rep(i, n) cout << i << ' ' << d[i] << endl;
}

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

using namespace std;

#define INF (10000001)

typedef pair<int, int> Edge;

int main()
{
    priority_queue<Edge,vector<Edge>,greater<Edge> > pq;
    vector<vector<Edge> > e;
    vector<int> d;
    Edge etemp;
    int n,u,k,v,c;
    cin >> n;
    e.resize(n);
    d.resize(n);
    for(int i=0; i<n; ++i) {
        cin >> u >> k;
        for(int j=0; j<k; ++j) {
            cin >> v >> c;
            e[u].push_back(Edge{c,v});
        }
        d[u]=INF;
    }

    d[0]=0;
    for(auto x : e[0]) {
        pq.push(x);
    }
    while(!pq.empty()) {
        etemp=pq.top(); pq.pop();
        v=etemp.second;
        c=etemp.first;
        if(d[v]<c) continue;
        d[v]=c;
        for(auto x : e[v]) {
            if(d[x.second]<=c+x.first) continue;
            pq.push(Edge{c+x.first,x.second});
        }
    }
    for(int i=0; i<n; ++i) {
        cout << i << " " << d[i] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int cost[MAX_N][MAX_N], d[MAX_N];
bool used[MAX_N];
void dijkstra(int n)
{
    for(int i = 0; i <= n; i++) { d[i] = INF; used[i] = false; }
    d[0] = 0;
    while(true)
    {
        int v = -1;
        for(int u = 0; u < n; u++)
        {
            if(not used[u] && (v == -1 || d[u] < d[v])) v = u;
        }
        if(v == -1) break;
        used[v] = true;
        for(int u = 0; u < n; u++) d[u] = min(d[u], d[v] + cost[v][u]);
    }
    for(int i = 0; i < n; i++) cout << i << " " << d[i] << endl;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++) cost[i][j] = INF;
    }
    for(int i = 0; i < n; i++)
    {
        int u, k; cin >> u >> k;
        for(int i = 0; i < k; i++)
        {
            int v, c; cin >> v >> c;
            cost[u][v] = c;
        }
    }
    dijkstra(n);
    return 0;
}

C++: #include<iostream>
#include<vector>
#define MAX 1000000
using namespace std;
static const int N = 100;
int A[N][N],d[N],p[N],color[N]={0};
void algo(int);
void print(int);
int main(void){
    int n,u,k,v,c;
    cin >> n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j] = -1;
        }
    }
    for(int i=0;i<n;i++){
        cin >> u >> k;
        if(k!=0){
            for(int j=1;j<=k;j++){
                cin >> v >> c;
                A[u][v] = c;
            }
        }
    }
    for(int i=0;i<n;i++){
        d[i] = MAX;
        p[i] = 0;
    }
    d[0] = 0;
    algo(n);
    print(n);
    return 0;
}
void algo(int n){
    vector<int> S;
    while(S.size()!=n){
        int min = MAX,u =-1;
        for(int i=0;i<n;i++){
            if(min>d[i] && color[i]!=1){
                min= d[i];
                u= i;
            }
        }
        color[u] = 1;
        S.push_back(u);
        for(int v=0;v<n;v++){
            if(d[u]+A[u][v]<d[v] && A[u][v]!=-1){
                d[v] = d[u] + A[u][v];
                p[v] = u;
            }
        }
    }
}
void print(int n){
    for(int i=0;i<n;i++){
        cout << i << ' ' << d[i] << endl;
    }
}

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

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

const int INF = 1 << 21;
const int MAX = 103;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
int n, G[MAX][MAX];

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

  fill((int*)d, (int*)(d+MAX), INF);
  memset(color, WHITE, sizeof(color));

  d[0] = 0;
  color[0] = GRAY;
  while (true) {
    minv = INF;
    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 && G[u][v] != INF) {
	if (d[v] > d[u] + G[u][v]) {
	  d[v] = d[u] + G[u][v];
	  color[v] = GRAY;
	}
      }
    }
  }

  rep(i,n) {
    cout << i << ' ' << (d[i] == INF ? -1 : d[i]) << endl;
  }
}

int main() {
  cin >> n;
  rep(i,n) {
    fill((int*)G[i], (int*)(G[i]+n), INF);
  }
  
  int u, k, v, c;
  rep(i,n) {
    cin >> u >> k;
    rep(j,k) {
      cin >> v >> c;
      G[i][v] = c;
    }
  }

  dijkstra();
  return 0;
}