Go: package main

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

type Node struct {
	Id int
	Dis int
}

type PriorityQueue []*Node

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

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].Dis < pq[j].Dis
}

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

func (pq *PriorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.(*Node))
}

func (pq *PriorityQueue) Pop() interface{} {
	n := len(*pq)
	pop := (*pq)[n-1]
	*pq = (*pq)[0:n-1]
	return pop
}

func main() {
	var n int
	fmt.Scan(&n)

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

	g := make([][]int, n)
	c := make([][]int, n)
	pq := make(PriorityQueue, 0, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		g[u] = make([]int, k)
		c[u] = make([]int, k)
		for j := 0; j < k; j++ {
			sc.Scan()
			g[u][j], _ = strconv.Atoi(sc.Text())
			sc.Scan()
			c[u][j], _ = strconv.Atoi(sc.Text())
		}

		if u == 0 {
			d[u] = 0
		} else {
			d[u] = 1 << 30
		}
		pq.Push(&Node{u, d[u]})
	}
	heap.Init(&pq)

	for pq.Len() > 0 {
		node := heap.Pop(&pq).(*Node)
		for i := 0; i < len(g[node.Id]); i++ {
			nextId := g[node.Id][i]
			nextDis := node.Dis + c[node.Id][i]
			if nextDis < d[nextId] {
				d[nextId] = nextDis
				heap.Push(&pq, &Node{nextId, nextDis})
			}
		}
	}

	wr := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		wr.WriteString(strconv.Itoa(i))
		wr.WriteString(" ")
		wr.WriteString(strconv.Itoa(d[i]))
		wr.WriteString("\n")
	}
	wr.Flush()
}

C++: ////////////////////////////////////////
//ALDS1_12_B:   Single Source Shortest Path I
//ALDS1_12_C:   Single Source Shortest Path II
//priority_queue????????¨??????????£?????????´??????1?????????????´????????£?????????????????????????
////////////////////////////////////////

#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <unordered_map> //hash func.
#include <fstream> //ifstream, ofstream

//#define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will be ignored. You had better define NDEBUG when u submit the code.
#include <cassert> //assert


using namespace std;

#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???                           ????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif

//If u wanna output to a text file instead of standard output, plz define OUTPUTFILE.
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif

//If u wanna input from a text file instead of standard input, plz define INPUTFROMTEXTFILE???.
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif


#define disp(A) dout << #A << " = " << setw(3) << (A) << endl
#define disP(A) dout << setw(3) << (A) << " "
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;


const int INF = 2e9-1;
const ll INF_LL = 1e18; //Be careful for overflow.
const int NONE = -1;

#define N_MAX 10000 //num of vertex
#define M_MAX 10000 //num of edge

int N; //num of vertex
int adjacencyMatrix[N_MAX][N_MAX];
//vector< vector< pii > > adjacencyList; //adjacencyList[i] = list of <adjacent vertex, cost to it from i>

ll minCostTo[N_MAX]; //min cost to i from 0. Will be updated by Daijkstra Algorithm.

//vi V; //vertices which is not yet determined by Dijkstra





int main() {
    //define priority queue
    auto func = [](pii x, pii y) {return x.second > y.second; };
    priority_queue<pii, vector<pii>, decltype(func)> Q(func);
    
    
    //read input data and initialize
    din >> N;
    
    rep(i,0,N) {
        minCostTo[i] = NONE;
        rep(j,0,N) {
            adjacencyMatrix[i][j] = NONE;
        }
    }
    
    int u, numOfAdjacentNode, v, c;
    rep(i,0,N) {
        din >> u >> numOfAdjacentNode; //u == i
        
        vector< pii > list;
        
        rep(j,0,numOfAdjacentNode) {
            din >> v >> c;
            adjacencyMatrix[u][v] = c;
        }
    }
    
//        //DEBUG
//        rep(i,0,N) {
//            rep(j,0,N) {
//                dout << setw(3) << adjacencyMatrix[i][j] << " ";
//            }
//            dout << endl;
//        }
//        dout << endl;
    
    
    //Dijkstra Algorithm
    Q.push(make_pair(0, 0));

    pii target;
    while( !Q.empty() ) {
        target = Q.top(); //Q????????????????????????????????????????°??????????u?????????????????????
        Q.pop();
        u = target.first;
        
        if( minCostTo[u] != NONE ) continue; //u has already been finalized. ----- *1
        
        minCostTo[u] = target.second; //u???????????????"?¢????"?????????
        
//                //DEBUG
//                dout << "---------------\n";
//                disp(u);
//                dout << "cost = "; disp(target.second);
//                dout << endl;
        
        rep(i,0,N) {
            v = i;
            c = adjacencyMatrix[u][v];
            
//                        //DEBUG
//                        dout << "---\n";
//                        disp(v);
//                        disp(c);
//                        dout << endl;
//            disp(minCostTo[v]);
//            disp(minCostTo[u]);
//            disp(minCostTo[u] + c);
            
            
            if( minCostTo[v]==NONE && c!= NONE) { //?????§???"?¢????"???????????????????????????????????????u->v??????????????¨???????????´??????v?????????????????´??°??????????????§??????????????????
                Q.push( make_pair(v, minCostTo[u] + c) ); //u????????£???v?????????????????´??°????????????????????????????????\??????v???enq
                //??????????????§?????§????????????????????§???v??????Q????????????????????????????????§????????????????????????????¨? *1 ????????¶?????????????????????????????¨?????????
//                dout << "enq " << v << endl;
            }
        }
        
        
//        //DEBUG
//        dispAll(minCostTo, N);
    }
    
    
    //output
    rep(i,0,N) {
        dout << i << " " << minCostTo[i] << endl;
    }
    
    
    
#ifdef INPUTFILE
    inputfile.close();
#endif
    
#ifdef OUTPUTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}