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<iostream>
using namespace std;
#include<queue>
#include<vector>
#include<string>
#include<string.h>
#include<algorithm>
bool b[2003];

#define REP(i, a) for(int i=0;i<a;i++)
	int N=(1<<20);
	int max=101;
		vector < pair<int,int> > A[100000];
int main()
{
	pair<int,int> f;
	priority_queue < pair < int,int > > PQ;
	int min,b,a,k,m,n ,e,p[101],d[100000],u,v; //u ?????????v????????¨
	bool color[10001];

	cin>>n;
	REP(i, n) {
		cin>>u>>k;
		REP(i, k) {
			cin>>a>>b;
			if(b==-1) b=N;
			A[u].push_back(make_pair(a,b));
		}
		color[i]=0;
		d[i]=N;
	}
	a=0;
	d[0]=0;
	PQ.push(make_pair(0,0));
	while(!PQ.empty()){
		f=PQ.top();
		PQ.pop();
		u=f.second;
		color[u]=1;
		REP(i,A[u].size()){
			v=A[u][i].first;
			if(color[v]==1){
			continue;
			}
				if(d[v]>A[u][i].second+d[u]){
					d[v]=A[u][i].second+d[u];
					PQ.push(make_pair(d[v]*-1,v));
				}
		}
	}
	REP(i,n){
		cout<<i<<" "<<(d[i] == N ? -1 : d[i])<<endl;
	
	}
	return 0;
}

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

#define INF (1 << 20)
#define MAX 10001
#define WHITE 0
#define BLACK 2
#define GRAY 1

using namespace std;

int n, dist[MAX], color[MAX];
vector<pair<int, int>> arrows[MAX];

void dijkstra(void);

int main() {
	int i, j, u, k, v, c;

	cin >> n;
	

	for (i = 0; i < n; i++) {
		cin >> u >> k;
		for (j = 0; j < k; j++) {
			cin >> v >> c;
			arrows[u].push_back(make_pair(v, c));
		}
	}

	dijkstra();

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


	return 0;
}

void dijkstra(void) {
	int i;
	pair<int, int> u;
	priority_queue<pair<int, int>> grays;
	vector<pair<int, int>>::iterator itv;
	
	dist[0] = 0;
	for (i = 1; i < n; i++) {
		dist[i] = INF;
		color[i] = WHITE;
	}

	color[0] = GRAY;
	grays.push(make_pair(0, 0));

	while (!grays.empty()) {
		u =  grays.top();
		u.first *= -1;
		grays.pop();
		color[u.second] = BLACK;

		for (itv = arrows[u.second].begin(); itv != arrows[u.second].end(); itv++) {
			if (color[itv->first] == BLACK)continue;
			if (dist[u.second] + itv->second < dist[itv->first]) {
				dist[itv->first] = dist[u.second] + itv->second;
				color[itv->first] = GRAY;
				grays.push(make_pair(((-1) * dist[itv->first]), itv->first));
			}
		}
	}

}

C++: #include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp       make_pair
#define mt       make_tuple
#define pb       push_back
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

const int MAX_N = 101000;//MAX???????????°
struct edge{
	int tar;//????????°
	int ecost;//???????????????
	edge(){}
	edge(int _tar, int _ecost){
		tar = _tar;
		ecost = _ecost;
	}
};
struct State{
	int pos;//?????¨??°
	int cost;//??????????????????pos?????§????????????
	State(){}
	State(int _pos, int _cost){
		pos = _pos;
		cost = _cost;
	}
	//cost?????§????????????????°?????????¨??????priority_queue???default???????????????
	bool operator < (const State &st) const {
		return cost > st.cost;
	}
};
vector<edge> edges[MAX_N];//??£??\?????????
int dist[MAX_N];//?§????s????????????dist[v]?????§??????????????????
void dijkstra(int s){
	priority_queue<State> que;
	fill(dist, dist + MAX_N, INF);
	dist[s] = 0;
	que.push(State(s, 0));
	while (!que.empty()){
		State p = que.top();
		que.pop();
		if (dist[p.pos] < p.cost)continue;
		for (int i = 0; i < edges[p.pos].size(); i++){
			edge e = edges[p.pos][i];
			if (dist[e.tar] > dist[p.pos] + e.ecost){
				dist[e.tar] = dist[p.pos] + e.ecost;
				que.push(State(e.tar, dist[e.tar]));
			}
		}
	}
	return ;
}
int N;
int main(){
	cin >> 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;
			edges[u].push_back(edge(v, c));
			// edges[v].push_back(edge(u, c));
		}
	}
	dijkstra(0);
	for (int i = 0; i < N; i++){
		cout << i << " " << dist[i] << endl;
	}
	return 0;
}