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 <algorithm>
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;

static const int Max = 100;
static const int Inf = (1<<20);
enum State {
	Not_Searched = 0,
	Connected = 1,
	Searched = 2
};

struct Node {
	int idx;
	int cost;
	Node(int i, int c):	idx(i),	cost(c)	{}
};

int n = 0;
//vector<pair<int,int> > adj[Max]; // pair<??£??\??????index, ??????cost> 
vector <vector<pair<int, int> > >adj; // pair<??£??\??????index, ??????cost>
//vector<Node> adj[Max];

void dijkstra() {
	priority_queue<pair<int, int> > pq; // pair<start ???????????????????????????????????§????????????????´???????idx>
	int st[Max];
	int min_cost[Max];
	for (int i = 0; i < n; i++) {
		st[i] = Not_Searched;
		min_cost[i] = Inf;
	}

	min_cost[0] = 0;
	pq.push(make_pair(0, 0));
	st[0] = Connected;

	while (!pq.empty()) {
		pair<int, int> f = pq.top();
		pq.pop();
		int u = f.second;
		st[u] = Searched;

		if (min_cost[u] < f.first * (-1)) continue;

		for (int i = 0; i < adj[u].size(); i++) {
			int adj_u_idx = adj[u][i].first;
			if (st[adj_u_idx] == Searched) continue;
			if (min_cost[adj_u_idx] > min_cost[u] + adj[u][i].second) {
				min_cost[adj_u_idx] = min_cost[u] + adj[u][i].second;
				pq.push(make_pair(min_cost[adj_u_idx]*(-1), adj_u_idx));
				st[adj_u_idx] = Connected;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		printf("%d %d\n", i, (min_cost[i] == Inf ? -1 : min_cost[i]));
		//cout << i << " " << (min_cost[i] == Inf ? -1 : min_cost[i]) << endl;
	}
	return;
}

int main() {
	cin >> n;
	//cout << n << endl;
	int u, k, vi, ci;
	for (int i = 0; i < n; i++) {
		//scanf("%d %d", &u, &k);
		cin >> u >> k;
		vector<pair<int, int> > buf;
		buf.reserve(k);
		for (int j = 0; j < k; j++) {
			//scanf("%d %d", &vi, &ci);
			cin >> vi >> ci;
			buf.push_back(make_pair(vi, ci));
			//adj[u].push_back(make_pair(vi, ci));
            //Node n(vi, ci);
			//adj[u].push_back(n);
		}
		adj.push_back(buf);
	}
	dijkstra();
	return 0;
}