Go: package main

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

type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }
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 = i
	pq[j].index = j
}
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
}

type Q struct {
	pq *PriorityQueue
	ds map[int]int // It has always same value as pq. For random access.
}

func newQ() *Q {
	pq := new(PriorityQueue)
	heap.Init(pq)
	return &Q{
		pq: pq,
		ds: make(map[int]int),
	}
}
func (q *Q) pop() (u, d int) {
	item := heap.Pop(q.pq).(*Item)
	delete(q.ds, item.value)
	return item.value, item.priority
}
func (q *Q) get(u int) (int, bool) { // returns d
	d, ok := q.ds[u]
	return d, ok
}
func (q *Q) set(u, d int) {
	q.ds[u] = d
	for _, item := range *q.pq {
		if item.value != u {
			continue
		}

		item.priority = d
		heap.Fix(q.pq, item.index)
		return
	}
	heap.Push(q.pq, &Item{
		priority: d,
		value:    u,
	})
}
func (q *Q) len() int {
	return len(q.ds)
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := newQ()
	q.set(0, 0)
	visited := make(map[int]struct{})
	out := make([]int, n)

	for q.len() > 0 {
		u, d := q.pop()
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			dd := d + c
			if d, ok := q.get(uu); !ok || d > dd {
				q.set(uu, dd)
			}
		}

		out[u] = d
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, 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;
}