Go: package main

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

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	route := make([][]Node, n)
	weight := make([]int, n)
	for i := 0; i < n; i++ {
		weight[i] = NIL
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		route[i] = make([]Node, k)
		for j := 0; j < k; j++ {
			route[u][j] = Node{id: scanInt(), weight: scanInt()}
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for _, nextNode := range route[node.id] {
			if weight[nextNode.id] != NIL {
				continue
			}
			nextNode.weight += node.weight
			heap.Push(h, nextNode)
		}
	}

	for i, w := range weight {
		fmt.Println(i, w)
	}
}

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