Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [][]int

func (g *Graph) dikstra(n int) string {
	var (
		u, minv = 0, 0
	)

	d := make([]int, n)
	color := make([]int, n)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		color[i] = WHITE
	}
	d[0] = 0
	color[0] = GRAY

	for {
		minv = INFTY
		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 color[v] != BLACK && (*g)[u][v] != INFTY {
				if d[v] > d[u]+(*g)[u][v] {
					d[v] = d[u] + (*g)[u][v]
					color[v] = GRAY
				}
			}
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		out.WriteString(fmt.Sprintf("%d ", i))
		if d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d\n", -1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d\n", d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make(Graph, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			g[i][j] = INFTY
		}
	}

	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())

		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())

			g[u][v] = c
		}
	}

	return g.dikstra(n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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