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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long INF = 1LL << 60;    // 1.15x10^18
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using edge = pair<int, long long>;
using Graph = vector<vector<edge>>;

vector<long long> dijkstra(const Graph &g, int s) {
    int n = g.size();
    vector<long long> dist(n, INF);
    vector<bool> fixed(n);
    dist[s] = 0;
    while (true) {
        int u = -1;
        for (int i = 0; i < n; i++) if (!fixed[i]) {
            if (u == -1 || dist[i] < dist[u]) u = i;
        }
        if (u == -1) break;
        fixed[u] = true;
        for (auto &e: g[u]) chmin(dist[e.first], dist[u] + e.second);
    }
    return dist;
}

int main() {
    int V; cin >> V;
    Graph g(V);
    for (int i = 0; i < V; i++) {
        int u, k; cin >> u >> k;
        while (k--) {
            int v, c; cin >> v >> c;
            g[u].emplace_back(v, c);
        }
    }
    auto dist = dijkstra(g, 0);
    for (int u = 0; u < V; u++) cout << u << " " << dist[u] << endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<stdio.h>
using namespace std;

static const int INFTY = (1<<21);
enum Color{
    WHITE,
    GRAY,
    BLACK
};
int n;

//int M[100][100];
vector<pair<int,int> > adj[10000];   //隣接リスト
priority_queue<pair<int, int> > PQ; 
Color color[10000];
int d[10000];

void dijkstra(){
    for(int i=0; i<n; i++){
        color[i] = WHITE;
        d[i] = INFTY;
    }
    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, size = adj[u].size(); j<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]==INFTY)? -1:d[i]) << endl;
    }
}

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, g;
        cin >> v >> g;
        adj[u].push_back(make_pair(v, g));
        }
    }

    dijkstra();
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<utility>
#include<vector>


using namespace std;



int main() {
	int n,tmp1,tmp2,size;
	int cnt_arrived=0;
	pair<int, int> ptmp;
	priority_queue<pair<int, int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
	cin >> n;
	int** mat = new int* [n];
	vector<pair<int, int>>* list_adjacent = new vector<pair<int, int>>[n];
	int* dist = new int[n];
	for (int i = 0;i < n;i++) {
		cin >> tmp1 >> size;
		for (int j = 0;j < size;j++) {
			cin >> tmp1 >> tmp2;
			list_adjacent[i].push_back(make_pair(tmp1, tmp2));
		}
		dist[i] = -1;
	}
	pq.push(make_pair(0, 0));
	while (cnt_arrived < n) {
		ptmp = pq.top();
		pq.pop();
		if (dist[ptmp.second] == -1) {
			cnt_arrived++;
			dist[ptmp.second] = ptmp.first;
			for (int i = 0;i < list_adjacent[ptmp.second].size();i++) {
				pq.push(make_pair(ptmp.first + list_adjacent[ptmp.second][i].second, list_adjacent[ptmp.second][i].first));
			}
		}
	}
	for (int i = 0;i < n;i++) {
		cout << i << " " << dist[i] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INFTY (1<<30)

int n;
vector<int> color(10001,0),d(10001,INFTY);
vector<vector<pair<int,int>>> M(10001,vector<pair<int,int>>(0));
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;

void dijkstra(int s){
    d[s]=0;
    pq.push(make_pair(0,s));
    while(!pq.empty()){
        int u=pq.top().second;
        int a=pq.top().first;
        pq.pop();
        color[u]=2;
        if(d[u]<a) continue;
        for(int i=0;i<M[u].size();i++){
            int v=M[u][i].first;
            if(color[v]!=2){
                if(d[u]+M[u][i].second<d[v]){
                    d[v]=d[u]+M[u][i].second;
                    color[v]=1;
                    pq.push(make_pair(d[v],v));
                }
            }
        }
    }

}

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        int a,b,c,k;
        cin>>a>>k;
        for(int j=0;j<k;j++){
            cin>>b>>c;
            M[a].push_back(make_pair(b,c));
        }
    }

    dijkstra(0);

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