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