Go: package main

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

type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }
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 = i
	pq[j].index = j
}
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
}

type Q struct {
	pq *PriorityQueue
	ds map[int]int // It has always same value as pq. For random access.
}

func newQ() *Q {
	pq := new(PriorityQueue)
	heap.Init(pq)
	return &Q{
		pq: pq,
		ds: make(map[int]int),
	}
}
func (q *Q) pop() (u, d int) {
	item := heap.Pop(q.pq).(*Item)
	delete(q.ds, item.value)
	return item.value, item.priority
}
func (q *Q) get(u int) (int, bool) { // returns d
	d, ok := q.ds[u]
	return d, ok
}
func (q *Q) set(u, d int) {
	q.ds[u] = d
	for _, item := range *q.pq {
		if item.value != u {
			continue
		}

		item.priority = d
		heap.Fix(q.pq, item.index)
		return
	}
	heap.Push(q.pq, &Item{
		priority: d,
		value:    u,
	})
}
func (q *Q) len() int {
	return len(q.ds)
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := newQ()
	q.set(0, 0)
	visited := make(map[int]struct{})
	out := make([]int, n)

	for q.len() > 0 {
		u, d := q.pop()
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			dd := d + c
			if d, ok := q.get(uu); !ok || d > dd {
				q.set(uu, dd)
			}
		}

		out[u] = d
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, d)
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

const int INF = 1001001001;

int main(){
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> gr(n, vector<pair<int, int>>());
  rep(i, n){
    int a;
    cin >> a;
    int k;
    cin >> k;
    rep(j, k){
      int v, c;
      cin >> v >> c;
      gr.at(i).push_back(make_pair(v, c));
    }
  }
  vector<int> d(n, INF);
  vector<bool> chk(n, false);
  d.at(0) = 0;
  chk.at(0) = true;
  int now = 0;
  rep(i, n-1){
    for(auto p : gr.at(now)){
      if(chk.at(p.first) == true) continue;
      d.at(p.first) = min(d.at(p.first), d.at(now) + p.second);
    }
    int mn = INF, nx = -1;
    rep(j, n){
      if(chk.at(j) == true) continue;
      if(mn > d.at(j)){
        mn = d.at(j);
        nx = j;
      }
    }
    if(nx == -1) break;
    chk.at(nx) = true;
    now = nx;
  }

  rep(i, n){
    cout << i << " " << d.at(i) << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX_V 100
#define INF (1 << 21)

struct edge { int to, cost; };
typedef pair<int, int> P;

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

// int cost[MAX_V][MAX_V];
// bool used[MAX_V];

int s = 0; // start

void dijkstra()
{
    priority_queue<P, vector<P>, greater<P> > que;
    for (int i = 0; i < V; i++) {
        d[i] = INF;
        // used[i] = false;
    }
    d[s] = 0;
    que.push(P(0, s));

    for (; !que.empty();) {
        P p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }

    for (int u = 0; u < V; u++) {
        cout << u << " " << (d[u] == INF ? -1 : d[u]) << endl;
    }

}
int main()
{
    cin >> V;

    // for (int i = 0; i < V; i++) {
    //     for (int j = 0; j < V; j++) {
    //         cost[i][j] = INF;
    //     }
    // }

    for (int i = 0; i < V; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v, c;
            cin >> v >> c;
            G[u].push_back({v, c});
            // cost[u][v] = c;
        }
    }

    dijkstra();

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxx=110;
const int infinity=(1<<21);

int A[maxx][maxx];
int d[maxx];
bool flag[maxx];

int main (){
	int n,a,b,c,minv,mini;
	cin>>n;
	for(int i=0;i<maxx;i++){
		flag[i]=false;
		d[i]=infinity;
		for(int j=0;j<maxx;j++){
			A[i][j]=infinity;
		}
	}
	for(int i=0;i<n;i++){
		cin>>a>>b;
		for(int j=0;j<b;j++){
			cin>>c>>A[a][c];
		}
	}
	
	d[0]=0;
	while(1){
		minv=infinity,mini=-1;
		for(int i=0;i<n;i++){
			if(flag[i]==false && d[i]<minv){
				minv=d[i];
				mini=i;
			}
		}
		if(mini==-1 || minv==infinity) break;
		flag[mini]=true;
		for(int i =0;i<n;i++){
			if(A[mini][i]!=infinity && flag[i]==false && d[mini]+A[mini][i]<d[i]){
					d[i]=d[mini]+A[mini][i];
			}
		}
		
	}
	cout<<n<<endl;
	int nn=n;
	for(int i=0;i<nn;i++) cout<<i<<" "<<d[i]<<endl;
	
	return 0;

}

C++: #include <iostream>

using namespace std;

int n;
int a[124][124] = {{}};
static const int INFTY = 1000000;

void dijkstra(){
  int flag[124] = {};
  int d[124] = {};
  int p[124] = {};
  int minv;
  int u;

  for (int i = 0;i < n;i++){
    d[i] = INFTY;
    p[i] = -1;
  }
  d[0] = 0;
  while(1){
    minv = INFTY;
    u = -1;

    for(int i = 0;i < n;i++){
      if(flag[i] == 0 && minv > d[i]){
        minv = d[i];
        u = i;
      }
    }
  //  cout << "u = " << u << endl;
  //  cout << "minv = " << minv << endl;
    if(u == -1) break;
    flag[u] = 1;

    for(int i = 0;i < n;i++){
      if(d[i] > d[u] + a[u][i] && a[u][i] != INFTY && flag[i] == 0){
        d[i] = d[u] + a[u][i];
        p[i] = u;
      }
    }
  }

  for(int i = 0;i < n;i++){
    if(d[i] != -1){
      cout << i <<  " " << d[i] << endl;
    }
    else cout << i << " " << -1 << endl;
  }
}



int main(){

  cin >> n;

  for (int i = 0 ;i < n;i++){
    for(int j = 0;j < n;j++){
      a[i][j] = INFTY;
    }
  }


  int u;
  int k;
  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for (int j = 0;j < k;j++){
      int v;
      cin >> v;
      cin >> a[u][v];
    }
  }
  dijkstra();
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 200001 
using namespace std;
int n;
int A[1000][1000];
int D[1000][1000];
 
void Init(int f){
  for(int i=0;i<f;i++){
    for(int j=0;j<f;j++)D[i][j] = INF;
  } 
}
 
int main(){
  scanf("%d",&n);
  int u,k,C,V;
  Init(n);
  
  for(int i=0;i<n;i++){
      scanf("%d %d",&u,&k);    
      for(int j=0;j<k;j++){
    scanf("%d %d",&V,&C);
    D[u][V] = C;
   
      }    
  }
  for(int i=0;i<n;i++)D[i][i] = 0;

  for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j]= min(D[i][j],D[i][k] + D[k][j]);

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