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 <bits/stdc++.h>
using namespace std;
#define MAX 101
#define INF 100100

static const int WHITE = 0; //not visited
static const int GRAY = 1; //visited
static const int BLACK = 2;

int n, M[MAX][MAX];
int color[MAX];
int dist_from_start[MAX];
int parent_node[MAX];

void dijkstra(int start) {
  //initialize
  for (int i = 1; i <= n; i++) {
    color[i] = WHITE;
    dist_from_start[i] = INF;
    parent_node[i] = -1;
  }

  dist_from_start[start] = 0;
  color[start] = GRAY;

  while (true) {
    int mincost = INF;
    int now = -1;

    //初回はmincost = 0になる(始点)
    for (int i = 1; i <= n; i++) {
      if (color[i] != BLACK && dist_from_start[i] < mincost) {
        mincost = dist_from_start[i];
        now = i;
      }
    }

    if (now == -1) break;

    color[now] = BLACK;

    for (int v = 1; v <= n; v++) {
      if (color[v] != BLACK && M[now][v] != INF) {
        if (dist_from_start[now] + M[now][v] < dist_from_start[v]) {
          dist_from_start[v] = dist_from_start[now] + M[now][v];
          parent_node[v] = now;
          color[v] = GRAY;
        }
      }
    }
  }

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

int main() {
  cin >> n;

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

  int u, k;
  for (int i = 1; i <= n; i++) {
    cin >> u >> k;
    for (int j = 1; j <= k; j++) {
      int v, t;
      cin >> v >> t;
      M[u+1][v+1] = t;
    }
  }

  dijkstra(1);

  return 0;
}

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

vector<vector<pair<int, int>>> v;
vector<int> d, color;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

void dijkstra(){
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
  q.push(make_pair(0, 0));
  d[0] = 0;

  while(!q.empty()){
    auto next = q.top(); q.pop();
    if(color[next.second] == BLACK) continue;

    color[next.second] = BLACK;

    for(auto ve : v[next.second]){
      if(color[ve.first] != BLACK && d[ve.first] > d[next.second] + ve.second){
        d[ve.first] = d[next.second] + ve.second;
        color[ve.first] = GRAY;
        q.push(make_pair(d[ve.first], ve.first));
      }
    }
  }
}

int main(){
  int n; cin >> n;

  v.resize(n);
  d.resize(n);
  color.resize(n);

  for(int i=0; i<n; ++i){
    d[i] = numeric_limits<int>::max();

    int u, k, ve, c; cin >> u >> k;
    for(int l=0; l<k; ++l){
      cin >> ve >> c;
      v[u].push_back(make_pair(ve, c));
    }
  }

  dijkstra();

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

C++: #include <cstdio>
#include <queue>
#include <utility>
using namespace std;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
vector<pair<int, int>> adj[100];
int *d;

void dijkstra() {
    priority_queue<pair<int, int>> PQ;
    int d[n], color[n];

    for (int i = 0; i < n; i++) {
        d[i] = INFTY;
        color[n] = WHITE;
    }
    d[0] = 0;
    PQ.push(make_pair(0, 0));

    while (!PQ.empty()) {
        pair<int, int> f = PQ.top();
        PQ.pop();

        int u = f.second;
        color[u] = BLACK;

        if (d[u] < f.first * (-1)) { continue; }

        for (int j = 0; j < adj[u].size(); j++) {
            int v = adj[u][j].first;
            if (color[v] == BLACK) { continue; }
            if (d[u] + adj[u][j].second < d[v]) {
                d[v] = d[u] + adj[u][j].second;
                // printf("d[%d] = %d\n", v, d[v]);
                PQ.push(make_pair(d[v] * (-1), v));
                color[v] = GRAY;
            }
        }
    }
    
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i, d[i]);
    }
}

int main() {
    scanf("%d", &n);

    // adj = new vector<pair<int, int>>[n];

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

    dijkstra();

    return 0;
}

C++: #include <iostream>
using namespace std;
int n;
static const int MAX = 100;
static const int INF = (1 << 21);
int M[MAX][MAX];
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;


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

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

    d[0] = 0;
    // color[]

    while(1){
        minv = INF;
        int u = -1;

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

        if (u == -1){ //全部探索済み
            break;
        }

        color[u] = BLACK;
        for (int i = 0; i < n; i++){
            if (color[i] != BLACK && d[i] > M[u][i] != INF){
                if (d[i] > d[u] + M[u][i]){
                    color[i] = GRAY;
                    d[i] = d[u] + M[u][i];
                }
            }
        }
    }
    for (int i = 0; i < n; i++){
            cout << i << " ";
            if (d[i] == INF){
                cout << -1 << endl;
            }else{
                cout << d[i] << endl;
            }
            
        }
}

int main(void){
    int tmp;

    cin >> n;

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

    int u, k;
    int v, c;
    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;
}