Go: package main

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

type Node struct {
	Id int
	Dis int
}

type PriorityQueue []*Node

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

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].Dis < pq[j].Dis
}

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

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

func (pq *PriorityQueue) Pop() interface{} {
	n := len(*pq)
	pop := (*pq)[n-1]
	*pq = (*pq)[0:n-1]
	return pop
}

func main() {
	var n int
	fmt.Scan(&n)

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

	g := make([][]int, n)
	c := make([][]int, n)
	pq := make(PriorityQueue, 0, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		g[u] = make([]int, k)
		c[u] = make([]int, k)
		for j := 0; j < k; j++ {
			sc.Scan()
			g[u][j], _ = strconv.Atoi(sc.Text())
			sc.Scan()
			c[u][j], _ = strconv.Atoi(sc.Text())
		}

		if u == 0 {
			d[u] = 0
		} else {
			d[u] = 1 << 30
		}
		pq.Push(&Node{u, d[u]})
	}
	heap.Init(&pq)

	for pq.Len() > 0 {
		node := heap.Pop(&pq).(*Node)
		for i := 0; i < len(g[node.Id]); i++ {
			nextId := g[node.Id][i]
			nextDis := node.Dis + c[node.Id][i]
			if nextDis < d[nextId] {
				d[nextId] = nextDis
				heap.Push(&pq, &Node{nextId, nextDis})
			}
		}
	}

	wr := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		wr.WriteString(strconv.Itoa(i))
		wr.WriteString(" ")
		wr.WriteString(strconv.Itoa(d[i]))
		wr.WriteString("\n")
	}
	wr.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;
}