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 <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ld long double
using ll = long long;
#define ALL(v) (v).begin(),(v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;  
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>> 
#define fi first
#define se second

void print(Vl vec){
    rep(i, 0, sz(vec)){
        if(i) cout << " ";
        cout << vec[i];
    }
    coel;
}

////////////////////////////////////////////////////////

// デバッグを消し忘れるな！！！

// dijkstra
// 有向グラフ

const ll mx = 10005;

vector<pair<ll, ll>> adj[mx];
ll d[mx];
ll color[mx];
ll n;

// 頂点0から各頂点vまでの最短経路

void dijkstra(){
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    rep(i, 0, mx) d[i] = INF;
    d[0] = 0;
    color[0] = 1;
    pq.push({0, 0});

    while(!pq.empty()){
        pair<ll, ll> f = pq.top(); pq.pop();
        ll u = f.second; // 番号
        color[u] = 1;
        if(d[u] < f.first) continue;
        rep(j, 0, sz(adj[u])){
            ll v = adj[u][j].first;
            if(color[v])  continue;
            if(d[v] > d[u] + adj[u][j].second){
                d[v] = d[u] + adj[u][j].second;
                pq.push({d[v], v});
            }
        }
    }

    rep(i, 0, n){
        cout << i << " " << (d[i] == INF ? -1 : d[i]) << endl;
    }

}

int main() {
    cin >> n;
    rep(i, 0, n){
        ll u, k; cin >> u >> k;
        rep(j, 0, k){
            ll v, c; cin >> v >> c;
            adj[u].push_back({v, c});
        }
    }

    dijkstra();

    return 0;
}

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

#define MAX 10001
#define INF 5000001

int a[MAX][MAX];
int n;
int d[MAX];

void Relax(int u, int v){
  if(d[v]> d[u]+a[u][v]){
    d[v]=d[u]+a[u][v];
  
  }
}

void ISS(int s){
  int v;
  for(v=0; v<n; v++){
    d[v]=INF;
  }
  d[s]=0;
}

void Dijkstra(int s){
  int i, v1, v, minv, u; 
  int flag[MAX];
  ISS(s);
  
  for(i=0; i<n; i++) flag[i]=0;

  for(v1=0; v1<n; v1++){
    
      minv = INF;
      // decide u
      for ( i = 0; i < n; i++ ){
          if(flag[i]==1) continue; 
          if(d[i]<=minv){
              u=i;
              minv=d[i];
          }
      }   
      for(v=0; v<n; v++){
          if(a[u][v]==-1) continue;
          if(flag[v]==1) continue;
          /* if(a[u][v]<d[v]){
             d[v]=a[u][v];
             }*/
          Relax(u, v);
      }
      flag[u]=1;
  }
}

int main(){

  int i, j ,u ,k,v,c;
  // int a[MAX][MAX];
  cin>>n;

  for(i=0; i<n; i++){
    for(j=0; j<n; j++){
      a[i][j]=-1;
    }
  }

  for(i=0; i<n; i++){
      cin>>u>>k;
    for(j=0; j<k; j++){
        cin>>v>>c;
        a[i][v]=c;
    }
  }

  /*for(i=0; i<n; i++){
    for(j=0; j<n; j++){
    cout<<a[i][j]<<endl;
    }
    cout<<endl;
    }*/
  Dijkstra(0);
  for(i=0; i<n; i++){
      cout<<i<<" "<<d[i]<<endl;
  }

  return 0;
}