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++: //Single Source Shortest Path I
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

static const int N = 100;
static const int WHITE = 0;
static const int BLACK = 1;
//int colors[N], d[N];
//
//void dijkstra(vector<vector<int>> &W, int n) {
//    int u, mind;
//    for(int i = 0; i < n; i++) {
//        colors[i] = WHITE;
//        d[i] = INT_MAX;
//    }
//    d[0] = 0;
//    while(1) {
//        u = -1;
//        mind = INT_MAX;
//        for(int i = 0; i < n; i++) {
//            if(colors[i] == WHITE && d[i] < mind) {
//                u = i;
//                mind = d[i];
//            }
//        }
//        if(u == -1) break;
//        colors[u] = BLACK;
//        for(int i = 0; i < n; i++) {
//            if(W[u][i] != -1) {
//                d[i] = min(d[i], mind + W[u][i]);
//            }
//        }
//    }
//}
int d[N];

void dijkstra(vector<vector<int>> &W, int n) {
    int colors[n];
    for(int i=0; i<n; i++) {
        colors[i] = WHITE;
        d[i] = INT_MAX;
    }
    
    d[0] = 0;

    while(1) {
        int u = -1;
        int min_distance = INT_MAX;
        for(int i=0; i<n; i++) {
            if(colors[i] == BLACK) continue;
            if(d[i] < min_distance) {
                u = i;
                min_distance = d[i];
            }
        }
        if(u == -1) break;

        colors[u] = BLACK;
        for(int i=0; i<n; i++) {
            if(colors[i] == BLACK) continue;
            if(W[u][i] == -1) continue;
            d[i] = min(d[i], d[u] + W[u][i]);
        }
    }
}

int main() {
    int n;
    int u, k, v, c;
    cin >> n;
    vector<vector<int>> W(n, vector<int>(n));
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            W[i][j] = -1;
    for(int i = 0; i < n; i++) {
        cin >> u >> k;
        for(int j = 0; j < k; j++) {
            cin >> v >> c;
            W[u][v] = c;
        }
    }

    dijkstra(W, n);

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

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 (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;
        }
    }
    
    dijkstra();
    
    return 0;
}

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

typedef struct NODE {
    int id;
    int degree;
    int min_cost;
    bool done;
    vector<int> adj_n;
    vector<int> cost_to_n;
} node;

node **nodes;
int n;

node *gen_node(int id, int degree, vector<int> adj_n, vector<int> cost_to_n) {
    node *p = (node *)malloc(sizeof(node));
    p->id = id;
    p->degree = degree;
    if (id != 0) {
        p->min_cost = 100010;
    } else {
        p->min_cost = 0;
    }

    p->done = false;
    p->adj_n = adj_n;
    p->cost_to_n = cost_to_n;
    return p;
}

void solve() {
    for (int i = 0; i < n; i++) {
        node *src = (node *)malloc(sizeof(node));
        src = nodes[i];
        for (int j = 0; j < src->degree; j++) {
            node *dst = (node *)malloc(sizeof(node));
            dst = nodes[src->adj_n[j]];

            // 最小コストの更新
            //cout << "src: " << src->id << " dst: " << dst->id;
            //cout << " dst's min cost: " << dst->min_cost << " src's cost to dst: " << src->min_cost + src->cost_to_n[j] << endl;
            dst->min_cost = min(dst->min_cost, src->min_cost + src->cost_to_n[j]);

            // // dstの隣接nodeからsrcを消す処理
            // auto itr = find(dst->adj_n.begin(), dst->adj_n.end(), src->id);
            // int erase_index = itr - dst->adj_n.begin();
            // dst->degree--;
            // dst->adj_n.erase(dst->adj_n.begin() + erase_index);
            // dst->cost_to_n.erase(dst->cost_to_n.begin() + erase_index);
        }
    }
}

void print_node() {
    for (int i = 0; i < n; i++) {
        cout << nodes[i]->id << " " << nodes[i]->min_cost << endl;
    }
}

int main() {
    int u, k;
    cin >> n;

    nodes = (node **)malloc(sizeof(node) * n);

    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        vector<int> adj_n;
        vector<int> cost_to_n;

        int node_tmp;
        int cost_tmp;
        for (int j = 0; j < k; j++) {
            cin >> node_tmp >> cost_tmp;
            adj_n.push_back(node_tmp);
            cost_to_n.push_back(cost_tmp);
        }
        node *p = gen_node(u, k, adj_n, cost_to_n);
        nodes[u] = p;
    }

    solve();
    solve();

    print_node();

    return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
struct edge {int from, to, cost;};
const long long INF = 1000000000;
void bellman_ford(std::vector<edge> & es, std::vector<int> & d, int E, int V, int s) {
  for(int i=0;i<V;i++) d[i] = INF;
  d[s] = 0;
  while(true) {
    bool update = false;
    for(int i=0;i<E;i++) {
      edge e = es[i];
      if(d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if(!update) break;
  }
}
 
int main() {
  int n;
  cin >> n;
  vector<edge> e;
  for(int i=0;i<n;i++) {
    int u, k;
    cin >> u >> k;
    for(int j=0;j<k;j++) {
      int vi, ci;
      cin >> vi >> ci;
      edge ei = {u, vi, ci};
      e.push_back(ei);
    }
  }
  vector<int> d(n);
  bellman_ford(e, d, e.size(), n, 0);
  for(int i=0;i<n;i++) {
    cout << i << " " << d[i] << endl;
  }

}