Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

//make priority queue
type Item struct {
	id       int
	priority int // this state is priority and distans from start
	index    int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

// we want the ascending-priority-queue.
// So we use less than here.
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = j
	pq[j].index = i
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(dist int, id int) {
	for _, i := range *pq {
		if i.id == id {
			if i.priority > dist {
				i.priority = dist
			}
		}
	}
}

// the main code
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	return n
}

func dikstra(dist [][]int, n int) []int {
	// preparate priority queue
	inf := 1 << 30
	pq := make(PriorityQueue, n)
	shortests := make([]int, n)
	for i := 0; i < n; i++ {
		pq[i] = &Item{
			id:       i,
			priority: inf,
			index:    i,
		}

		shortests[i] = inf
	}
	pq[0].priority = 0
	shortests[0] = 0
	heap.Init(&pq)

	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		shortests[item.id] = item.priority
		for id, d := range dist[item.id] {
			if d != -1 {
				pq.update(item.priority+d, id)
			}
		}
		heap.Init(&pq)
	}
	return shortests
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// init
	n := nextInt(sc)
	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dist[i][j] = -1
		}
	}

	// std Input
	for i := 0; i < n; i++ {
		u := nextInt(sc)
		k := nextInt(sc)
		for j := 0; j < k; j++ {
			v, c := nextInt(sc), nextInt(sc)
			dist[u][v] = c
		}
	}

	shortests := dikstra(dist, n)
	for i, d := range shortests {
		fmt.Printf("%d %d\n", i, d)
	}
}

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

struct Node {
  int v, cost;
  bool operator>(const Node &n) const {
    return cost > n.cost;
  }
};

int main() {
  int n;
  cin >> n;

  vector<vector<int>> g(n, vector<int>(n, -1));
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v, c;
      cin >> v >> c;
      g[u][v] = c;
    }
  }

  vector<int> dists(n, -1);
  priority_queue<Node, vector<Node>, greater<Node>> q;
  q.push({0, 0});
  while (!q.empty()) {
    Node node = q.top();
    q.pop();

    if (dists[node.v] != -1) {
      continue;
    }
    dists[node.v] = node.cost;

    for (int i = 0; i < n; i++) {
      if (g[node.v][i] != -1) {
	q.push({i, node.cost + g[node.v][i]});
      }
    }
  }

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

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
template<class T> inline bool chmax(T& a, T b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, T b) {if (a>b) {a=b; return true;} return false;}

const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;

int main() {
  ll n; cin >> n;
  vvll edges(n, vll(n, -1));
  REP(i, n) {
    ll u, k; cin >> u >> k;
    REP(j, k) {
      ll v, c; cin >> v >> c;
      edges.at(u).at(v) = c;
    }
  }
  vll d(n, HIGHINF);
  V<bool> visited(n, false);
  d.at(0) = 0;
  REP(i, n) {
    ll now = -1;
    ll mind = HIGHINF;
    REP(j, n) {
      if (!visited.at(j) && d.at(j) < mind) {
        now = j;
        mind = d.at(j);
      }
    }
    REP(j, n) {
      if (edges.at(now).at(j) != -1 && d.at(j) > d.at(now) + edges.at(now).at(j)) {
        d.at(j) = d.at(now) + edges.at(now).at(j);
      }
    }
    visited.at(now) = true;
  }
  REP(i, n) cout << i << ' ' << d.at(i) << endl;
  return 0;
}