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>
using namespace std;

class Vertice;
class Edge;

class Vertice{
private:
	int id;
	int edge_num;
	int *vertice_id_list;
	Edge **edges;
	bool flag_fixed;
	int distance_min;
public:
	Vertice(int id, int edge_num_max);
	~Vertice(){ delete[] edges, vertice_id_list; }
	int getDistanceMin() const { return distance_min; }
	void addEdge(int vertice_id, Edge *edge);
	void addVertices(Vertice *vertices[]);
	void setDistance(int d);
	void serveDistance();
	void setFlagFixed(){ flag_fixed = true; }
};

class Edge{
private:
	int distance;
	Vertice *vertice;
public:
	Edge(int distance){ this->distance = distance; }
	void addVertice(Vertice *vertice){ this->vertice = vertice; }
	void setDistance(int distance){ vertice->setDistance(distance + this->distance); }
};

struct verticeComp {
	bool operator()(const Vertice *v1, const Vertice *v2)
	{
		return v1->getDistanceMin() > v2->getDistanceMin();
	}
};

priority_queue<Vertice*, vector<Vertice*>, verticeComp> PQ; 

Vertice::Vertice(int id, int edge_num_max)
{
	this->id = id;
	edge_num = 0;
	vertice_id_list = new int[edge_num_max];
	edges = new Edge*[edge_num_max];
	flag_fixed = false;
	distance_min = 100000000;
}

void Vertice::addEdge(int vertice_id, Edge *edge)
{
	edges[edge_num] = edge;
	vertice_id_list[edge_num] = vertice_id;
	edge_num++;
}

void Vertice::addVertices(Vertice *vertices[])
{
	for (int i = 0; i < edge_num; i++) {
		edges[i]->addVertice(vertices[vertice_id_list[i]]);
	}
}

void Vertice::setDistance(int d)
{
	if (flag_fixed) return;
	if (d < distance_min) distance_min = d;
	PQ.push(this);
}

void Vertice::serveDistance()
{
	for (int i = 0; i < edge_num; i++) {
		edges[i]->setDistance(distance_min);
	}
	if (PQ.empty()) return;
	Vertice *fixed_vertice = PQ.top();
	PQ.pop();
	fixed_vertice->setFlagFixed();
	fixed_vertice->serveDistance();
}

int main(void)
{
	Vertice **vertices;

	int n;
	cin >> n;
	vertices = new Vertice*[n];
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		vertices[u] = new Vertice(u, k);
		for (int j = 0; j < k; j++) {
			int v, c;
			cin >> v >> c;
			vertices[u]->addEdge(v, new Edge(c));
		}
	}

	for (int i = 0; i < n; i++) {
		vertices[i]->addVertices(vertices);
	}

	vertices[0]->setDistance(0);
	vertices[0]->setFlagFixed();
	PQ.pop();
	vertices[0]->serveDistance();

	for (int i = 0; i < n; i++) {
		cout << i << " ";
		cout << vertices[i]->getDistanceMin() << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define N 100000
const int INF = 1 << 30; // > 10^9
int n;
using namespace std;
struct edge : vector<pair<int, int> > {
    void add_edge(int to, int cost) {
        push_back(pair<int, int>(to, cost));
    }
} info[N];
void bfs();
int hoge[N];
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        while (k--) {
            int v, c;
            cin >> v >> c;
            info[u].add_edge(v, c);
        }
    }
    bfs();
    for (int i = 0; i < n; i++) {
        cout << i << " " << hoge[i] << endl;
    }
}
void bfs() {
    fill_n(hoge, n, INF); //hoge ~ hoge + v <- INF
    queue<int> que;
    que.push(0);
    hoge[0] = 0;
    while (!que.empty()) {
        int p = que.front();
        que.pop();
        for (int i = 0; i < info[p].size(); i++) {
            if (info[p][i].second + hoge[p] < hoge[info[p][i].first]) {
                que.push(info[p][i].first);
                hoge[info[p][i].first] = info[p][i].second + hoge[p];
            }
        }
    }
    
}

C++: #define RangeFor(i,n) for(int i=0; i<n; ++i)
#define RvRngFor(i,n) for(int i=n-1; i>=0; --i)

#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <numeric>

using std::vector;
using std::cin;
using std::priority_queue;
using std::pair;
using std::make_pair;


vector<int> sssp(vector<vector<pair<int, int> > > c, int start_node = 0){
    int n = c.size();
    priority_queue<pair<int, int>, vector<pair<int,int> >, std::greater<pair<int, int>> > pq;
    vector<int> temp_dist(n, std::numeric_limits<int>::max());
    pq.push(make_pair(0, start_node));

    for(;!pq.empty();pq.pop()){
        auto t = pq.top();
        auto d = t.first;
        auto u = t.second;


        if(d < temp_dist[u]){
            temp_dist[u] = d;
            auto k = c[u].size();
            RangeFor(j, k) {
                pq.push(make_pair(d + c[u][j].second, c[u][j].first));
            }
        }
    }

    return temp_dist;
}

int main(){
    int n; cin >> n;
    vector<vector< pair<int,int> > > c(n);
    RangeFor(i, n) {
        int u;
        cin >> u;
        int k;
        cin >> k;
        c[u] = vector<pair<int, int> >(k);
        RangeFor(j, k) {
            int v;
            cin >> v;
            int d;
            cin >> d;
            c[u][j] = make_pair(v, d);
        }
    }

//    RangeFor(i, n){
//        RangeFor(j, c[i].size()){
//            std::cout << c[i][j].first << ' ' << c[i][j].second;
//        }
//        std::cout << std::endl;
//    }

    auto res = sssp(c);
    RangeFor(i, n){
        std::cout << i << ' ' << res[i] << std::endl;
    }
    return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<queue>
using namespace std;

static const int MAX=10000;
static const int INF=(1<<20);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n;
vector<pair<int,int>> adj[MAX];
void dijkstra()
{
	priority_queue<pair<int,int>> PQ;
	int color[MAX];
	int d[MAX];
	for(int i=0;i<n;i++)
	{
		d[i]=INF;
		color[i]=WHITE;
	}
	
	d[0]=0;
	PQ.push(make_pair(0,0));
	color[0]=GRAY;
	
	while(!PQ.empty())
	{
		pair<int,int> f=PQ.top();
		PQ.pop();
		int u=f.second;
		color[u]=BLACK;
		
		if(d[u]<f.first*(-1))continue;
		
		for(int j=0;j<adj[u].size();j++)
		{
			int v=adj[u][j].first;
			if(color[v]==BLACK)continue;
			if(d[v]>d[u]+adj[u][j].second)
			{
				d[v]=d[u]+adj[u][j].second;
			}
			PQ.push(make_pair(d[v]*(-1),v));
			color[v]=GRAY;
		}
	}
	for(int i=0;i<n;i++)
	{
		cout<<i<<" "<<(d[i]==INF?-1:d[i])<<endl;
	}
}

int main()
{
	int k,u,c,v;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>u>>k;
		for(int j=0;j<k;j++)
		{
			cin>>v>>c;
			adj[u].push_back(make_pair(v,c));
		}
	}
	
	dijkstra();
	
	return 0;
}