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 <stdio.h>
#include <iostream>
#include <queue>
#include <list>
using namespace std;

struct MiniNode
{
	int id;
	int weight_from_0;
};

class Node
{
private:
	list<int> next_node_list;
	list<int> next_weight_list;
public:
	Node();
	void AddNextNode(int id, int weight);
	void SetWeight(int weight);
	void DistributeWeight();
	int weight_from_0;
	bool check;
};

Node::Node()
{
	weight_from_0 = -1;
	check = false;
}

Node *Node_list;

struct GreatNode
{
	bool operator()(const MiniNode n1, const MiniNode n2) const;
};
bool GreatNode::operator()(const MiniNode n1, const MiniNode n2) const
{
	return n1.weight_from_0 > n2.weight_from_0;
}
priority_queue<MiniNode, vector<MiniNode>, GreatNode > Q;

void Node::AddNextNode(int id, int weight)
{
	next_node_list.push_back(id);
	next_weight_list.push_back(weight);
}

void Node::SetWeight(int weight)
{
	if (weight_from_0 == -1 || weight < weight_from_0) {
		weight_from_0 = weight;
	}
}

void Node::DistributeWeight()
{
	check = true;
	list<int>::iterator itr2 = next_weight_list.begin();
	for(list<int>::iterator itr = next_node_list.begin(); itr != next_node_list.end(); itr++){	
		if (Node_list[*itr].check == false) {
			Node_list[*itr].SetWeight(weight_from_0 + *itr2);
			MiniNode mini_node;
			mini_node.id = *itr;
			mini_node.weight_from_0 = Node_list[*itr].weight_from_0;
			Q.push(mini_node);
		}
		itr2++;
	}
	next_node_list.clear();
	next_weight_list.clear();
}

int main(void)
{
	//input
	int n;
	cin >> n;
	Node_list = new Node[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;
			Node_list[u].AddNextNode(v, c);
		}
	}

	//weight from 0 ?
	Node_list[0].weight_from_0 = 0;
	Node_list[0].DistributeWeight();
	while (!Q.empty()) {
		MiniNode mini_node = Q.top();
		Q.pop();
		Node_list[mini_node.id].DistributeWeight();
	}

	//output
	for (int i = 0; i < n; i++) {
		cout << i << " " << Node_list[i].weight_from_0 << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

vector<int> adja[10000], cost[10000];

#define INF (1 << 30)

int main() {
	int n, id, k;

	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> id >> k;
		adja[id].resize(k);
		cost[id].resize(k);
		for (int j = 0; j < k; ++j) {
			cin >> adja[id][j] >> cost[id][j];
		}
	}

	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;
	que.push(make_pair(0, 0)); //cost, node
	int best[10000];
	fill(best, best + n, INF);
	best[0] = 0;

	while (!que.empty()) {
		pair<int, int> top = que.top();
		que.pop();

		if (best[top.second] < top.first) {
			continue;
		}

		for (int j = 0, len = adja[top.second].size(); j < len; ++j) {
			if (best[adja[top.second][j]] > top.first + cost[top.second][j]) {
				best[adja[top.second][j]] = top.first + cost[top.second][j];
				que.push(make_pair(top.first + cost[top.second][j], adja[top.second][j]));
			}
		}
	}

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

	return 0;
}