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への重み最小の親
	distFromS int         // 始点からの距離
	color     color
}

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, math.MaxInt32, 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].distFromS = 0

	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか

		var minNode *node
		min := math.MaxInt32
		for _, v := range nodes {
			if v.color == mst {
				continue
			}
			if v.distFromS < min {
				min = v.distFromS
				minNode = v
			}
		}
		if minNode == nil {
			// すべて完了している.
			break
		}

		// MSTに追加する頂点決定
		minNode.color = mst

		// fmt.Printf(">[%v]\n", minNode)
		// MSTに属していない頂点それぞれについて,Startからの最小の重みの距離を更新
		for i, v := range nodes {
			if v.color == mst {
				continue
			}
			w, ok := minNode.weights[i]
			if !ok {
				// minNodeと接してない
				continue
			}

			if minNode.distFromS+w < v.distFromS {
				// minNode経由の距離がもっとも重みが少ないので親を変更
				v.distFromS = minNode.distFromS + w
				v.parent = minNode
				// fmt.Printf("[%v %v]\n", v, minNode)
			}
		}
		// fmt.Printf("<\n")
	}

	for i, v := range nodes {
		fmt.Printf("%d %d\n", i, v.distFromS)
	}
}

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<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<cstring>
#define P pair<int,int>//cost to
using namespace std;

vector<P>rinsetu[100];
int mincost[100];
signed main() {
	int a; cin >> a;
	for (int b = 0; b < a; b++) {
		int c, d; cin >> c >> d;
		for (int e = 0; e < d; e++) {
			int f, g; scanf("%d%d", &f, &g);
			rinsetu[c].push_back(P(g,f));
		}
	}
	fill(mincost, mincost + a, 1 << 29);
	mincost[0] = 0;
	priority_queue<P, vector<P>, greater<P>>Q;
	Q.push(P(0,0));
	while (Q.size()) {
		P t = Q.top(); Q.pop();
		if (t.first > mincost[t.second])continue;
		for (P i : rinsetu[t.second]) {
			if (mincost[i.second] > t.first + i.first) {
				mincost[i.second] = t.first + i.first;
				Q.push(P(t.first + i.first,i.second));
			}
		}
	}
	for (int i = 0; i < a; i++) {
		cout << i << " " << mincost[i] << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <numeric>

#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define Sort(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
typedef long long int ll;
typedef std::pair<int,int> P;
int D=1;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

using namespace std;

int main(){
    int n;cin>>n;
    vector<vector<int>> v(n,vector<int>(n,INF));
    rep(i,0,n)v[i][i]=0;
    rep(i,0,n){
        int u,k; cin>>u>>k;
        rep(j,0,k){
            int to,cost;
            cin>>to>>cost;
            v[u][to]=cost;
        }
    }
    
    rep(i,0,n){
        rep(j,0,n){
            rep(k,0,n){
                v[j][k]=min(v[j][k],v[j][i]+v[i][k]);
            }
        }
    }
    rep(i,0,n){
        cout<<i<<" "<<v[0][i]<<endl;
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000000;
vector<long long> bellman_ford(vector<vector<pair<int, int>>> &E, int s){
    int V = E.size();
    vector<long long> d(V, INF);
    d[s] = 0;
    while (1){
        bool update = false;
        for (int i = 0; i < V; i++){
            for (int j = 0; j < E[i].size(); j++){
                int c = E[i][j].first;
                int v = E[i][j].second;
                if (d[v] > d[i] + c && d[i] != INF){
                    d[v] = d[i] + c;
                    update = true;
                }
            }
        }
        if (!update){
            break;
        }
    }
    return d;
}
int main(){
    int n;
    cin >> n;
    vector<vector<pair<int, int>>> E(n);
    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;
            E[i].push_back(make_pair(c, v));
        }
    }
    vector<long long> d = bellman_ford(E, 0);
    for (int i = 0; i < n; i++){
        cout << i << ' ' << d[i] << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct edge {int to, cost;};

int main(void){
    int n;
    cin >> n;
    vector<edge> d[n];
    rep(i, n){
        int u, k;
        cin >> u >> k;
        rep(j, k){
            int v, c;
            cin >> v >> c;
            d[u].push_back({v, c});
        }
    }
    vector<int> v(n);
    fill(v.begin(), v.end(), INT_MAX);
    priority_queue<P,vector<P>, greater<P>> q;
    q.push({0, 0});
    while(!q.empty()){
        P p = q.top(); q.pop();
        int g = p.second;
        if(v[g] < p.first) continue;
        v[g] = p.first;
        for(auto x : d[g]){
            if(v[x.to] > v[g] + x.cost)
                q.push({v[g] + x.cost, x.to});
        }
    }
    rep(i, n) cout << i << ' ' << v[i] << endl;
    return 0;
}

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

int n;
vector< vector<int> > G;
vector<int> color, d, p;

void Dijsktra() {
  color.resize(n), d.resize(n, 1000000000), p.resize(n, -1);
  d[0] = 0;

  while (true) {
    int min_cost = 1000000000, u;
    for (int i = 0; i < n; i++) if (color[i] != 2 && d[i] < min_cost) min_cost = d[i], u = i;
    if (min_cost == 1000000000) break;
    color[u] = 2;
    for (int v = 0; v < n; v++) {
      if (color[v] != 2 && G[u][v] != -1)
        if (d[u] + G[u][v] < d[v]) d[v] = d[u] + G[u][v], p[v] = u, color[v] = 1;
    }
  }
}

int main() {
  cin >> n;
  G.resize(n, vector<int>(n, -1));
  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;
      G[u][v] = c;
    }
  }

  Dijsktra();

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