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;

#define INT_MIN     (-2147483647 - 1) /* minimum (signed) int value */
#define INT_MAX       2147483647    /* maximum (signed) int value */
static const int MAX = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, m[MAX][MAX];

int prim()
{
	int color[MAX], p[MAX], d[MAX];
	int u, minv;

	for (int i = 0; i < n; ++i)
	{
		color[i] = WHITE;
		p[i] = -1;
		d[i] = INT_MAX;
	}

	d[0] = 0;

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

		if (-1 == u)
			break;
		color[u] = BLACK;

		for (int i = 0; i < n; ++i)
		{
			if (m[u][i] < d[i] && color[i] != BLACK)
			{
				d[i] = m[u][i];
				p[i] = u;
				color[i] = GRAY;
			}
		}
	}

	int sum = 0;
	for (int i = 0; i < n; ++i)
	{
		if (p[i] != -1)
			sum += m[p[i]][i];
	}
	return sum;
}

void dijkstra()
{
	int minv;
	int d[MAX], color[MAX];

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

	d[0] = 0;
	color[0] = GRAY;

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

		if (-1 == u) break;
		color[u] = BLACK;

		for (int i = 0; i < n; ++i)
		{
			if (color[i] != BLACK && m[u][i] + d[u] < d[i])
			{
				d[i] = d[u] + m[u][i];
				color[i] = GRAY;
			}
		}
	}

	for (int i = 0; i < n; ++i)
		cout << i << " " << ((d[i] == INT_MAX) ? -1 : d[i]) << endl;
}

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

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

	dijkstra();
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int M[200][200];
string color[200];
int d[200];
int p[200];
int n;
const int INF = 1001001001;

void dijkstra(int s){
    rep(i, n){
        color[i] = "WHITE";
        d[i] = INF;
    }
    d[s] = 0;
    p[s] = -1;

    int u;
    while(true){
        int mincost = INF;
        for (int i = 0; i < n; i++){
            if(color[i] != "BLACK" && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }
        if(mincost == INF){
            break;
        }
        color[u] = "BLACK";
        
        for (int v = 0; v < n; v++){
            if(color[v] != "BLACK" && M[u][v] != INF){
                if(d[u] + M[u][v] < d[v]){
                    d[v] = d[u] + M[u][v];
                    p[v] = u;
                    color[v] = "GRAY";
                }
            }
        }
    }
}

int main() {
    cin >> n;
    rep(i, n){
        rep(j, n){
            M[i][j] = INF;
        }
    }
    rep(i, n){
        int u; cin >> u;
        int k; cin >> k;
        rep(j, k){
            int c, v; cin >> c >> v;
            M[u][c] = v;
        }
    }
    dijkstra(0);
    rep(i, n){
        cout << i << " " << d[i] << endl;
    }

    return 0;
}

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

using namespace std;
const int INF = 1001001001;

void djkstra(const vector< vector< int > >& edges, vector< int >& d, int root)
{
    int n = d.size(), node, min_dist;
    d[root] = 0;
    vector<int> color(n, 0);

    while(true)
    {
        min_dist = INF;
        for(int i=0;i<n;++i)
        {
            if(color[i] == 0 && d[i] < min_dist)
            {
                node = i;
                min_dist = d[i];
            }
        }
        if(min_dist == INF) break;
        color[node] = 1;
        for(int i=0;i<n;++i)
        {
            d[i] = min(d[i], edges[node][i] + d[node]);
        }
    }
}

int main()
{
    int n, k, u, v, c;
    cin >> n;
    vector< vector< int > > edges(n, vector<int>(n, INF));
    vector<int> d(n, INF);
    for(int i=0;i<n;++i)
    {
        cin >> u >> k;
        for(int j=0;j<k;++j)
        {
            cin >> v >> c;
            edges[u][v] = c;
        }
    }

    djkstra(edges, d, 0);
    for(int i=0;i<d.size();++i)
    {
        cout << i << " " << d[i] << endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

const int INF = 1000 * 1000 * 10;
const int N = 100;
int d[N][N];
int n;

int main() {
  int i, j, k;
  int u, v, c; 

  scanf("%d ", &n);
  for (i=0; i<n; i++) 
    for (j=0; j<n; j++) {
      if (i==j)
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }

  for (i=0; i<n; i++) {
    scanf("%d%d ", &u, &k);
    for (j=0; j<k; j++) {
      scanf("%d%d ", &v, &c);
      d[u][v] = c;
    }
  }

  //Warshall-Floyd
  for (k=0; k<n; k++)
    for (i=0; i<n; i++)
      for (j=0; j<n; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

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

	return 0;
}

C++: #include<iostream>
using namespace std;
#define S 100
#define INF 1000000
#define WHITE 0;
#define GRAY 1;
#define BLACK 2

int n,a[S][S];

void dijkstra(){
    int minv;
    int d[S],cnd[S];
    int i,v;
    for(i=0;i<n;i++){
        d[i]=INF;
        cnd[i]=WHITE;
    }

    d[0]=0;
    cnd[0]=GRAY;

    while(1){
        minv=INF;
        int u=-1;
        for(i=0;i<n;i++){
            if(minv>d[i]&&cnd[i]!=BLACK){
                u=i;
                minv=d[i];
            }
        }
        if(u==-1)break;
        cnd[u]=BLACK;
        for(int v=0;v<n;v++){
            if(cnd[v]!=BLACK && a[u][v]!=INF){
                if(d[v]>d[u]+a[u][v]){
                    d[v]=d[u]+a[u][v];
                    cnd[v]=GRAY;
                }
            }
        }
    }

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

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

C++: #include<cstdio>
#include<climits>
#include<cstring>
#define INF 1<<25
using namespace std;
typedef long ull;
 
int main(){
  int n;
  int A[1000][1000];
  ull D[1000][1000];
  scanf("%d",&n);
  int u,k,C,V;
  
  for(int i=0;i<=n;i++){
    for(int j=0;j<=n;j++)D[i][j] = INF;
  }
  
  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;
      D[V][u] = 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++){
    cout << i << " " << D[0][i] << endl;
  }
  
  return 0;
}