Go: package main

import (
	"strconv"
	"fmt"
	"bufio"
	"os"
)
const INF = 1 << 21
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}


func main() {
	scanner.Split(bufio.ScanWords)
	n := nextInt()
	adj := make([][]int, n)
	for i := range adj {
		adj[i] = make([]int, n)
	}

	for i := 0;i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			v := nextInt()
			c := nextInt()
			adj[u][v] = c
		}
	}

	// for i := range adj {
	// 	fmt.Println(adj[i])
	// }

	dist := make([]int, n)
	prev := make([]int, n)
	visited := make([]bool, n)
	dijkstra := func(start int, size int) {
		for i := 0; i < size; i++ {
			dist[i] = INF
			prev[i] = -1
			visited[i] = false
		}
		dist[start] = 0
		for {
			u := -1
			tmp_min := INF
			for i := 0; i < size; i++ {
				if visited[i] == false && dist[i] < tmp_min {
					tmp_min = dist[i]
					u = i
				}
			}
			if u == -1 {
				break
			}
			visited[u] = true
			for i := 0; i < size; i++ {
				if adj[u][i] > 0 {
					w := adj[u][i]
					newlen := dist[u] + w
					if newlen < dist[i] {
						dist[i] = newlen
						prev[i] = u
					}
				}
			}
		}
	}
	dijkstra(0, n)
	for i, cost := range dist {
		fmt.Printf("%d %d\n", i, cost)
	}
}

C++: #include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

void dijkstra()
{
    int minv; // 最小のコスト
    int d[MAX], color[MAX]; // d[n]は始点からnまでの最短コスト，color[v]はvの訪問状態

    for (int i = 0; i < n; i++) {
        // 最短コストと訪問状態の初期化
        d[i] = INFTY;
        color[i] = WHITE;
    }

    d[0] = 0; // 始点から始点までの最短コストは0
    color[0] = GRAY; // 始点を訪問済みにする
    while (1) {
        minv = INFTY; // 最小コストをINFで初期化
        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) { // vに訪問済みでなくu->vへのコストがINFでないとき
                if (d[v] > d[u] + M[u][v]) { // 「vへの最小コスト」より「現在までのコストと現在地からvまでのコストを加えたもの」のほうが小さい時
                    d[v] = d[u] + M[u][v]; // vまでのコストを更新
                    color[v] = GRAY; // vへ訪問済みとする
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout << i << " " << (d[i] == INFTY ? -1 : 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;
        }
    }

    int k, c, u, v;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            cin >> v >> c;
            M[u][v] = c;
        }
    }

    dijkstra();

    return 0;
}

C++: #include <iostream>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
#define INF 2000000000
int Min(int x,int y){
        return x>y?y:x;
}
int main(){
        int N;
        cin >> N;
        vector <vector <int> > adjancent(N,vector <int>(N,INF));
        for(int i=0 ; i<N ; i++ ){
                int n,degree,to,cost;
                cin >> n >> degree;
                for(int l=0 ; l<degree ; l++ ){
                        cin >> to >> cost;
                        adjancent[n][to]=cost;
                }
        }
        vector<int> dp(N,INF);
        dp[0]=0;
        bool sign=true;
        while(sign){
                sign=false;
                for(int now=0 ; now<N ; now++){
                        for(int i=0 ; i<N ; i++ ){
                                int tmp=dp[i];
                                dp[i]=Min(dp[i],dp[now]+adjancent[now][i]);
                                if(tmp!=dp[i])
                                        sign=true;
                        }
                }
        }
        for(int i=0 ; i<N ; i++ )
                cout <<i<<" "<<dp[i]<<endl;
        return 0;
}

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

const long MAX = 100;
const long INFTY = (1<<21);
const long WHITE = 0;
const long GRAY = 1;
const long BLACK = 2;

long n, M[MAX][MAX];

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

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

	d[0] = 0;
	color[0] = GRAY;
	while (1){
		minv = INFTY;
		long u = -1;
		for (long 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 (long 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 (long i = 0; i < n; i++){
		cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
	}
}

int main(){
	cin >> n;
	for (long i = 0; i < n; i++){
		for (long j = 0; j < n; j++){
			M[i][j] = INFTY;
		}
	}

	long k, c, u, v;
	for (long i = 0; i < n; i++){
		cin >> u >> k;
		for (long j = 0; j < k; j++){
			cin >> v >> c;
			M[u][v] = c;
		}
	}

	dijkstra();

	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <list>
using namespace std;

#define NODE_NUM 100

class Node
{
private:
	int next_node_list[NODE_NUM];
	int next_node_num;
	int next_weight_list[NODE_NUM];
public:
	Node();
	void AddNextNode(int id, int weight);
	void SetWeight(int weight);
	void DistributeWeight();
	int weight_from_0;
	bool check;
};

Node::Node()
{
	next_node_num = 0;
	weight_from_0 = -1;
	check = false;
}

Node *Node_list;
list<int> Distributed_node_list;

void Node::AddNextNode(int id, int weight)
{
	next_node_list[next_node_num] = id;
	next_weight_list[next_node_num] = weight;
	next_node_num++;
}

void Node::SetWeight(int weight)
{
	if (weight_from_0 == -1 || weight < weight_from_0) {
		weight_from_0 = weight;
	}
}

void Node::DistributeWeight()
{
	check = true;
	for (int i = 0; i < next_node_num; i++) {
		if (Node_list[next_node_list[i]].check == false) {
			Distributed_node_list.push_back(next_node_list[i]);
			Node_list[next_node_list[i]].SetWeight(weight_from_0 + next_weight_list[i]);
		}
	}
}

int main(void)
{
	//input
	int n;
	cin >> n;
	Node_list = new Node[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;
			Node_list[i].AddNextNode(v, c);
		}
	}

	//weight from 0 ?
	Node_list[0].SetWeight(0);
	Node_list[0].DistributeWeight();
	while (!Distributed_node_list.empty()) {
		int min_weight = Node_list[Distributed_node_list.front()].weight_from_0;
		list<int>::iterator min_idx = Distributed_node_list.begin();
		list<int>::iterator itr;
		for(itr = Distributed_node_list.begin(); itr != Distributed_node_list.end(); itr++){
			if (Node_list[*itr].weight_from_0 < min_weight) {
				min_weight = Node_list[*itr].weight_from_0;
				min_idx = itr;
			}
		}
		Node_list[*min_idx].DistributeWeight();
		Distributed_node_list.erase(min_idx);
	}
	
	//output
	for (int i = 0; i < n; i++) {
		cout << i << " " << Node_list[i].weight_from_0 << endl;
	}
	
	return 0;
}