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++: #define _USE_MATH_DEFINES
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define str string
#define ld long double
#define vec vector
#define vll vec<ll>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
#define ALL(a) a.begin(),a.end()
#define rALL(a) a.rbegin(),a.rend()
#define kai "\n"
#define yuque priority_queue
#define ryuque(t) priority_queue<t,vec<t>,greater<t>>
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mie min_element
#define mae max_element
#define tos to_string
#define sep setprecision
#define MOD 1000000007ll
using namespace std;
void solve();
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed;
	solve();
}

//nCk(mod p)
const ll NUM=2;
ll fac[NUM],finv[NUM],inv[NUM];
void cmodf(ll p=MOD){
	fac[0]=fac[1]=1;
	finv[0]=finv[1]=1;
	inv[1]=1;
	rep(i,2,NUM){
		fac[i]=fac[i-1]*i%p;
		inv[i]=p-inv[p%i]*(p/i)%p;
		finv[i]=finv[i-1]*inv[i]%p;
	}
}
ll cmod(ll n,ll k,ll p=MOD){
	return fac[n]*(finv[k]*finv[n-k]%p)%p;
}

//最大公約数
ll gcd(ll n,ll m){
	ll k=n%m;
	while(k!=0){n=m;m=k;k=n%m;}
	return m;
}
ll gcd(vector<ll> a){
	rep(i,1,a.size()){
		ll k=a[i-1]%a[i];
		while(k!=0){
			a[i-1]=a[i];
			a[i]=k;
			k=a[i-1]%a[i];
		}
	}
	return a.back();
}

//最小公倍数
ll lcm(ll n,ll m){
	ll t=n*m,k=n%m;
	while(k!=0){n=m;m=k;k=n%m;}
	return t/m;
}
ll lcm(vector<ll> a){
	ll n;
	rep(i,1,a.size()){
		n=a[i-1]*a[i];
		ll k=a[i-1]%a[i];
		while(k!=0){
			a[i-1]=a[i];
			a[i]=k;
			k=a[i-1]%a[i];
		}
		n/=a[i];
		a[i]=n;
	}
	return n;
}

//keyの小さい方から探していった時に初めてvalueがjoken以上になる時のkey（keyが増えたらvalueも増えるか同じになるとき）
ll value(ll key) {
	/*変更*/
	return 0;
}
ll mink(ll lkey,ll rkey/*ここも調べる*/,ll joken){
	ll mkey;
	while(lkey!=rkey){
		mkey=(lkey+rkey)/2;
		if(value(mkey)>=joken)rkey=mkey;
		else lkey=mkey+1;
	}
	return rkey;
}

//ペア配列の整理
struct pea{
	ll f,s;
	bool operator<(const pea &c)const{
		return f!=c.f?f<c.f:s<c.s;
	};
};

//変数定義

//再帰関数

//アルゴリズム
void solve(){
	ll n;cin>>n;
	vec<vll> we(n,vll(n,MOD));
	vec<set<ll>> ch(n);
	vec<vll> pa(n);
	rep(i,0,n){
		ll u,k;cin>>u>>k;
		rep(j,0,k){
			ll v,c;cin>>v>>c;
			ch[u].insert(v);
			pa[v].pub(u);
			we[u][v]=c;
		}
	}
	vll a,ans(n);
	ans[0]=0;
	a.pub(0);
	rep(i,0,pa[0].size())ch[pa[0][i]].erase(0);
	while(a.size()!=n){
		ll m=MOD;ll jj;
		rep(i,0,a.size()){
			for(auto j=ch[a[i]].begin();j!=ch[a[i]].end();j++){
				if(ans[a[i]]+we[a[i]][*j]<m){
					m=ans[a[i]]+we[a[i]][*j];
					jj=*j;
				}
			}
		}
		ans[jj]=m;
		a.pub(jj);
		rep(i,0,pa[jj].size())ch[pa[jj][i]].erase(jj);
	}
	rep(i,0,n){
		cout<<i<<' '<<ans[i]<<kai;
	}
}

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 = 100001;
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<iostream>
#include<queue>
#define INF 100000000
using namespace std;
int main(){
  int n;cin>>n;
  int u,k,v,c;
  int mp[101][101],d[101];
  for(int i=0;i<=100;i++){
    d[i]=INF;
    for(int j=0;j<=100;j++)mp[i][j]=INF;
  }
    while(u!=n-1){
    cin>>u>>k;
    for(int i=0;i<k;i++){
      cin>>v>>c;
      mp[u][v]=c;
    }
  }
     
    int mn,s,t[101]={},cn=n-1;
    d[0]=0,t[0]=1;
    while(cn){
      mn=INF;
      for(int i=0;i<n;i++){
    if(t[i]==0){
      if(d[i]>d[s]+mp[s][i])d[i]=d[s]+mp[s][i];
      if(mn>d[i])mn=d[i],k=i;
    }
      }
      if(mn!=INF)cn--,t[k]=1;
    }
    for(int i=0;i<n;i++)cout<<i<<" "<<d[i]<<endl;
     
  return 0;
}