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++: /**
 * @brief  ??????????????????(shortest paths problem)????????????Dijkstra?????¢?????´?????????(Dijkstra's algorithm)?????±???
 * @note   ??¢??£URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_B
 * @date   2016/02/20 ~ 2016/03/13
 */



//****************************************
// ???????????????????????????????????????????????????
//****************************************

#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#include <cassert>
#include <functional>



//****************************************
// ???????????§????????¢?????????????????????
//****************************************

#define GRAPH_BEGIN namespace graph {
#define GRAPH_END   }



//****************************************
// ????????????????§????
//****************************************

GRAPH_BEGIN



//****************************************
// ?§????????????????
//****************************************

using weight_t   = std::int32_t;  /**< ???(u, v)????????????w?????¨?????? */
using index_t    = std::int32_t;  /**< ??????v??????????????¨??????       */
using capacity_t = weight_t;      /**< ???(u, v)??????????????¨??????    */



namespace limits {
    enum {  // scoped??§????????????????????????????????????enum(strongly-typed enum)??§?????????
        inf = std::numeric_limits<weight_t>::max() / 3,  /**< @brief ???????????¨???????????´???????????¨????????????     */
        nil = std::numeric_limits<index_t>::min() / 3,   /**< @brief ?????????????????¨???????????´???????????¨???????????? */
    };
}


/**
 * @brief  ????????????????§???????(scoped enum)
 * @detail (u, v) ??? E??§??????u??????????????°??????v?????°????????§??????
 *         ?????????????????????????????£??\????????¨?????????????????????????????§??????
 *         ??°???????????????????????£??\???????????¨???????????????????????????????????¢??????????????¨???????????????????¢?????????????
 */
enum struct vcolor : std::int32_t {
    white,  /**< ??????????????? */
    black,  /**< ??¢???????????? */
    gray,   /**< ??¢???????????? */
};


/**
 * @brief ??°????????¨?????????(??????) 
 */
struct vertex {
    union {
        weight_t d;       /**< ?§????s??????????????¢  */
        weight_t key;     /**< Prim?????¢?????´???????????????????????¨????±?????????????????????¨????????¶?????? */
    };
    index_t pi;           /**< ????????????(?????????) */
    union {
        vcolor color;     /**< ????????????        */
        bool  visited;    /**< ????????????????     */
    };
    // weight_t f;           /**< ??????????????°(DFS?????????????????????????????????????????¨??????????????????)     */
    vertex() : d(0), pi(0), color(vcolor::white)/*, f(0)*/ {}
};

/**
 * @brief ??°????????¨??¨??????(???)
 * @note  G = (V, E)???????????¢??°w???????????????????????°????????¨????????¨???
 *        ???(u, v) ??? E????????????w(u, v)??¨??¨?????????
 */
struct edge {
    index_t  src;   /**< ???????§????u */
    index_t  dst;   /**< ????????????v */
    union {
        weight_t w;     /**< ???(u, v)????????????(?????????) */
        capacity_t c;   /**< ???(u, v)????????? */
    };
    edge() = default;
    edge(index_t src, index_t dst) : src(src), dst(dst), w(1) {}
    edge(index_t src, index_t dst, weight_t w) : src(src), dst(dst), w(w) {}
};



using edges_t    = std::vector<edge>;      /**< ??°??????G=(V, E)????????????E   */
using vertices_t = std::vector<vertex>;    /**< ??°??????G=(V, E)???????????????V */
using array_t    = std::vector<weight_t>;  /**< ??????w?????????  */
using indices_t  = std::vector<index_t>;   /**< ????????????????????? */
using matrix_t   = std::vector<array_t>;   /**< ??°??????G?????£??\????????¨???(???????????¨????????¨???) */
using graph_t    = std::vector<edges_t>;   /**< ??°??????G?????£??\???????????¨???(???????????????????????¨??????) */



//****************************************
// ??¢??°?????????
//****************************************

/**
 * @brief  Prim?????¢?????´???????????????????????????????°???¨?????¨A????±?????????????????????????(A????????¨??\?¶??????????????????????)?????£???????????????????????¢??????
 *         Dijkstra?????¢?????´????????????????????????????§????s?????????????°???????????????¨?????????????????????u ??? V - S???????????????(??????S????±??????????"????????????"??????????±???????)
 *
 * @note   ????????¢??°???????????????????????????Heap???????????????Prim?????????Dijkstra?????¢?????´???????????§?????????
 *         ????????§??????Heap?????¨???????????§min????????????????????\????????????????????????
 *         ?????????????????¢?????´?????????????¨???°??????????????????????????????????????????????????????????????°?????????????????¨????????????
 *         ?????£??????v.visited???false?????¨??????v.color?????????????????°?????§?????????true?????¨??????????????§??????
 *
 * @param  const vertices_t& V ??¢?´¢????±????????????????V
 * @param  index_t n           ????????????V???????????°n
 * @return index_t u           ?????¶??????????????????u(?????¶??????????????????????????¨???????????´??????nil?????????)
 */
