Go: package main

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

type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }
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 = i
	pq[j].index = j
}
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
}

type Q struct {
	pq *PriorityQueue
	ds map[int]int // It has always same value as pq. For random access.
}

func newQ() *Q {
	pq := new(PriorityQueue)
	heap.Init(pq)
	return &Q{
		pq: pq,
		ds: make(map[int]int),
	}
}
func (q *Q) pop() (u, d int) {
	item := heap.Pop(q.pq).(*Item)
	delete(q.ds, item.value)
	return item.value, item.priority
}
func (q *Q) get(u int) (int, bool) { // returns d
	d, ok := q.ds[u]
	return d, ok
}
func (q *Q) set(u, d int) {
	q.ds[u] = d
	for _, item := range *q.pq {
		if item.value != u {
			continue
		}

		item.priority = d
		heap.Fix(q.pq, item.index)
		return
	}
	heap.Push(q.pq, &Item{
		priority: d,
		value:    u,
	})
}
func (q *Q) len() int {
	return len(q.ds)
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := newQ()
	q.set(0, 0)
	visited := make(map[int]struct{})
	out := make([]int, n)

	for q.len() > 0 {
		u, d := q.pop()
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			dd := d + c
			if d, ok := q.get(uu); !ok || d > dd {
				q.set(uu, dd)
			}
		}

		out[u] = d
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, d)
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define fi first
#define se second
#define mp make_pair
#define itrfor(itr,A) for(auto itr = A.begin(); itr !=A.end();itr++)
#define dame { printf("No\n"); return 0;}
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;
typedef long long llong;
char moji[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char moji2[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char moji3[10]={'0','1','2','3','4','5','6','7','8','9'};
#define Sort(a) sort(a.begin(),a.end());
#define Reverse(a) reverse(a.begin(),a.end());
#define print(a) cout << a << endl;
#define MOD llong(1e9+7)
#define MAX int(2 * 1e5 +5)
#define debug(x)  cout << #x << " = " << (x) << endl;
#define pi acos(-1.0)
#define int llong
#define INF llong(1e17)


template <typename T> class Dijkstra{
    vector<pair < T, int >>* edges;
    int* par;
    T* Distance;
    int size;

public:
    Dijkstra(int n){
        size = n;
        edges = new vector<pair<T , int> >[n];
        Distance = new T[n];
        par = new int[n];
    };

    T Get_Distance(int x){
        if(x < 0 or x>= size) return T(-1);
        else return Distance[x];
    };

    void add_edge(T dist, int s,int t);

    void Disp_Graph();

    void Get_Shortest(int s);



};


template<typename T> void Dijkstra<T>::add_edge(T dist, int s,int t){
    edges[s].push_back( mp(dist, t));
}

template<typename T> void Dijkstra<T>::Disp_Graph(){
    REP(i,size){
        REP(j,edges[i].size()){
            cout << edges[i][j].se << " " << edges[i][j].fi <<endl;
        }
        cout << endl;
    }
}


template<typename T> void Dijkstra<T>::Get_Shortest(int s){
    REP(i,size) Distance[i] = T(INF);
    reversed_priority_queue< pair<T, int > > Q;
    bool used[size];
    REP(i,size) used[i] = false;
    REP(i,size) par[i] = -1;
    int Q_size = 1;
    Distance[s] = 0;
    Q.push( mp(T(0),s ) );
    while(Q_size){
        pair < T, int > x = Q.top();
        Q.pop();
        Q_size --;
        if( used[x.se]) continue;
        used[x.se] = true;
        REP(i,edges[x.se].size()){
            pair < T, int > y = edges[x.se][i];
            if(  Distance[y.se] > Distance[x.se] + y.fi){
                Distance[y.se] = Distance[x.se] + y.fi;
                Q.push( mp ( Distance[y.se],y.se) );
                Q_size ++;
                par[y.se] = x.se;
            }
        }

    }

}











signed main(){
    int n,m,s;
    cin >> n;
    Dijkstra<int> D(n);
    int t1,d1;
    REP(i,n){
        int u,k;
        scanf("%lld%lld",&u,&k);
        REP(j,k){
            scanf("%lld%lld",&t1,&d1);
            D.add_edge(d1,u,t1);
        
        }
    }


    D.Get_Shortest(0);
    int tmp = 0;
    REP(i,n){
        printf("%lld %lld\n",i,D.Get_Distance(i));
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

static const int N = 10000;
static const int INF = 1000000;

struct Vertex {
    int id;
    bool in_S;
    int min_cost;
};

bool operator>(const Vertex &vertex1, const Vertex &vertex2)
{
    return vertex1.min_cost > vertex2.min_cost;
}


class SSSP {
private:
    vector<pair<int, int> > adj[N];
    Vertex vertex[N];
    int n;

public:
    SSSP()
    {
        cin >> n;
        int id;
        int degree;
        int v;
        int c;
        for (int i = 0; i < n; i++)
        {
            cin >> id;
            vertex[id].id = id;
            cin >> degree;
            for (int j = 0; j < degree; j++)
            {
                cin >> v >> c;
                adj[id].push_back(make_pair(v, c));
            }
        }
    }

    void dijkstra(int start)
    {
        priority_queue<Vertex, vector<Vertex>, greater<Vertex> > PQ;
        for (int i = 0; i < n; i++)
        {
            vertex[i].in_S = false;
            vertex[i].min_cost = INF;
        }
        vertex[start].min_cost = 0;

        PQ.push(vertex[start]);

        while (!PQ.empty())
        {
            int u = PQ.top().id;
            int cost = PQ.top().min_cost;
            PQ.pop();

            vertex[u].in_S = true;

            if (vertex[u].min_cost < cost)
                continue;

            for (int j = 0; j < adj[u].size(); j++)
            {
                int v = adj[u][j].first;
                if (!vertex[v].in_S)
                {
                    if (vertex[u].min_cost + adj[u][j].second < vertex[v].min_cost)
                    {
                        vertex[v].min_cost = vertex[u].min_cost + adj[u][j].second;
                        PQ.push(vertex[v]);
                    }
                }
            }
        }
    }

    void print()
    {
        for (int i = 0; i < n; i++)
            cout << i << " " << (vertex[i].min_cost == INF ? -1 : vertex[i].min_cost) << endl;
    }
};

int main()
{
    SSSP mst;
    mst.dijkstra(0);
    mst.print();
}