Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
const INF = 1 << 60

type Edge struct {
	to, cost int
}

type Node struct {
	id, cost int
}

type PQ []Node

func (pq PQ) Len() int           { return len(pq) }
func (pq PQ) Less(i, j int) bool { return pq[i].cost < pq[j].cost }
func (pq PQ) Swap(i, j int)      { pq[i], pq[j] = pq[j], pq[i] }

func (pq *PQ) Push(x interface{}) {
	*pq = append(*pq, x.(Node))
}

func (pq *PQ) Pop() interface{} {
	old := *pq
	n := len(old)
	x := old[n-1]
	*pq = old[0 : n-1]
	return x
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	E := make([][]Edge, N)
	for i := 0; i < N; i++ {
		id, k := sc.nextInt(), sc.nextInt()
		E[id] = make([]Edge, k)
		for j := 0; j < k; j++ {
			to, cost := sc.nextInt(), sc.nextInt()
			E[id][j] = Edge{to, cost}
		}
	}

	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	done := make([]bool, N)
	pq := &PQ{}
	heap.Init(pq)
	d[0] = 0
	heap.Push(pq, Node{0, 0})
	for pq.Len() > 0 {
		cur := heap.Pop(pq).(Node).id
		if done[cur] {
			continue
		}
		done[cur] = true
		for i := 0; i < len(E[cur]); i++ {
			to, cost := E[cur][i].to, E[cur][i].cost+d[cur]
			if !done[to] && cost < d[to] {
				d[to] = cost
				heap.Push(pq, Node{to, cost})
			}
		}
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i, d[i])
	}
	wtr.Flush()
}

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;
}