Go: package main

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

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 := make(map[int]int)
	q[0] = 0
	visited := make(map[int]struct{})
	out := make([]int, n)

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

		out[u] = q[u]
		delete(q, u)
		visited[u] = struct{}{}
	}

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

func top(q map[int]int) int {
	minu := -1
	min := -1
	for u, d := range q {
		if min < 0 || d < min {
			minu = u
			min = d
		}
	}

	return minu
}

C++: #include <iostream>
using namespace std;

static const int MAX = 100;
static const int INFTY = (1<<21);

int n, M[MAX][MAX];
int d[MAX];
int color[MAX];

void dijkstra(){
  int minv;

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

  d[0] = 0;
  color[0] = 1;
  while(1){
    minv = INFTY;
    int u = -1;
    for(int i = 0; i < n; i++){
      if(minv > d[i] && color[i] != 2){
        u = i;
        minv = d[i];
      }
    }

    if(u == -1){
      break;
    }
    color[u] = 2;

    for(int v = 0; v < n; v++){
      if(M[u][v] != INFTY && color[v] != 2){
        if(d[u] + M[u][v] < d[v]){
          d[v] = d[u] + M[u][v];
          color[v] = 1;
        }
      } 
    }
  }
}

int main(){
  cin >> n;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      M[i][j] = INFTY;
    }
  }

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

  dijkstra();
  for(int i  = 0; i < n; i++){
    cout << i << " " << d[i] << endl;
  }

  return 0;
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define MOD 1000000007

typedef long long int ll;

//const ll INF=(ll)1e18;
const int INF=(1<<21);

int main(){
  int n;
  cin >> n;

  vector<pair<int, int>> e[n];
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> pq;

  int d[n];
  bool visited[n];

  REP(i,n)d[i]=INF;
  REP(i,n)visited[i]=false;

  REP(i,n){
    int u,k;
    cin >> u >> k;

    REP(j,k){
      int v,c;
      cin >> v >> c;;
      e[i].push_back(make_pair(v,c));
    }
  }

  d[0]=0;
  visited[0]=true;

  pq.push(make_pair(0,0));
  while(!pq.empty()){
    int c=pq.top().first;
    int u=pq.top().second;
    pq.pop();

    visited[u] = true;
    if(d[u] < c)continue;

    REP(i,e[u].size()){
      int v = e[u][i].first;
      if(visited[v])continue;

      if(d[u] + e[u][i].second < d[v]){
        d[v] = d[u] + e[u][i].second;
        pq.push(make_pair(d[v], v));
      }
    }

  }

  REP(i,n){
    cout << i << " " << d[i] << endl;
  }


}

C++: #include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
typedef pair<int,int> P;
typedef pair<P,P> PP;
int main(void)
{
	int i,j,k,u,n,flg[10001],v,c,sum,ky[10001],t,min,g;
	PP ke[10001];
	scanf("%d",&n);
	sum=0;
	for(i=0;i<n;i++) ky[i]=M,flg[i]=0;
	ky[0]=0;
	for(i=0;i<n;i++){
		scanf("%d%d",&u,&k);
		for(j=sum;j<k+sum;j++){
			scanf("%d%d",&v,&c);
			ke[j].ff=u , ke[j].fs=v , ke[j].sf=c;
		}
		sum+=k;
	}
	sort(ke,ke+sum);
	//for(i=0;i<sum;i++) printf("ke[%d].ff=%d ke[%d].fs=%d ke[%d].sf=%d\n",i,ke[i].ff,i,ke[i].fs,i,ke[i].sf);
	for(i=0;i<sum;i++){
		min=M;
		for(j=0;j<sum;j++) if(flg[j]==0&&min>ky[j]) g=j,min=j;
		flg[g]=1;
		if(min==M) break;
		for(j=0;j<sum;j++){
			t=ky[ke[j].ff]+ke[j].sf;
			if(ky[ke[j].fs]>t) g=j,ky[ke[j].fs]=t;
		}
	}
	for(i=0;i<n;i++) printf("%d %d\n",i,ky[i]);
	return 0;
}