static index_t extract_min(const vertices_t& V, index_t n)
{
    index_t  u = limits::nil;
    weight_t d = limits::inf; 
    for (index_t v = 0; v < n; ++v) {
        if (!V[v].visited && V[v].d < d) { d = V[v].d; u = v; }
    }
    return u;
}


/**
 * @brief  ??(V)???????¶????????????£??????????????¨????????¨???????????????????¨??????????????????°??????????????????
 * @note   ??????????????????????????????v ??? V????????????v.?? = NIL???v.visited = false
 *         ????????????v ??? V - {s}????????????v.d = ?????§?????????
 */
static inline void initialize_single_source_with_visitor(vertices_t& V, index_t s)
{
    for (auto&& v : V) {
        v.d       = limits::inf;
        v.pi      = limits::nil;
        v.visited = false;
    }
    V[s].d = 0;
}


/**
 * @brief  ???(u, v)?????????(relaxing)??????u????????±???????????¨??§v????????¢??\???????????????
 *         ????????§??????????????????????????????????????§??????????????°v.d??¨v.???????´??°??????
 *         ??????????????£??????????????¨??????v.d???????°???????v?????????????±???§v.???????´??°??????????????¨?????????
 *
 * @note   ??\????????????????????????(u, v)?????????????????(1)????????§????????????
 *         ????????????????¶????pred(V, u)?????(1)??§????????????????????¨???????????????
 *
 * @param  vertices_t& V   ????????????V
 * @param  index_t u       ???(u, v)????§????u (????????????u ??? V)
 * @param  index_t v       ???(u, v)?????????v (????????????v ??? V)
 * @param  weight_t w      ???(u, v)?????????w
 * @param  Predicate pred  relax????????????????????¶????¨???°????????°???
 */
static void relax(vertices_t& V,
           index_t u, index_t v, weight_t w,
           std::function<bool(const vertices_t&, index_t)> pred)
{
    if (pred(V, u) && V[v].d > V[u].d + w) {
        V[v].d = V[u].d + w;
        V[v].pi = u;
    }
}


// ??????????????????????????????relax??¢??°???

static inline void relax(vertices_t& V, const edge& e,
           std::function<bool(const vertices_t&, index_t)> pred)
{
    relax(V, e.src, e.dst, e.w, pred);
}
static inline void relax(vertices_t& V, const matrix_t& W,
           index_t u, index_t v,
           std::function<bool(const vertices_t&, index_t)> pred)
{
    relax(V, u, v, W[u][v], pred);
}


/**
 * @brief  ????????????????????????????????§????????¨????????????????????§???Dijkstra(??????????????????)?????¢?????´?????????(Dijkstra's algorithm)???
 *         ????????????????????°??????G = (V, E)?????????????§???????????????????????§£???. ????????§?????????(u, v) ??? E????????????w(u, v) >= 0???????????????
 *
 * @note   ??????v ??? V??????????????????????????(V)?????????????????????????????§?????¨?????§??(V^2)???????????????
 *
 * @param  const matrix_t& W    ?????????????????????????????°??????W
 * @param  index_t         s    ?§????s
 * @return ?§????s????????????????????????????????????????±????????????????????????????S
 */
vertices_t dijkstra(const matrix_t& W, index_t s)
{
    index_t n = W.size();
    vertices_t S(n);
    auto relax_pred = [](const vertices_t& V, index_t u) -> bool { return !V[u].visited; };    


    initialize_single_source_with_visitor(S, s);  // ?????????????????????d?????¨???????????????????????
    while(true) {     
        // ?§????s?????????????°???????????????¨?????????????????????u ??? V - S???????????????
        index_t u = extract_min(S, n);   
        if (u == limits::nil) { break; }    // ??????u???NIL?????????????????°?????¢?´¢???????????§??????
        for (index_t v = 0; v < n; v++) {
            relax(S, W, u, v, relax_pred);  // u????????±???????????¨??§v??????????????????????????§????????´??????????????¨??????v.d??¨?????????v.pi?????´??°??????
        }
        S[u].visited = true;  // ??????????????????S????±????
    }
    // ??????????????§??????S = V????????§?????????????????????u ??? V????????????u.d = ?´(s, u)??§??????
    // ?????????????????¨?????????????????¨?????°??????G?????s????????¨?????????????????¨??§??????
    return S;
}



//****************************************
// ?????????????????????
//****************************************

GRAPH_END



//****************************************
// ??¨?????????????????????
//****************************************

int main(void)
{
    using namespace std;
    using namespace graph;
    
    matrix_t M;
    int n, k, u, v, c;
    
    cin >> n; M.resize(n);
    for (int j = 0; j < n; j++) {
        M[j].resize(n);
        for (int i = 0; i < n; i++) {
            M[j][i] = limits::inf;
        }
    }
    for (int j = 0; j < n; j++) {
        cin >> u >> k;
        for (int i = 0; i < k; i++) {
            cin >> v >> c;
            M[u][v] = c;
        }
    }
    vertices_t S = dijkstra(M, 0);
    for (int i = 0; i < n; i++) {
        cout << i << " " << S[i].d << endl; 
    }

    return 0;
}