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 <cstdio>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

using Weight=int;
using DArray=vector<Weight>;
using DMatrix=vector<DArray>;

static const Weight INF=1<<29;

struct Edge {
    size_t src, dst;
    Weight weight;
    Edge() {}
    Edge(size_t src, size_t dst, Weight weight=1):
        src(src), dst(dst), weight(weight)
    {}
};

bool operator<(const Edge &e, const Edge &f) {
    if (e.weight != f.weight) {
        return e.weight > f.weight;
    } else {
        return e.src!=f.src? e.src<f.src : e.dst<f.dst;
    }
}

using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w=1) {
    // non-directed
    g[s].push_back(Edge(s, d, w));
    g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w=1) {
    // directed
    g[s].push_back(Edge(s, d, w));
}

template <class T>
using lp_queue=priority_queue<T, deque<T>, greater<T>>;

template <typename T, typename U=size_t>
union raw_cast {
    public:
        constexpr raw_cast(T t): _t(t) {}
        operator U() const {
            return _u;
        }

    private:
        T _t;
        U _u;
};

size_t bsr(size_t n) {
    /* Bit scan reverse */
    // logarithmus binarius
    if (!n) return -1;

    size_t res;
    for (res=0; n>>=1; ++res)
        if (!n) return res;

    return res;
}

template <class Index, class T, size_t bucketSize=sizeof(Index)<<3>
struct RadixHeap {
    vector<pair<Index, T>> v[bucketSize|1];
    Index last;
    size_t size_;
    RadixHeap(): last(0), size_(0) {}
    void push(Index n, T t) {
        if (last > n) return;

        ++size_;
        size_t idx=raw_cast<Index>(n);
        idx ^= raw_cast<Index>(last);
        v[bsr(idx)+1].push_back(make_pair(n, t));
    }
    void push(pair<Index, T> p) {
        push(p.first, p.second);
    }
    void pop() {
        if (!size_) return;
        if (!v[0].size()) {
            size_t i; for (i=1; !v[i].size(); ++i);
            last = min_element(v[i].begin(), v[i].end())->first;
            for (const pair<Index, T> &p: v[i]) {
                size_t idx=raw_cast<Index>(p.first);
                idx ^= raw_cast<Index>(last);
                v[bsr(idx)+1].push_back(p);
            }

            v[i].clear();
        }
        --size_;
        v[0].pop_back();
    }
    pair<Index, T> top() {
        if (!size_) return pair<Index, T>();
        if (!v[0].size()) {
            size_t i; for (i=1; !v[i].size(); ++i);
            last = min_element(v[i].begin(), v[i].end())->first;
            for (const pair<Index, T> &p: v[i]) {
                size_t idx=raw_cast<Index>(p.first);
                idx ^= raw_cast<Index>(last);
                v[bsr(idx)+1].push_back(p);
            }

            v[i].clear();
        }
        return v[0].back();
    }
    size_t size() {
        return size_;
    }
    bool empty() {
        return !size_;
    }
    void clear() {
        last = size_ = 0;
        for (size_t i=0; i<=bucketSize; ++i)
            v[i].clear();
    }
};

vector<Weight> shortest_distance(const Graph &g, size_t s) {
    /* Dijkstra */
    size_t V=g.size();
    vector<Weight> d(V, INF); d[s]=0;

    RadixHeap<Weight, size_t> q; q.push(0, s);
    while (!q.empty()) {
        pair<Weight, size_t> p=q.top(); q.pop();
        size_t v=p.second;
        if (d[v] < p.first) continue;

        for (const Edge &e: g[v])
            if (d[e.dst] > d[v] + e.weight) {
                d[e.dst] = d[v] + e.weight;
                q.push(d[e.dst], e.dst);
            }
    }
    return d;
}

int main() {
    size_t n;
    scanf("%zu", &n);

    Graph g(n);
    for (size_t i=0; i<n; ++i) {
        size_t u, k;
        scanf("%zu %zu", &u, &k);

        for (size_t j=0; j<k; ++j) {
            size_t c;
            Weight v;
            scanf("%zu %d", &c, &v);

            connect(g, u, c, v);
        }
    }

    vector<Weight> d=shortest_distance(g, 0);
    for (size_t i=0; i<n; ++i)
        printf("%zu %d\n", i, d[i]);

    return 0;
}

C++: #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAX=105;
const int INF= 1<<30;
int M[MAX][MAX],n,d[MAX],p[MAX],vis[MAX];
void init(){
	for(int i=0;i<n;i++){
		d[i]=INF;
		p[i]=-1;
		vis[i]=0;
	}
	d[0]=0;
}
void dijkstra(){
	init();
	while (true){
		int u=-1;
		int minCost=INF;
		for(int i=0;i<n;i++){
			if(!vis[i] && d[i]<minCost){
				u=i;
				minCost=d[i];
			}
		}
		if(u==-1)break;
		vis[u]=1;
		for(int i=0;i<n;i++){
			if(!vis[i] && M[u][i]!=-1 &&M[u][i]+d[u]<d[i]){
				d[i]=M[u][i]+d[u];
				p[i]=u;
			}
		}
	}
}
int main(){
	cin>>n;
	memset(M,-1,sizeof(M));
	for(int i=0;i<n;i++){
		int u,k,v,c;
		cin>>u>>k;
		for(int j=0;j<k;j++){
			cin>>v>>c;
			M[u][v]=c;
		}
	}
	dijkstra();
	for(int i=0;i<n;i++){
		printf("%d %d\n",i,d[i]);
	}
	return 0;
}