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++: //最短経路問題をダイクストラ法で解く。
//隣接行列表現を用いる。
//計算量O(v^2) (vは頂点の数)

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

#define MAX 105
#define INFTY 10000000
#define WHITE 0
#define GRAY 1
#define BLACK 2

//Mは隣接行列
int n, M[MAX][MAX], color[MAX];

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

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

    d[0] = 0;
    color[0] = WHITE;

    while(1){
        minv = INFTY;
        int u = -1;
        for(int i=0; i<n; i++){
            if(minv > d[i]  &&  color[i] != BLACK){
                u = i;
                minv = d[i];
            }
        }
        if(u == -1) break;
        color[u] = BLACK;
        for(int v=0; v<n; v++){
            if(color[v] != BLACK  &&  M[u][v] != INFTY){
                if(d[v] > d[u] + M[u][v]){
                    d[v] = d[u] + M[u][v];
                    color[v] = GRAY;
                }
            }
        }
    }

    for(int i=0; i<n; i++){
        if(d[i] == INFTY) cout << i << " -1" << endl;
        else cout << i << " " << d[i] << endl;
    }
}

int main(){
    cin >> n;
    //隣接リスト表現→隣接行列表現への変換
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            M[i][j] = INFTY;
        }
    }
    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;
            M[u][v] = c;
        }
    }

    dijkstra();

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<climits>
using namespace std;
 
#define N 1000000
int n;
 
int g[N][N];
 
int weight[N];
bool us[N];
 
//prim
void prim(){
 
  int v;
  
  weight[0]=0;
   
  while(true){
    v=-1;
    
    for(int i=0;i<n;i++){
      if(us[i]!=true && (v==-1 ||weight[i]<weight[v]))v=i;
    }
 
    if(v==-1)break;
    us[v]=true;
  
 
    for(int i=0;i<n;i++){
      if(weight[i]>(g[v][i]+weight[v])){
 
        weight[i]=(g[v][i]+weight[v]);
        //      cout <<"fsafa"<<endl;
      }
    }
  }
  
}
 
 
int main(){
 
  int u,k1,c,v;
 
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      g[i][j]=9953535;
    }
  }
 
  //load n
  cin >>n;
 
  //load data
  for(int i=0;i<n;i++){
    weight[i]=INT_MAX;
    us[i]=false;
    scanf("%d %d",&u,&k1);
    for(int j=0;j<k1;j++){
      scanf("%d %d",&v,&c);
      g[u][v]=c;
      //    cout <<g[i][j]<<" ";
    }
    //    cout <<endl;
  }
 
 
 
 
  //print
  prim();
 
  for(int i=0;i<n;i++){
    cout <<i<<" "<<weight[i]<<endl;
  }
  return 0;
 
}

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

class link_vector{
	std::vector<int> vec;
	std::vector<int> &ind;
public:
	link_vector(std::vector<int> &ind): ind(ind) {}
	void print(){
		for(int i : vec){
			std::cout << i << " ";
		}
		std::cout << std::endl;
		for(int i : ind){
			std::cout << i << " ";
		}
		std::cout << std::endl;
	}
	void enlarge(){
		vec.push_back(int());
	}
	void reduce(){
		vec.pop_back();
	}
	void substitute(const int &i, const int &x){
		vec[i] = x;
		ind[x] = i;
	}
	int operator[](const int &i){
		return vec[i];
	}
	int size(){
		return vec.size();
	}
	int front(){
		return vec.front();
	}
	int back(){
		return vec.back();
	}
	bool empty(){
		return vec.empty();
	}
};

class heap{
	link_vector vec;
	std::function<bool(int, int)> fnc;
public:
	heap(std::vector<int> &ind, std::function<bool(int, int)> fnc): vec(ind), fnc(fnc) {}
	void push(int x){
		vec.enlarge();
		chmin(vec.size() - 1, x);
	}
	bool empty(){
		return vec.empty();
	}
	int top(){
		return vec.front();
	}
	void pop(){
		int x = vec.back();
		int i = 0;
		vec.reduce();
		for(;;){
			int j = i * 2 + 1;
			if(j + 1 < vec.size() && fnc(vec[j + 1], x) && fnc(vec[j + 1], vec[j])) ++j;
			else if(j >= vec.size() || fnc(x, vec[j])) break;
			vec.substitute(i, vec[j]);
			i = j;
		}
		vec.substitute(i, x);
	}
	void chmin(int i, int x){
		while(i != 0){
			int j = (i - 1) / 2;
			if(fnc(vec[j], x)) break;
			vec.substitute(i, vec[j]);
			i = j;
		}
		vec.substitute(i, x);
	}
	void print(){
		vec.print();
	}
};

struct edge{
	int to;
	int cost;
};

std::vector<int> dijkstra(std::vector<std::vector<edge>> e){
	std::vector<int> ret(e.size(), 0);
	std::vector<int> ind(e.size(), -1);
	std::vector<bool> fin(e.size(), false);

	heap h(ind, [&ret](int l, int r){ return ret[l] < ret[r]; });

	h.push(0);

	while(!h.empty()){
		int cur = h.top();
		h.pop();
		fin[cur] = true;
		for(edge i : e[cur]){
			if(!fin[i.to]){
				if(ind[i.to] == -1){
					ret[i.to] = ret[cur] + i.cost;
					h.push(i.to);
				}else if(ret[i.to] > ret[cur] + i.cost){
					ret[i.to] = ret[cur] + i.cost;
					h.chmin(ind[i.to], i.to);
				}
			}
		}
	}

	return ret;
}

int main(){
	int n;
	std::cin >> n;

	std::vector<std::vector<edge>> e(n);

	for(int i = 0; i < n - 1; ){
		std::cin >> i;
		int k;
		std::cin >> k;
		e[i].resize(k);
		for(int j = 0; j < k; ++j){
			std::cin >> e[i][j].to >> e[i][j].cost;
		}
	}

	std::vector<int> ans = dijkstra(e);

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