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<stdio.h>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<queue>
#include<functional>
using namespace std;
int main(){
	int a;
	int b;
	int s;
	int t;
	vector<int> n[10000];
	int nn[10000];
	vector<int> x[10000];
	priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >y;
	vector<pair<int, int> >::iterator yy;
	int z;
	int d[10000];
	int e;
	int c[10000];
	int zz;
	pair<int, int>pai;
	for (int i = 0; i < 10000; i++){
		d[i] = 1000000009;
		c[i] = 0;
	}
	scanf("%d", &a);
	for (int i = 0; i < a; i++){
		scanf("%d %d", &s, &b);
		for (int j = 0; j < b; j++){
			scanf("%d %d", &t, &e);
			n[s].push_back(t);
			nn[s]++;
			x[s].push_back(e);

		}
	}
	d[0] = 0;
	y.push(std::make_pair(d[0], 0));
	for (int i = 0; y.empty() == 0; i++){
		zz = 0;
		for (int j = 0; y.empty() == 0 && zz == 0; j++){
			pai = y.top();
			z = pai.second;
			if (c[z] == 0)zz++;
			else y.pop();
		}
		if (zz == 0)break;
		c[z] = 1;
		for (int j = 0; j < nn[z]; j++){
			if (d[z] + x[z][j] < d[n[z][j]])
			{
				d[n[z][j]] = d[z] + x[z][j];
				y.push(std::make_pair(d[n[z][j]], n[z][j]));
			}
		}
		y.pop();
	}
	for (int i = 0; i<a; i++)printf("%d %d\n",i,d[i]);
		return 0;
}

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;
}