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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll;  // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll;     // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define TS to_string
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)


ll INF = 1e9;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b;}     //最小公倍数

void yes(){ cout << "Yes" << endl;}
void no(){ cout << "No" << endl;}

//-----------------------------------------

class dijkstra{
  private:
    ll N; // 頂点数
    struct edge{
      ll to; ll cost; // 頂点iからの行き先とそのコスト
    };
    vector<vector<edge>> G; // 辺の隣接リスト
    vector<vector<edge>> F; // 辺の隣接リスト
    vll dis; // sからiへの最小コスト
    vll par; // iの一個前の頂点
    ll inf = 1e18; 
    //pair<sからiへ行くのの暫定最小コスト、i>の優先度付きキュー
    priority_queue<pll, vector<pll>, greater<pll>> que;
  public:
    dijkstra(ll n){
      N = n;
      G.resize(N);
      F.resize(N);
      dis.resize(N);
      par.resize(N);
    }

    // 頂点iからの辺を入力
    void in_edge(ll i, ll to_, ll cost_){
      edge e = {to_, cost_};
      G[i].push_back(e);
    }

    // 辺の逆転
    void rev_edge(){
      for(ll i=0;i<N;i++){
        for(ll j=0;j<G[i].size();j++){
          edge tmp = G[i][j];
          edge in = {i, tmp.cost};
          F[tmp.to].push_back(in);
        }
      }
    }

    // 初期化
    void init(ll n){
      while(!que.empty()) que.pop();

      for(ll i=0;i<n;i++){
        dis[i] = inf; // コストの初期化
        par[i] = i; // 親の初期化
      }
    }

    // 頂点sから各頂点への最短距離の配列を返す
    vll dis_from_s(ll s){
      init(N);

      dis[s] = 0;
      que.push(make_pair(0,s));

      while(!que.empty()){
        pll pai = que.top(); que.pop();
        ll v = pai.second;

        if(dis[v] < pai.first) continue;

        for(ll i=0;i<G[v].size();i++){
          edge tmp = G[v][i];
          if(dis[tmp.to] > dis[v] + tmp.cost){
            dis[tmp.to] = dis[v] + tmp.cost;
            par[tmp.to] = v;
            que.push(make_pair(dis[tmp.to],tmp.to));
          }
        }
      }
      return dis;
    }

    // 各頂点からsへの最短距離への最短距離を返す (有向グラフの辺を逆転し、sからの最短距離を求める)
    vll dis_to_s(ll s){
      init(N);
      rev_edge();
      que.push(make_pair(0,s));

      while(!que.empty()){
        pll pai = que.top(); que.pop();
        ll v = pai.second;

        if(dis[v] < pai.first) continue;

        for(ll i=0;i<F[v].size();i++){
          edge tmp = F[v][i];
          if(dis[tmp.to] > dis[v] + tmp.cost){
            dis[tmp.to] = dis[v] + tmp.cost;
            par[tmp.to] = v;
            que.push(make_pair(dis[tmp.to],tmp.to));
          }
        }
      }
      return dis;
    }

    // sからtまでの最短経路
    vll get_path_s_to_t(ll t){
      vector<ll> res;
      ll i = t;
      while(i != par[i]){
        res.push_back(i);
        i = par[i];
      }
      reverse(ALL(res));
      return res;
    }
};
//-----------------------------------------

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  dijkstra D(n);
  for(ll i=0;i<n;i++){
    ll v, k;
    cin >> v >> k;
    for(ll j=0;j<k;j++){
      ll a, b;
      cin >> a >> b;
      D.in_edge(i,a,b);
    }
  }
  vll tmp = D.dis_from_s(0);
  REP(i,n) cout << i << " " << tmp[i] <<endl;

  return 0;
}