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 N = 100;
static const int INF = 10000000;
static const int WHITE =0;
static const int GRAY =1;
static const int BLACK =2;

int n,A[N][N]; //隣接行列

void dijkstra(){
    int min ;
    int d[N],color[N];

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

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

int main(){
    cin >> n;
    for(int i=0;i<n;i++){
        for(int j =0;j<n;j++){
            A[i][j] = INF;
        }
    }
    
    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;
            A[u][v] = c;
        }
    }

    dijkstra();

    return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>


#define MAX_E 20
#define MAX_V 7
#define INF 100000

typedef struct edge{
    int from;
    int to;
    int cost;
}edge;


edge es[MAX_E];

int distance[MAX_V];

void bellman_ford(int n, int s, int e) {
    int i, update;
    
    for (int i = 0; i < MAX_V; i++) distance[i] = INF;
    distance[s] = 0;
    while (1) {
        update = 0;
        for (i = 0; i < e; i++) {
            edge e = es[i];
            if (distance[e.from] != INF && distance[e.to] > distance[e.from] + e.cost) {
                distance[e.to] = distance[e.from] + e.cost;
                update = 1;
            }
        }
        if (!update) break;
    }
}


int main( ){
    
    int n, i, j, l, u, v, k, c;
    scanf("%d", &n);
    
    
    l = 0;
    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        
        for (j =0 ; j < k; j++) {
            scanf("%d %d", &v, &c);
            es[l].from = u;
            es[l].to = v;
            es[l].cost = c;
            l++;
        }
    }
    
    bellman_ford(n, 0, l);
    
    for (i=0; i<n; i++) {
        printf("%d %d\n", i, distance[i]);
    }
    
    return 0;
}

C++: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Graph vector< vector<P> >
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;

int main(){
    int N;
    cin >> N;
    Graph M(N);
    rep(i, N){
        int u, k;
        cin >> u >> k;
        rep(j, k){
            int v, c;
            cin >> v >> c;
            M[u].push_back(P(v, c));
        }
    }

    vector<int> d(N, INF), p(N, -1);
    vector<bool> seen(N, false);
    d[0] = 0;

    while(1){
        int mincost = INF;
        int u;

        rep(i, N){
            if(!seen[i] && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }

        if(mincost == INF) break;
        seen[u] = true;

        rep(i, M[u].size()){
            int v = M[u][i].first, c = M[u][i].second;
            if(!seen[v] && d[u] + c < d[v]){
                d[v] = d[u] + c;
                p[v] = u;
            }
        }
    }

    rep(i, N){
        cout << i << ' ' << d[i] << endl;
    }

}

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

#define MAX 100
#define INF 2097152
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n,M[MAX][MAX];


int main(void){

	int i,j;
	int minv,d[MAX],color[MAX];
	int u,v;

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

	int k,l,m,p;
	for(i=0;i<n;i++){
		scanf("%d %d",&m,&k);
		for(j=0;j<k;j++){
			scanf("%d %d",&p,&l);
			M[m][p] = l;
		}
	}

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

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

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

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

return 0;

}

C++: #include <stdio.h>
#define INF (1<<21)

int main(void)
{
 int i,j,n,m,u,k,l,v,c,a[10000],b[10000],d[10000][10000];

 scand("%d",&n);
 
 for(i=0;i<n;i++){
 	scanf("%d %d",&u,&k);
 	a[i]=INF;
 	b[i]=0;
 	for(j=0;j<k;j++){
 		scanf("%d %d",&v,&c);
 		d[u][v]=c;
 	}
 }
 a[0]=0;
 
 while(1){
 	
 	l=-1;
 	m=INF;
 	
 	for(i=0;i<n;i++){
 		if(a[i]<m && b[i]=0){
 			m=a[i];
 			l=i;
 		}
 	}
 	
 	if(l==-1) break;
 	
 	b[l]=1;
 	
 	for(i=0;i<n;i++){
 		if(a[i]>=d[l][i]+a[l]) a[i]=d[l][i]+a[l];
 	}
 }
 
 for(i=0;i<n;i++){
 	if(a[i]==INF) printf("%d -1\n",i);
 	else printf("%d %d\n",i,a[i]);
 }
 
 return 0;
}

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;
    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] = D[j][i] = 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;
}