Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [][]int

func (g *Graph) dikstra(n int) string {
	var (
		u, minv = 0, 0
	)

	d := make([]int, n)
	color := make([]int, n)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		color[i] = WHITE
	}
	d[0] = 0
	color[0] = GRAY

	for {
		minv = INFTY
		u = -1
		for 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 v := 0; v < n; v++ {
			if color[v] != BLACK && (*g)[u][v] != INFTY {
				if d[v] > d[u]+(*g)[u][v] {
					d[v] = d[u] + (*g)[u][v]
					color[v] = GRAY
				}
			}
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		out.WriteString(fmt.Sprintf("%d ", i))
		if d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d\n", -1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d\n", d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make(Graph, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			g[i][j] = INFTY
		}
	}

	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())

		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())

			g[u][v] = c
		}
	}

	return g.dikstra(n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include<bits/stdc++.h>
#define INF 10000000
#define NIL -1
#define BLACK 2
#define WHITE 0
#define GRAY 1
#define MAX 100
using namespace std;

int color[MAX]={},d[MAX]={},pi[MAX]={},n;

void initializeSingleSource(int w[MAX][MAX]){
    for(int v=0;v<n;v++){
        d[v]=INF;
        pi[v]=NIL;
    }
    d[0]=0;
}

void relax(int u,int v,int w[MAX][MAX]){
    if(d[v]>d[u]+w[u][v]){
        d[v]=d[u]+w[u][v];
        pi[v]=u;
    }
}

void dijkstra(int w[MAX][MAX]){
    int mincost,i,v,u;
    initializeSingleSource(w);
    while(1){
        mincost=INF;
        u=NIL;
        for(i=0;i<n;i++){
            if(color[i]!=BLACK&&d[i]<mincost){
                mincost=d[i];
                u=i;
            }
        }
        if(u==NIL)break;
        color[u]=BLACK;
        for(v=0;v<n;v++){
            if(color[v]!=BLACK&&d[u]+w[u][v]<d[v]){
                pi[v]=u;
                d[v]=d[u]+w[u][v];
                color[v]=GRAY;
            }
        }
    }
}

void prim(int w[MAX][MAX]){
    int u,i,v,mincost,j;
    for(u=0;u<n;u++){
        d[u]=INF;
        pi[u]=NIL;
        color[u]=WHITE;
    }
    d[0]=0;
    while(1){
        mincost = INF;
        u=-1;
        for(i=0;i<n;i++){
            if(color[i]!=BLACK&&d[i]<mincost){
                mincost=d[i];
                u=i;
            }
        }
        if(u==-1)break;
        color[u]=BLACK;
        for(v=0;v<n;v++){
            if(color[v]!=BLACK&&w[u][v]!=INF){
                if(d[v]>w[u][v]){
                    pi[v]=u;
                    d[v]=w[u][v];
                    color[v]=GRAY;
                }
            }
        }
    }
    int sum=0;
    for(i=0;i<n;i++){
        if(pi[i]!=NIL)sum+=w[i][pi[i]];
    }

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

    cout<<endl;

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

int main(){
    int w[MAX][MAX],i,j,id,k,weight,gow,ans;
    cin>>n;
    for(i=0;i<MAX;i++){
        for(j=0;j<MAX;j++){
            w[i][j]=INF;
        }
    }
    for(i=0;i<n;i++){
        cin>>id>>k;
        for(j=0;j<k;j++){
            cin>>gow>>weight;
            w[id][gow]=weight;
        }
    }
    dijkstra(w);
    for(i=0;i<n;i++){
        cout<<i<<" "<<d[i]<<endl;
    }
}

C++: #include<iostream>
#include<iomanip>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<sstream>
using namespace std;
 
#define P(p) cout<<(p)<<endl
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(),greater<int>());
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return
#define lb(v,n) lower_bound(v.begin(),v.end(),n)
#define ub(v,n) upper_bound(v.begin(),v.end(),n)
#define mae(v) max_element(v.begin(),v.end())
#define mie(v) min_element(v.begin(),v.end())
#define mod 1000000007
typedef long long ll;
////////////////////////////////////////////////////////////

typedef struct edge_{
	int to,cost;
}edge;

class graph{ 
	typedef long cost_type;
	typedef int edge_type;
	typedef pair<cost_type,edge_type> P; 	//first:最短経路コスト second:頂点番号
	
	
	private:
		vector<vector<edge> > gra; 	//グラフ本体
		priority_queue<P,vector<P>,greater<P> > que; 	//最短経路の値の小さいものから取り出せる
		edge_type V; 	//頂点の個数
	  vector<cost_type> dp; 	//開始地点からの最小コストを格納する配列
	
	public:
		void init(int num){
			V = num;
			dp.assign(V,0x7FFFFFFF);
			gra.assign(V,vector<edge>());
		}


		void setCost(edge_type from , edge_type to , edge_type cost){
			edge e = {to , cost};
			edge e2 = {from , cost};
			gra[from].push_back( e );
			gra[to].push_back(e2);
		}	

		
		void setDirectCost(edge_type from , edge_type to , edge_type cost){
			edge e = {to , cost};
			gra[from].push_back( e );
		}	


		void dijkstra(edge_type s){
			dp[s] = 0;
			que.push(pair<cost_type,edge_type>(0,s)); 

			while(!que.empty()){
				P p = que.top();
				que.pop();
				edge_type v = p.second;
				if( dp[v] > p.first ) continue;
				for(edge_type i = 0 ; i < gra[v].size() ; i++ ){
					edge e = gra[v][i];
					if( dp[e.to] > dp[v] + e.cost ){
						dp[e.to] = dp[v] + e.cost;
						que.push(pair<cost_type,edge_type>(dp[e.to],e.to));
					}
				}
			}
		}

		cost_type getCost(edge_type to){
			return dp[to];
		} 

};

int main(){
	graph g;
	int n;
	cin >> n;
	g.init(n);
	rep(i,0,n){
		int from , k;
		cin >> from >> k;
		rep(i,0,k){
			int to;
			long cost;
			cin >> to >> cost;
			g.setDirectCost(from,to,cost);
		}

	}

	g.dijkstra(0);

	rep(i,0,n)
		cout << i << " " << g.getCost(i) << endl;

	ret 0;
}

C++: #include <cstdio>
#include <unordered_map>
#define N 100
#define INF 0x7FFFFFFF

void solve(std::unordered_map<int, int> a[N], const int n){
    std::unordered_map<int, int> result;
    result.insert(std::make_pair(0, 0));
    while(result.size() != n){
        std::pair<int, int> newV;
        newV.first = INF;
        newV.second = INF;
        for(auto &x: result){
            for(auto &y: a[x.first]){
                int w = y.second + x.second;
                if(result.find(y.first) == result.end() && newV.second > w){
                    newV.first = y.first;
                    newV.second = w;
                }
            }
        }
        result.insert(newV);
    }
    for(int i = 0; i < n; ++i){
        std::printf("%d %d\n", i, result.find(i)->second);
    }
}

int main(){
    int n;
    std::scanf("%d", &n);
    std::unordered_map<int, int> a[N];
    for(int i = 0; i < n; ++i){
        int id;
        std::scanf("%d", &id);
        int num;
        std::scanf("%d", &num);
        for(int j = 0; j < num; ++j){
            std::pair<int, int> v;
            std::scanf("%d", &v.first);
            std::scanf("%d", &v.second);
            a[i].insert(v);
        }
    }
    solve(a, n);
    return 0;
}