Go: package main

import (
	"strconv"
	"fmt"
	"bufio"
	"os"
)
const INF = 1 << 21
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}


func main() {
	scanner.Split(bufio.ScanWords)
	n := nextInt()
	adj := make([][]int, n)
	for i := range adj {
		adj[i] = make([]int, n)
	}

	for i := 0;i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			v := nextInt()
			c := nextInt()
			adj[u][v] = c
		}
	}

	// for i := range adj {
	// 	fmt.Println(adj[i])
	// }

	dist := make([]int, n)
	prev := make([]int, n)
	visited := make([]bool, n)
	dijkstra := func(start int, size int) {
		for i := 0; i < size; i++ {
			dist[i] = INF
			prev[i] = -1
			visited[i] = false
		}
		dist[start] = 0
		for {
			u := -1
			tmp_min := INF
			for i := 0; i < size; i++ {
				if visited[i] == false && dist[i] < tmp_min {
					tmp_min = dist[i]
					u = i
				}
			}
			if u == -1 {
				break
			}
			visited[u] = true
			for i := 0; i < size; i++ {
				if adj[u][i] > 0 {
					w := adj[u][i]
					newlen := dist[u] + w
					if newlen < dist[i] {
						dist[i] = newlen
						prev[i] = u
					}
				}
			}
		}
	}
	dijkstra(0, n)
	for i, cost := range dist {
		fmt.Printf("%d %d\n", i, cost)
	}
}

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

#define INF 1000000

int n;
int cost[110][110], d[110];
bool used[110];

void dijkstra(int s){

  //initialization
  for(int i = 0;i < n;i++) d[i] = INF;
  for(int i = 0;i < n;i++) used[i] = false;

  //distance of starting position is 0
  d[s] = 0;
  
  while(true){
    int v = -1;
    
    for(int u = 0;u < n;u++){
      if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
    }
    
    if(v == -1) break; //finish
    used[v] = true; //use v

    //update distance
    for(int u = 0;u < n;u++){
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}

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

  //input and initialization
  cin >> n;
  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      cost[i][j] = INF;
    }
  }
  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      cin >> v >> c;
      cost[u][v] = min(cost[u][v], c);
    }
  }
  
  dijkstra(0);

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

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

static const int MAX = 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[MAX][MAX];

void dijkstra() {
  int minv;
  int d[MAX], color[MAX];
  for (int i = 0; i < n; i ++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }

  d[0] = 0;
  color[0] = GRAY;
  while (true) {
    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;
    }
  }

  dijkstra();

  return 0;
}

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

int n;
long d[MAX], C[MAX][MAX];
vector<int> M[MAX];

struct Edge{
    int u;
    Edge(int u): u(u){}
    bool operator < (const Edge &e) const {
        if(d[u] != d[e.u]) return d[u] < d[e.u];
        else return u < e.u;
    }
};

void dijkstra(){
    bool seen[n], todo[n];
    list<Edge> E;
    fill(seen, seen + n, false);
    fill(todo, todo + n, false);
    seen[0] = true;
    E.push_front(Edge(0));
    
    while(!E.empty()){
        E.sort();
        int u = E.begin()->u;
        seen[u] = true;
        E.pop_front();
        vector<int>::iterator it;
        
        for(it = M[u].begin(); it != M[u].end(); it++){
            int v = *it;
            if(!seen[v]) d[v] = min(d[v], d[u] + C[u][v]);
            if(!seen[v] && !todo[v]){
                E.push_back(Edge(v));
                todo[v] = true;
            }
        }
    }
    return;
}

int main(){
    cin >> n;
    fill(d, d + n, LONG_MAX);
    d[0] = 0;
    for(int i = 0; i < n; i++){
        fill(C[i], C[i] + n, LONG_MAX);
        int u, q, v, cost;
        cin >> u >> q;
        for(int j = 0; j < q; j++){
            cin >> v >> cost;
            C[u][v] = cost;
            M[u].push_back(v);
        }
    }
    dijkstra();
    for(int i = 0; i < n; i++)
        cout << i << " " << d[i] << endl;
    return 0;
}

C++: #include<iostream>
#define INF 1e9
using namespace std;

int n,mp[101][101],D[101];
void dijkstra(){
  bool used[101]={};
  for(int i=0;i<101;i++)D[i]=INF;
  D[0]=0;
 
  while(1){
    int cost=INF,pos=-1;
    for(int i=0;i<n;i++) if(!used[i]&&D[i]<cost) pos=i,cost=D[i];//もし使用していなくて格納されているものよりコストが大きれればpositionをずらし、コストに今入っているDを入れる
    if(pos==-1)return ;
    used[pos]=1;//positionをずらしたら見たことになるので１に書き換える    
    for(int i=0;i<n;i++)D[i]=min(D[i],cost+mp[pos][i]);
  }
}

int main(){
     cin>>n;
  for(int i=0;i<101;i++)
    for(int j=0;j<101;j++) mp[i][j]=INF;
   
  for(int i=0,a,m;i<n;i++){
    cin>>a>>m;//aが頂点mが次数
    for(int j=0,b,c;j<m;j++)cin>>b>>c,mp[a][b]=c;
  }
  dijkstra();
  for(int i=0;i<n;i++)cout <<i<<" "<<D[i]<<endl;
   
  return 0;
}

C++: #include<iostream>
using namespace std;
#define INF 100000
int m[100][100]; //adjacency matrix
int n; //vertexes
void dijkstra();
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            m[i][j] = INF;
        }
    }
    for (int i = 0; i < n; i++) {
        int u; //number
        int k; //output orders
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v; //vertex
            int c; //weight
            cin >> v >> c;
            m[u][v] = c;
        }
    }
    dijkstra();
    return 0;
}
void dijkstra() {
    int d[100]; //minimum cost
    int p[100]; //top parent
    d[0] = 0;
    p[0] = 1;
    for (int i = 1; i < n; i++) {
        p[i] = 0;
        d[i] = INF;
    }
    while (1) {
        int minv = INF;
        int u = -1;
        for (int i = 0; i < n; i++) {
            if (minv > d[i] && p[i] != 2) { //firstly only d[0]
                u = i; //tmp parent
                minv = d[i]; //min cost of 0 to i
            }
        }
        if (u == -1) {
            break;
        }
        p[u] = 2; //決まったら
        for (int v = 0; v < n; v++) {
            if (m[u][v] != INF && p[v] != 2) { //u and v are connected
                if (d[v] > d[u] + m[u][v]) {
                    d[v] = d[u] + m[u][v]; //get smaller cost
                    //ここでd[v]がINFから解放され、次の親になれる
                    p[v] = 1;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout << i << " ";
        if (d[i] == INF) {
            cout << -1 << endl;
        } else {
            cout << d[i] << endl;
        }
    }
}