Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const (
	MAX_N      = 10000
	MAX_WEIGHT = 100000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix, err := createAdjacentMatrix(n, sc)
	if err != nil {
		panic(err)
	}

	sssp([]int{0}, shortestList, adjacentMatrix)
	for _, v := range shortestList {
		fmt.Printf("%d %d\n", v.getEnd(), v.shortest)
	}
}

func (si ShortestInfo) getEnd() int {
	if len(si.edges) == 0 {
		return 0
	} else {
		return si.edges[len(si.edges)-1].end
	}
}

func createAdjacentMatrix(n int, sc *bufio.Scanner) ([][]int, error) {
	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			return nil, err
		}
		adjacentMatrix[id] = make([]int, n)
		for j := 0; j < n; j++ {
			adjacentMatrix[id][j] = -1
		}
		vertexNum, err := nextInt(sc)
		if err != nil {
			return nil, err
		}
		for j := 0; j < vertexNum; j++ {
			v, err := nextInt(sc)
			if err != nil {
				return nil, err
			}
			c, err := nextInt(sc)
			if err != nil {
				return nil, err
			}
			adjacentMatrix[id][v] = c
		}
	}
	return adjacentMatrix, nil
}

func sssp(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			if currentShortest.shortest+adj.weight < getShortest(adj.end, shortestList).shortest {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode]
	edges := []Edge{}
	for i, v := range adjacents {
		if v >= 0 {
			edges = append(edges, Edge{start: currentNode, end: i, weight: v})
		}
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <stdio.h>
#include <vector>
#include <algorithm>


using namespace std;

short heap_index[10000];

struct Info{
	Info(){
		to=0;
		weight = 0;
	}
	Info(short arg_to,int arg_weight){
		to = arg_to;
		weight = arg_weight;
	}
	short to;
	int weight;
};

class InfoPQ{
public:
	InfoPQ(){
		array = new Info[10000];
		n = 0;
	}
	void push(Info arg_info){
		array[n] = arg_info;
		int tmp = n;
		while(tmp != 0 && array[tmp].weight < array[(tmp-1)/2].weight){
			std::swap(array[tmp], array[(tmp-1)/2]);
			tmp = (tmp-1)/2;
		}
		n++;
	}

	bool isEmpty(){
		return n == 0;
	}

	void writeHeapIndex(){
		for(int i=0;i<n;i++){
			heap_index[array[i].to] = i;
		}
	}

	void update(short update_index,int new_weight){
		array[heap_index[update_index]].weight = new_weight;
		int tmp = heap_index[update_index];
		while(tmp != 0 && array[tmp].weight < array[(tmp-1)/2].weight){
			std::swap(heap_index[array[tmp].to],heap_index[array[(tmp-1)/2].to]);
			std::swap(array[tmp], array[(tmp-1)/2]);
			tmp = (tmp-1)/2;
		}
	}

	Info pop(){
		Info ret = array[0];
		n--;
		heap_index[array[n].to] = 0;
		std::swap(array[0],array[n]);
		int tmp = 0;
		while(2*tmp+1 <= n-1){
			if(2*tmp+2 <= n-1){
				if(array[2*tmp+2].weight < array[tmp].weight && array[2*tmp+2].weight <= array[2*tmp+1].weight){
					std::swap(heap_index[array[tmp].to],heap_index[array[2*tmp+2].to]);
					std::swap(array[2*tmp+2],array[tmp]);
					tmp = 2*tmp+2;
				}else if(array[2*tmp+1].weight < array[tmp].weight && array[2*tmp+1].weight <= array[2*tmp+2].weight){
					std::swap(heap_index[array[tmp].to],heap_index[array[2*tmp+1].to]);
					std::swap(array[2*tmp+1],array[tmp]);
					tmp = 2*tmp+1;
				}else{
					break;
				}
			}else{
				if(array[tmp].weight > array[2*tmp+1].weight){
					std::swap(heap_index[array[tmp].to],heap_index[array[2*tmp+1].to]);
					std::swap(array[tmp],array[2*tmp+1]);
					tmp = 2*tmp+1;
				}else{
					break;
				}
			}
		}
		return ret;
	}

private:
	Info* array;
	int n;
};



int main(){
	int n,count=0,tmp,left_node,right_node,dist;
	scanf("%d",&n);
	vector<Info> V[n];
	Info catcher;
	InfoPQ A;

	int P[n],zero_dist[n];

	for(int i=0;i<n;i++){
		P[i]=0;
		zero_dist[i] = 10000001;
	}
	zero_dist[0] = 0;
	P[0]=1;

	for(int i=0;i<n;i++){
		scanf("%d %d",&left_node,&tmp);
		if(left_node == 0){
			for(int k=0;k<tmp;k++){
				scanf("%d %d",&right_node,&dist);
				zero_dist[right_node] = dist;
			}
		}else{
			for(int k=0;k<tmp;k++){
				scanf("%d %d",&right_node,&dist);
				V[left_node].push_back(Info(right_node,dist));
			}
		}
	}

	for(short k=1;k<n;k++){
		A.push(Info(k,zero_dist[k]));
	}

	A.writeHeapIndex();

	while(count<n-1){

		catcher = A.pop();
		P[catcher.to]=1;
		count++;

		for(int k=0;k<V[catcher.to].size();k++){
			if(zero_dist[V[catcher.to][k].to] > catcher.weight+V[catcher.to][k].weight){
				zero_dist[V[catcher.to][k].to] = catcher.weight+V[catcher.to][k].weight;
				A.update(V[catcher.to][k].to,catcher.weight+V[catcher.to][k].weight);
			}
		}
	}

	for(int i=0;i<n;i++){
		printf("%d %d\n",i,zero_dist[i]);
	}

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

const int INF = 1 << 21;
const int MAX = 10003;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
int n;
vector<P> adj[MAX];

void dijkstra() {
  priority_queue<P> PQ;
  int color[MAX], d[MAX];

  fill((int*)d, (int*)(d+MAX), INF);
  fill((int*)color, (int*)(color+MAX), WHITE);

  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!PQ.empty()) {
    P f = PQ.top(); PQ.pop();
    int u = f.second;
    color[u] = BLACK;

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

    rep(j, adj[u].size()) {
      int v = adj[u][j].first;
      if (color[v] == BLACK) continue;
      if (d[v] > d[u] + adj[u][j].second) {
	d[v] = d[u] + adj[u][j].second;
	PQ.push(make_pair(d[v] * (-1), v));
	color[v] = GRAY;
      }
    }
  }

  rep(i,n) {
    cout << i << ' ' << (d[i] == INF ? -1 : d[i]) << endl;
  }
}

int main() {
  cin >> n;  
  int u, k, v, c;
  rep(i,n) {
    cin >> u >> k;
    rep(j,k) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }

  dijkstra();
  return 0;
}