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>
#include<vector>
#include<queue>
#include<tuple>
#include<climits>
using namespace std;

int main(){
  priority_queue <tuple<int,int>,vector<tuple<int,int> >,greater<tuple<int,int> > > checkpoint;
  vector<vector<int> > path;
  vector<int> min_distance;
  int n;

  cin >> n;
  path.resize(n);
  for(int i=0;i<n;i++){
    path[i].resize(n);
    for(int j=0;j<n;j++){
      path[i][j]=INT_MAX;
    }
  }
  min_distance.resize(n);
  for(int i=0;i<n;i++){
    min_distance[i]=INT_MAX;
  }

  for(int i=0,from,to,cost,m;i<n;i++){
    cin >> from >> m;
    for(int j=0;j<m;j++){
      cin >> to >> cost;
      path[i][to]=cost;
    }
  }

  checkpoint.push(make_tuple(0,0));
  while(!checkpoint.empty()){
    int from=get<1>(checkpoint.top());
    int distance=get<0>(checkpoint.top());

    if(distance<min_distance[from]){
      min_distance[from]=distance;
      for(int i=0;i<n;i++){
        if(path[from][i]!=INT_MAX){
          checkpoint.push(make_tuple(path[from][i]+distance,i));
	}
      }
    }
    checkpoint.pop();
  }

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

C++: // ALDS1_12_B.cpp
// Graph II - Single Source Shortest Path I
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;

struct Node {
    int v;
    int c;
    Node(int v, int c): v(v), c(c) {}
};

const int maxN = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int INF = (1<<21);
vector<Node> G[maxN];
vector<int> dist(maxN, INF);
vector<int> p(maxN);
vector<int> color(maxN, WHITE);
int n;
int cnt = 0;
void dijkstra(int x)
{
    dist[x] = 0;
    p[x] = -1;
    while(++cnt != n) {
        int u = 0;
        int mindist = INF;
        for (int i = 0; i < n; i++) {
            if (color[i] != BLACK && dist[i] < mindist) {
                mindist = dist[i];
                u = i;
            }
        }

        for (int i = 0; i < G[u].size(); i++) {
            if (color[u] == BLACK) continue;
            int v = G[u][i].v;
            int c = G[u][i].c;
            if (dist[u] + c < dist[v]) {
                dist[v] = dist[u] + c;
                p[v] = u;
                color[v] = GRAY;
            }
        }
        color[u] = BLACK;
    }
}

int main()
{
    cin >> n;
    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;
            G[i].push_back(Node(v, c));
        }
    }
    dijkstra(0);
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i, dist[i]);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
//#define ll long long
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define all(x) (x).begin(),(x).end()
ll t1,t2,t3;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;

int main(){
  ll n;
  cin >> n;
  vector<ll> ans(n,INFLONG);
  vector<pair<ll,ll>> vec[n];
  ll u;
  ll k;
  rep(_,n){
    cin >> u >> k;
    rep(i,k){
      cin >> t1 >> t2;
      vec[u].emplace_back(make_pair(t2,t1)); //cost , node_num
    }
  }
  priority_queue<pair<ll,ll>> pq;
  ans[0] = 0;
  for(auto itr:vec[0]){
    pq.push(make_pair(-itr.first,itr.second)); //-cost
  }
  pair<ll,ll> pa;
  while(!pq.empty()){
    pa = pq.top();
    pq.pop();
    t1 = -pa.first; //cost
    t2 = pa.second;
    //cout << t1 << " " << t2 << endl;
    if(t1 < ans[t2]){
      ans[t2] = t1;
      for(auto itr:vec[t2]){
        pq.push(make_pair(-t1 - itr.first,itr.second));
      }
    }
  }
  rep(i,n){
    cout << i << " " << ans[i] << endl;
  }
}

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

#define MAX_VERTEX 100
#define INF 1000000

int vertex_num;   // ???????????°

int cost[MAX_VERTEX][MAX_VERTEX]; // ????????????cost[?§????][??????]??§?§?????????????????????????????????¨???
int d[MAX_VERTEX];                // ????????????d[u]??§?§??????¨u??????????????¢?????¨??????
bool used[MAX_VERTEX];            // ????????????????????£????????¢????±???????????????????

void Input() {
	cin >> vertex_num;

	// (a1) ??\???????????¶??§?????????
	// d??¨cost?????¨???INF
	// used????????????false
	for (int i = 0; i < vertex_num;i++){
		d[i] = INF;
		used[i] = false;
		for (int j = 0; j < vertex_num;j++){
			cost[i][j] = INF;
		}
	}

	for (int i = 0; i < vertex_num; i++) {
		int from, out_num;
		cin >> from >> out_num;
		for (int j = 0; j < out_num; j++) {
			int to, c;
			cin >> to >> c;
			cost[from][to] = c;
		}
	}
}

void Dijkstra() {
	// (b1) ?§????????????????
	d[0] = 0;
	while (true) {
		// ????????????????????¨???
		int vertex = -1;

		// (b2) ????????????????????£????????¢????±??????????????????????
		// ?????????????????????????????????????????§????????¢???????°???????????????¢???
		for (int i = 0; i < vertex_num; i++){
			if (!used[i] && (vertex == -1 || d[i] < d[vertex])){
				vertex = i;
			}
		}

		// ??¨????????????????????????????¨?????????????????????´????????????????????????
		if (vertex == -1) break;

		used[vertex] = true;

		// ???????????¢????¨????????????????
		for (int u = 0; u < vertex_num; u++) {
			d[u] = min(d[u], d[vertex] + cost[vertex][u]);
		}

	}

}

void Output() {
	for (int i = 0; i < vertex_num; i++) {
		cout << i << " " << d[i] << endl;
	}
}

int main() {

	Input();
	Dijkstra();
	Output();

	return 0;
}