Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

//make priority queue
type Item struct {
	id       int
	priority int // this state is priority and distans from start
	index    int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

// we want the ascending-priority-queue.
// So we use less than here.
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = j
	pq[j].index = i
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(dist int, id int) {
	for _, i := range *pq {
		if i.id == id {
			if i.priority > dist {
				i.priority = dist
			}
		}
	}
}

// the main code
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	return n
}

func dikstra(dist [][]int, n int) []int {
	// preparate priority queue
	inf := 1 << 30
	pq := make(PriorityQueue, n)
	shortests := make([]int, n)
	for i := 0; i < n; i++ {
		pq[i] = &Item{
			id:       i,
			priority: inf,
			index:    i,
		}

		shortests[i] = inf
	}
	pq[0].priority = 0
	shortests[0] = 0
	heap.Init(&pq)

	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		shortests[item.id] = item.priority
		for id, d := range dist[item.id] {
			if d != -1 {
				pq.update(item.priority+d, id)
			}
		}
		heap.Init(&pq)
	}
	return shortests
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// init
	n := nextInt(sc)
	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dist[i][j] = -1
		}
	}

	// std Input
	for i := 0; i < n; i++ {
		u := nextInt(sc)
		k := nextInt(sc)
		for j := 0; j < k; j++ {
			v, c := nextInt(sc), nextInt(sc)
			dist[u][v] = c
		}
	}

	shortests := dikstra(dist, n)
	for i, d := range shortests {
		fmt.Printf("%d %d\n", i, d)
	}
}

C++: #include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
#ifdef DEBUG
#define line() cerr << "[" << __LINE__ << "] ";
#define dump(i) cerr << #i ": " << i << " ";
#define dumpl(i) cerr << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;
typedef map<int, vector<int>> Graph;
typedef map<int, map<int, int>> WGraph;
typedef pair<int, int> Edge;

map<Edge, bool> mp;
map<int, int> back;
vector<int> dijkstra(int v, WGraph &g, int r)
{
    vector<int> cost(v, 1 << 30);
    cost[r] = 0;
    priority_queue<pair<int, int>> pq;

    pq.push({0, r});

    while (!pq.empty())
    {
        //cerr << "-------------------------------" << endl;
        auto t = pq.top();
        pq.pop();
        //cerr << t.second << "," << t.first << endl;
        if (cost[t.second] < -t.first)
        {
            continue;
        }
        for (auto x : g[t.second])
        {
            if (cost[x.first] > cost[t.second] + g[t.second][x.first])
            {
                cost[x.first] = cost[t.second] + g[t.second][x.first];
                //cerr << "update cost[" << x.first << "] with " << cost[x.first] << endl;
                back[x.first] = t.second;
                pq.push({-cost[x.first], x.first});
            }
        }
    }
    return cost;
}

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    WGraph g;
    rep(i, n)
    {
        int k;
        cin >> k >> k;
        rep(j, k)
        {
            int v, c;
            cin >> v >> c;
            g[i][v] = c;
        }
    }
    auto cost = dijkstra(n, g, 0);
    rep(i, n)
    {
        cout << i << " " << cost[i] << endl;
    }
    return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
	short id;
	long cost;
	node(short i, long c){
		this->id = i;
		this->cost = c;
	}
	bool operator<(node c) const {
		return this->cost > c.cost;
	}
};

int main() {
	int n;
	scanf("%d", &n);
	
	int c[n];
	int w[n][n];
	for(int i = 0; i < n; i++){
		int u, k;
		scanf("%d %d", &u, &k);
		
		c[u] = u ? LONG_MAX : 0;
		for(int j = 0; j < n; j++){
			w[u][j] = -1;
		}
		while(k--){
			int v, c;
			scanf("%d %d", &v, &c);
			w[u][v] = w[v][u] = c;
		}
	}
	
	priority_queue<node> q;
	q.push(*new node(0, 0));
	while(q.size()){
		int i = q.top().id;
		int ca = q.top().cost;
		q.pop();
		
		for(int j = 0; j < n; j++){
			if(w[i][j] != -1 && c[j] > w[i][j] + ca){
				c[j] = w[i][j] + ca;
				q.push(*new node(j, c[j]));
			}
		}
	}
	
	for(int i = 0; i < n; i++){
		printf("%d %d\n", i, c[i]);
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <stack>
#include <climits>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll mod = 1000000007;
const int inf = 1e9;
const long long INF = 1LL << 60;

const int MAX_N = 105;
int g[MAX_N][MAX_N];
int d[MAX_N];
bool used[MAX_N];
int n;

void dijkstra(int s){
    fill(d,d+n,inf);
    fill(used,used+n,false);
    d[s] = 0;

    while(true){
        int v = -1;
        for(int u = 0; u < n; u++){
            if(!used[u] && (v == -1 || d[u] < d[v])){
                // 1番最小のd[u]となるuを、v = u　として保存
                // ループを抜けた後に、vを介して[u]を更新
                v = u;
            }
        }
        //　この時点でd[u]が最小となるuは決定されている。
        if(v == -1) break;
        used[v] = true;
        for(int u = 0; u < n; u++){
            d[u] = min(d[u],d[v] + g[v][u]);
        }
    }
}

int main()
{
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            g[i][j] = inf;
        }
    }
    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;
            g[u][v] = c;
        }
    }
    dijkstra(0);
    for(int i = 0; i < n; i++){
        cout << i << ' ' << d[i] << endl;
    }
}