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++: # -*- coding: utf-8 -*-
from heapq import heapify, heappop, heappush

N = int(input())
INF = float("inf")
d = [INF for n in range(N)]
d[0] = 0
adj = [[] for _ in range(N)]

searched = [False for _ in range(N)]
searched[0] = True

for n in range(N):
    inp = tuple(map(int, input().split()))
    assert n == inp[0]
    for i in range(inp[1]):
        adj[n] = list(zip(inp[2::2], inp[3::2]))

heap = [[0, 0]]

while len(heap):
    pcost, p = heappop(heap)
    searched[p] = True
    
    if d[p] < pcost:
        continue
    
    for v, c in adj[p]:
        if not searched[v]:
            new_cost = pcost + c
            if new_cost < d[v]:
                d[v] = new_cost
                heappush(heap, [new_cost, v])

for n in range(N):
    print("{} {}".format(n, d[n]))

C++: #include<iostream>
#include<vector>
#include<queue>
#include <bitset>
using namespace std;
template< typename FirstType, typename SecondType >
struct PairComparator {
  bool operator()( const pair<FirstType, SecondType>& p1, const pair<FirstType, SecondType>& p2 ) const 
    {  
		return p1.first > p2.first;
    }
};
bool inMST(vector< std::pair<int,int> > & MST,int j) {
	for (int k=0;k<(int)MST.size();++k){
		if(j == MST[k].second)
			return true;
	}
	return false;
}
int main(){
	int n;
	cin >> n;
	//vector<int> E(n*n,-1);
	vector<vector<pair<int,int> > > edge(n);
	vector<int> DPG(n,-1);
	vector< std::pair<int,int> > MST;
	std::bitset<10000> flg;

	{
		for(int j=0;j<n;++j) {
			int u,k; 
			cin >> u >> k;
			edge[u].resize(k);
			for(int i=0;i<k;++i){
				int v,c;
				cin >> v >> c;
				edge[u][i] = make_pair(v,c);
			}
		}
	}
	priority_queue<pair<int,pair<int,int> >,vector< pair<int,pair<int,int> > >,
		PairComparator<int,pair<int,int> > > r;

	int cost = 0;
	int i=0;
	MST.push_back(pair<int,int>(i,i));
	flg.set(i);
	DPG[i] = 0;
	while((int)MST.size() < n) {
		int ii=i*n;
//		for (int j=0;j<n;++j) 
		for (int p=0;p<(int)edge[i].size();++p)
		{
			pair<int,int> add = std::make_pair(i,edge[i][p].first);
//			if (!inMST(MST,add.second))
			if (!flg.test(edge[i][p].first))
			{
				r.push(std::pair<int,std::pair<int,int> >(edge[i][p].second+DPG[i],add));
			}
		}
		pair<int,pair<int,int> > top;
		while (!r.empty()) {
			top = r.top();
			r.pop();
//			if (!inMST(MST,top.second.second))
			if (!flg.test(top.second.second))
			{
				break;
			}
		}
		cost += top.first;
		MST.push_back(top.second);
		flg.set(top.second.second);
		i = top.second.second;
		DPG[i] = top.first;
//		for(int j=0;j<n;++j)
		for (int p=0;p<(int)edge[i].size();++p)
		{
			int j = edge[i][p].first;
			if (DPG[j] < 0)continue;
			DPG[j] = std::min(DPG[j],DPG[i] + edge[i][p].second);
		}
	}
	for (int j=0;j<n;++j) {
		cout << j << " " <<  DPG[j] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using pii = pair<int, int>;

template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

//pow(llpow,modpow)
template <class T>
ll llpow(ll x, T n)
{
    ll ans = 1;
    if (x == 0)
        ans = 0;
    while (n)
    {
        if (n & 1)
            ans *= x;
        x *= x;
        n >>= 1;
    }
    return ans;
}
long long modpow(long long a, long long n, long long mod)
{
    long long res = 1;
    while (n > 0)
    {
        if (n & 1)
            res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
//最大公約数
template <class T>
inline T gcd(T x, T y)
{
    if (y == 0)
        return x;
    else
    {
        return gcd(y, x % y);
    }
}
//最小公倍数
template <class T>
inline T lcm(T x, T y) { return x / gcd(x, y) * y; }
//逆元
long long modinv(long long a, long long m)
{
    long long b = m, u = 1, v = 0;
    while (b)
    {
        long long t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0)
        u += m;
    return u;
}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)
#define white 0
#define gray 1
#define black 2

long long INF = 1LL << 60;
int main()
{
    int n;
    cin >> n;
    vector<pii> t[n];
    using plli = pair<ll, int>;
    rep(i, 0, n)
    {
        int u, k;
        cin >> u;
        cin >> k;
        rep(j, 0, k)
        {
            int v, c;
            cin >> v >> c;
            pii in = make_pair(v, c);
            t[u].push_back(in);
        }
    }
    vi state(n,white);
    vector<int> res(n, 1<<30);
    res[0]=0;
    priority_queue<plli, vector<plli>, greater<plli>> q; //cost,index
    q.emplace(0, 0);
    rep(i, 1, n)
    {
        q.emplace(1 << 30, i);

    }
    while (!(q.empty()))
    {
        pii node = q.top();
        q.pop();
        int index = node.second;
        int v = node.first;
        state[index]=black;
        if(res[index]<v)continue;
        res[index]=v;
        for (pii to : t[index])
        {
            if (state[to.first] != black && to.second + v < res[to.first] )
            {
                state[to.first]=gray;
                res[to.first]=to.second+v;
                q.emplace(res[to.first],to.first);
            }
        }
    }
    rep(i, 0, n) cout << i << " " << res[i] << endl;
    return 0;
}