Go: package main

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

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	route := make([][]Node, n)
	weight := make([]int, n)
	for i := 0; i < n; i++ {
		weight[i] = NIL
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		route[i] = make([]Node, k)
		for j := 0; j < k; j++ {
			route[u][j] = Node{id: scanInt(), weight: scanInt()}
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for _, nextNode := range route[node.id] {
			if weight[nextNode.id] != NIL {
				continue
			}
			nextNode.weight += node.weight
			heap.Push(h, nextNode)
		}
	}

	for i, w := range weight {
		fmt.Println(i, w)
	}
}

C++: #include <cstdio>
#include <vector>
#include <queue>

int main(void){

	int n, count, next_node, num_neighbors;
	std::vector< std::vector<int> > edges;
	std::vector< std::vector<int> > neighbors;
	std::vector<bool> isUsed;
	std::vector<int> distance;
	std::priority_queue< std::pair<int, int> > distance_heap;
	
	scanf("%d", &n);
	
	edges.resize(n);
	isUsed.resize(n);
	neighbors.resize(n);
	distance.resize(n);
	
	for(int i=0;i<n;i++){
		scanf("%d %d", &next_node, &num_neighbors);
		edges[next_node].resize(num_neighbors);
		neighbors[next_node].resize(num_neighbors);
		for (int j=0;j<num_neighbors;j++){
			scanf("%d %d", &neighbors[next_node][j], &edges[next_node][j]);
		}
		isUsed[next_node]=-false;
		distance[next_node]=300000;
	}
	isUsed[0] = true;
	count=0;
	distance[0] = 0;
	for(int i=0;i<neighbors[0].size();i++){
		distance[neighbors[0][i]] = edges[0][i];
		distance_heap.push(std::make_pair(distance[neighbors[0][i]*-1], edges[0][i]));
	}

	while(!distance_heap.empty()){
		std::pair<int, int> p = distance_heap.top();
		distance_heap.pop();
		next_node = p.second;
		if (isUsed[next_node]) continue;
		isUsed[next_node] = true;
		for(int i=0; i<neighbors[next_node].size();i++){
			int neighbor = neighbors[next_node][i];
			if (!isUsed[neighbor] && distance[neighbor] > distance[next_node] + edges[next_node][i]){
				distance[neighbor] = distance[next_node] + edges[next_node][i];
				distance_heap.push(std::make_pair(distance[neighbor]*-1, neighbor));
			}
		}
	}
	for(int i=0;i<n;i++){
		printf("%d %d\n", i, distance[i]);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

typedef pair<int,int> PII;
#define INF 1001001001
#define rep(i,n) for(int i=0; i<n; ++i)
#define mp make_pair

struct edge{
    short to;
    short cost;
    edge(short to,short cost):to(to),cost(cost){}
};

int V;
vector<edge> G[10000];
int d[10000];

void dijkstra(int s){
    priority_queue<PII, vector<PII>, greater<PII>> Q;
    fill(d,d+V,INF);
    d[s]=0;
    Q.push(mp(0,s));

    while(!Q.empty()){
        PII p=Q.top();Q.pop();
        int v=p.second;
        if(d[v]<p.first) continue;
        rep(i,G[v].size()){
            edge e=G[v][i];
            if(d[e.to]>d[v]+e.cost){
                d[e.to]=d[v]+e.cost;
                Q.push(mp(d[e.to],e.to));
            }
        }
    }
}
int n;
int main(){
    cin>>V;
    rep(i,V){
        rep(j,V){
            int u,k;
            cin>>u>>k;
            rep(t,k){
                int v, c;
                cin>>v>>c;
                G[u].push_back(edge(v,c));
            }
        }
    }
    dijkstra(0);
    rep(i,V){
        cout<<i<<" "<<d[i]<<endl;
    }
    return 0;
}

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

template<typename T> struct Graph {
  struct Edge { int to; T cost; };
  vector<int> prev;
  vector<vector<Edge>> g;
  Graph(int n) : prev(n, -1), g(n) {}

  void addEdge(int u, int v, T w) {
    g[u].push_back({v, w});
    //g[v].push_back({u, w});
  }
  vector<T> dijkstra(int s) {
    vector<T> dist(g.size(), -1);
    using Node = pair<T, int>;
    priority_queue<Node, vector<Node>, greater<Node>> pq;
    pq.push({dist[s] = 0, s});

    while (!pq.empty()) {
      auto d = pq.top().first;
      auto u = pq.top().second;
      pq.pop();
      if (dist[u] < d) continue;
      for (auto&& v : g[u]) {
        if (dist[v.to] < 0 || dist[v.to] > dist[u] + v.cost) {
          dist[v.to] = dist[u] + v.cost;
          prev[v.to] = u;
          pq.push({dist[v.to], v.to});
        }
      }
    }
    return dist;
  }
  vector<int> getPath(int t) {
    vector<int> path;
    for (; t != -1; t = prev[t]) path.push_back(t);
    reverse(begin(path), end(path));
    return path;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n; cin >> n;
  Graph<int> g(n);
  for (int i = 0; i < n; ++i) {
    int u, k; cin >> u >> k;
    for (int j = 0; j < k; ++j) {
      int v, c; cin >> v >> c;
      g.addEdge(u, v, c);
    }
  }
  vector<int> d = g.dijkstra(0);
  for (int i = 0; i < n; ++i) {
    cout << i << ' ' << d[i] << endl;
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <list>
#include <vector>

class TNode
{
	std::list<std::pair<TNode*, int> > FNexts;
	int  FDistance;
public:
	TNode()
	: FDistance(-1)
	{
	}
	
	int GetDistance()const {return FDistance;}
	void SetDistance(int Distance){FDistance = Distance;}
	
	void Push(TNode* New, int Weight)
	{
		FNexts.push_back(std::make_pair(New, Weight));
	}
	
	void Visit()
	{
		for(std::list<std::pair<TNode*, int> >::iterator It = FNexts.begin(); It != FNexts.end(); ++It){
			int NextDistance = It->first->GetDistance();
			if(NextDistance >= 0 && NextDistance < FDistance + It->second) continue;
			It->first->SetDistance(FDistance + It->second);
		}
	}
};

class TGraph
{
	std::vector<TNode*> FNodes;
	std::list<TNode*> FSearchList;
	
	std::list<TNode*>::iterator FindMinimum()
	{
		std::list<TNode*>::iterator Result = FSearchList.begin();
		int Distance = (*Result)->GetDistance();
		for(std::list<TNode*>::iterator It = FSearchList.begin(); It != FSearchList.end(); ++It){
			int TempDistance = (*It)->GetDistance();
			if(Distance < 0 || TempDistance >= 0 && TempDistance < Distance){
				Result = It;
				Distance = TempDistance;
			}
		}
		return Result;
	}
public:
	TGraph(int Count)
	: FNodes(Count)
	{
		for(int i = 0; i < Count; ++i){
			FNodes[i] = new TNode();
			FSearchList.push_back(FNodes[i]);
		}
		FNodes[0]->SetDistance(0);
	}
	
	~TGraph()
	{
		for(unsigned int i = 0; i < FNodes.size(); ++i){
			delete FNodes[i];
		}
	}
	
	void InputNode()
	{
		int Index;
		std::cin >> Index;
		int BranchCount;
		std::cin >> BranchCount;
		for(int i = 0; i < BranchCount; ++i){
			int Target, Weight;
			std::cin >> Target >> Weight;
			FNodes[Index]->Push(FNodes[Target], Weight);
		}
	}
	
	void DijkstraSearch()
	{
		while(!FSearchList.empty()){
			std::list<TNode*>::iterator It = FindMinimum();
			TNode* Minimum = *It;
			FSearchList.erase(It);
			Minimum->Visit();
		}
	}
	
	int Distance(int Target)const
	{
		return FNodes[Target]->GetDistance();
	}
};

int main()
{
	int Count;
	std::cin >> Count;
	TGraph Graph(Count);
	for(int i = 0; i < Count; ++i){
		Graph.InputNode();
	}
	Graph.DijkstraSearch();
	
	for(int i = 0; i < Count; ++i){
		std::cout << i << " " << Graph.Distance(i) << std::endl;
	}
	return 0;
}