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<algorithm>
#include<queue>
#include<vector>
#include<string.h>
using namespace std;
#define NIL -2000000001
#define INFTY 2000000001
#define MAX 100001
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n = 0;
int color[MAX], d[MAX];
vector<pair<int, int> > G[MAX];


void sp() {
  for ( int i = 0; i < n; i++ ) {
    color[i] = WHITE;
    d[i] = INFTY;
  }
  priority_queue<pair<int, int> > PQ;
  PQ.push(make_pair(0, 0));
  d[0] = 0;
  color[0] = GRAY;

  while (true) {
    int mincost;
    int u; //訪問する頂点

    // for (int i = 0; i < n; i++) {
    //   if (color[i] != BLACK && d[i] < mincost) {
    //     mincost = d[i];
    //     u = i;
    //   }
    // }

    pair<int, int> f = PQ.top();
    PQ.pop();
    u = f.second;
    mincost = (-1)*f.first;

    color[u] = BLACK;

    // u周りの頂点vの情報を入れる
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i].first;
      if (color[v] != BLACK && d[u]+G[u][i].second < d[v]) {
        d[v] = d[u]+G[u][i].second;
        color[v] = GRAY;
        PQ.push(make_pair(-d[v], v));
      }
    }
    if (PQ.empty()) break;
  }

  for (int v = 0; v < n; v++) {
    printf("%d %d\n", v, d[v]);
  }
}

int main() {
  scanf("%d", &n);
  for ( int i = 0; i < n; i++ ) {
    int stash, k;
    scanf("%d %d", &stash, &k);
    for ( int j = 0; j < k; j++) {
      int v;
      scanf("%d", &v);
      int g;
      scanf("%d", &g);
      G[i].push_back(make_pair(v, g));
    }
  }

  sp();

  return 0;
}