Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Node struct {
	cost int
	done bool
}

type Edge struct {
	to, cost int
}

const INF = 1 << 60

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	nodes := make([]Node, N)
	for i := 0; i < N; i++ {
		nodes[i].cost = INF
	}
	E := make([][]Edge, N)
	for i := 0; i < N; i++ {
		id, k := sc.nextInt(), sc.nextInt()
		for j := 0; j < k; j++ {
			to, cost := sc.nextInt(), sc.nextInt()
			E[id] = append(E[id], Edge{to, cost})
		}
	}
	nodes[0].cost = 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			n := nodes[i]
			if !n.done && n.cost < minCost {
				cur = i
				minCost = n.cost
			}
		}
		if minCost == INF {
			break
		}
		nodes[cur].done = true
		for i := 0; i < len(E[cur]); i++ {
			to, cost := E[cur][i].to, E[cur][i].cost+nodes[cur].cost
			if !nodes[to].done && cost < nodes[to].cost {
				nodes[to].cost = cost
			}
		}
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i, nodes[i].cost)
	}
	wtr.Flush()
}

C++: #include<iostream>
using namespace std;
#define MAX 100
#define INF 1 << 30

int n, A[MAX][MAX], d[MAX];
bool vis[MAX];

void dijkstra(int s){
  for ( int i = 0; i < n; i++){
    d[i] = A[s][i];
    vis[i] = false;
  }
  d[s] = 0;
  vis[s] = true;

  while ( 1 ){
    int minCost = INF;
    for ( int i = 0; i < n; i++){
      if ( !vis[i] && minCost > d[i]){
        minCost = d[i];
        s = i;
      }
    }

    if ( minCost == INF ) break;
    vis[s] = true;

    for ( int i = 0; i < n; i++){
      if ( !vis[i] && d[i] > d[s] + A[s][i]){
        d[i] = d[s] + A[s][i];
      }
    }
  }
}

int main(){
  cin >> n;

  // init
  for ( int i = 0; i < n; i++){
    for ( int j = 0; j < n; j++ ){
      A[i][j] = INF;
    }
  }


  int u, k, v, c;
  for ( int i = 0; i < n; i++){
    cin >> u >> k;
    for ( int j = 0; j < k; j++){
      cin >> v >> c;
      A[u][v] = (c == -1) ? INF : c;
    }
  }

  dijkstra(0);

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

  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
using namespace std;

typedef vector<int>::iterator vitr;
typedef map<int, int>::iterator mitr;

map<int, int> w[100];
int d[100];

vector<int> a;
vector<int> b;

void findPath(){
    int candiW = INT_MAX;
    vitr candiI;
    for(vitr i=a.begin();i!=a.end();i++){
        for(vitr j=b.begin();j!=b.end();j++){
            mitr k = w[*i].find(*j);
            if(k == w[*i].end())continue;
            if(k->second + d[*i] < candiW){
                candiW = k->second + d[*i];
                candiI = j;
            }
        }
    }
    a.push_back(*candiI);
    d[*candiI] = candiW;
    b.erase(candiI);
}

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

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

    d[0] = 0;
    a.push_back(0);
    for(i=1;i<n;i++)b.push_back(i);

    for(i=1;i<n;i++)findPath();

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

    return 0;
}

C++: #include <iostream>
using namespace std;
static const int maxs=100;
static const int infty=(1<<21);
static const int white=0;
static const int gray=1;
static const int black=2;

int n,m[maxs][maxs];

void dicstra(){
  int minv;
  int d[maxs],color[maxs];
  for(int i=0;i<n;i++){
    d[i]=infty;
    color[i]=white;
  }
  d[0]=0;
  color[0]=gray;
  while(1){
    minv=infty;
    int u=-1;
    for(int 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(int v=0;v<n;v++){
      if(color[v]!=black&&m[u][v]!=infty){
	if(d[v]>d[u]+m[u][v]){
	  d[v]=d[u]+m[u][v];
	  color[v]=gray;
	}
      }
    }
  }
  for(int i=0;i<n;i++){
    cout<<i<<" "<<(d[i]==infty?-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]=infty;
    }
  }
  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;
    }
  }
  dicstra();
  return 0;
}

C++: #include<bits/stdc++.h>
#define N 100
#define INFTY (1<<21)
#define WH 0
#define GR 1
#define BL 2
using namespace std;

vector<int> color(N),d(N),p(N);
vector<vector<int>> M(N,vector<int>(N));
int n;

void dijkstra(){
	for(int i=0;i<n;i++){
		color.at(i) = WH;
		d.at(i) = INFTY;
	}
	d.at(0) = 0;
	color.at(0) = GR;
	
	while(1){
		int mincost = INFTY;
		int u = -1;
		for(int i=0;i<n;i++){
			if(color.at(i) != BL && d.at(i) < mincost){
				mincost = d.at(i);
				u = i;
			}
		}
		if(u == -1) break;
		
		color.at(u) = BL;
		
		for(int v=0;v<n;v++){
			if(color.at(v) != BL && M.at(u).at(v) != INFTY){
				if(d.at(u) + M.at(u).at(v) < d.at(v)){
					d.at(v) = d.at(u) + M.at(u).at(v);
					p.at(v) = u;
					color.at(v) = GR;
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		cout << i << " ";
		if(d.at(i) == INFTY) cout << -1 << endl;
		else cout << d.at(i) << endl;
	}
	
}

int main(){
	cin >> n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			M.at(i).at(j) = INFTY;
		}
	}
	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.at(u).at(v) = c;
		}
	}
	dijkstra();
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF (1<<21)
#define white 0
#define gray 1
#define black 2

int n;
int W[100][100];
int color[100],d[100],p[100];

void dijkstra(int s) {
  d[s] = 0;

  while(1) {

    int mini = INF,u;
    for(int i = 0;i < n;i++) {
      if(color[i] != black && mini > d[i]) {
        mini = d[i];
        u = i;
      }
    }

    if(mini == INF) break;
    color[u] = black;

    for(int i = 0;i < n;i++) {
      if(color[i] != black && W[u][i] != INF) {
        if(d[u] + W[u][i] < d[i]) {
          d[i] = d[u] + W[u][i];
        }
      }
    }

  }

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


int main() {

  cin >> n;
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < n;j++) {
      W[i][j] = INF;
    }
  }

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

  dijkstra(0);

}