Go: package main

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

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := make(map[int]int)
	q[0] = 0
	visited := make(map[int]struct{})
	out := make([]int, n)

	for len(q) > 0 {
		u := top(q)
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			if d, ok := q[uu]; !ok || d > q[u]+c {
				q[uu] = q[u] + c
			}
		}

		out[u] = q[u]
		delete(q, u)
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, d)
	}
}

func top(q map[int]int) int {
	minu := -1
	min := -1
	for u, d := range q {
		if min < 0 || d < min {
			minu = u
			min = d
		}
	}

	return minu
}

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 << " ";
    if(d[i] == INFTY) cout << -1 << endl;
    else cout << 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>
using namespace std;
#define N 100
static const int infinity =(1<<21);
static const int white =0;
static const int gray = 1;
static const int black =2;

int n,M[N][N];

void dijkstra(){
    int min,u;
    int color[N],d[N];
    
    for(int i=0;i<n;i++){
        color[i]=white;
        d[i]=infinity;
    }
    
    d[0]=0;
    
    while(1){
        min=infinity;
        int u=-1;
        for(int i=0;i<n;i++){
            if(color[i]!=black&&d[i]<min){
                min=d[i];
                u=i;
            }
        }
        if(min==infinity)break;
        
        color[u]=black;
        
        for(int v=0;v<n;v++){
            if(color[v]!=black&&M[u][v]!=infinity){
                if(d[u]+M[u][v]<d[v]){
                    d[v]=d[u]+M[u][v];
                    color[v]=gray;
                }
            }
        }
    }
    for(int i=0;i<n;i++)cout<<i<<" "<<d[i]<<endl;
}

int main(){
    cin>>n;
    int u,k,v,c;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)M[i][j]=infinity;
    }
    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();
    
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <string.h>

using namespace std;
static const int INF = 1e9;
typedef pair<int, int> P;
struct edge
{
	int to;
	int cost;
};

int d[101];
vector<edge> G[101];

void solve()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; ++j)
		{
			int to, cost;
			cin >> to >> cost;
			edge e1, e2;
			e1.to = to;
			e1.cost = cost;
			e2.to = u;
			e2.cost = cost;
			G[u].push_back(e1);
			G[to].push_back(e2);
		}
	}
	for (int i = 0; i < 101; ++i)
	{
		d[i] = INF;
	}
	d[0] = 0;
	priority_queue<P, vector<P>, greater<P> > pque;
	pque.push(P(0, 0));
	while (!pque.empty())
	{
		P p = pque.top();
		pque.pop();
		int v = p.second;
		for (int i = 0; i < G[v].size(); ++i)
		{
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost)
			{
				d[e.to] = d[v] + e.cost;
				pque.push(P(d[e.to], e.to));
			}
		}
	}
	for (int i = 0; i < n; ++i)
	{
		cout << i << " " << d[i] << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <bits/stdc++.h>
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 <bits/stdc++.h>
using namespace std;

const int INF = INT_MAX;
typedef pair<int, int> P;

struct edge {int to, cost;};

int dijkstra(const vector<vector<edge>> &graph, int s, int g ) {
  vector<int> d(graph.size(), INF);
  d[s] = 0;
  priority_queue<P, vector<P>, greater<P>> now;
  now.push(P(0, s));
  
  while(!now.empty()) {
    int pos = now.top().second; now.pop();
    if (pos == g) {
      for(int i = 0; i <= g; i++) {
        cout << i << " " << d[i] << endl;
      }
      return INF;
    }
    for(auto ed: graph[pos]) {
      if(d[ed.to] > d[pos] + ed.cost) {
        d[ed.to] = d[pos] + ed.cost;
        now.push(P(d[ed.to], ed.to));
      }
    }
  }
  return INF;
}
  
int main() {
  int num;
  cin >> num;
  vector<vector<edge>> graph(num, vector<edge>());
  int f, n, k, v, c;
  for(int i = 0; i < num; i++) {
    cin >> f >> k;
    for(int j = 0; j < k; j++) {
      cin >> v >> c;
      graph[f].push_back(edge{v, c});
      // graph[v].push_back(edge{f, c});
    }
  }
  dijkstra(graph, 0, num - 1);
  return 0;
}

C++: #include<iostream>

using namespace std;

static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0, GRAY = 1, 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;
	       
  }