Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [][]int

func (g *Graph) dikstra(n int) string {
	var (
		u, minv = 0, 0
	)

	d := make([]int, n)
	color := make([]int, n)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		color[i] = WHITE
	}
	d[0] = 0
	color[0] = GRAY

	for {
		minv = INFTY
		u = -1
		for 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 v := 0; v < n; v++ {
			if color[v] != BLACK && (*g)[u][v] != INFTY {
				if d[v] > d[u]+(*g)[u][v] {
					d[v] = d[u] + (*g)[u][v]
					color[v] = GRAY
				}
			}
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		out.WriteString(fmt.Sprintf("%d ", i))
		if d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d\n", -1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d\n", d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make(Graph, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			g[i][j] = INFTY
		}
	}

	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())

		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())

			g[u][v] = c
		}
	}

	return g.dikstra(n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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 init() {
    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;
        }
    }
}

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;
    }
}

void solve() {
    init();
    dijkstra();
}

int main() {
    // freopen("infile", "r", stdin);
    // freopen("outfile", "w", stdout);
    solve();
    return 0;
}

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

#define INF 100000000
#define WH 0
#define GR 1
#define BK 2

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

    int color[100] = {};

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

    int p[100];
    for (int i = 0; i < n; i++) {
        p[i] = -1;
    }

    while (true) {
        d[0] = 0;

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

        if (min_cost == INF) {
            break;
        }

        color[u] = BK;

        for (int v = 0; v < n; v++) {
            if (color[v] != BK && M[u][v] < INF) {
                if (d[u] + M[u][v] < d[v]) {
                    d[v] = d[u] + M[u][v];
                    p[v] = u;
                    color[v] = GR;
                }
            }
        }
    }

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


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

    return 0;
}

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

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

struct edge {int to, cost;};

/*
[input]
const vector<vector<edge>> &graph : ??\?????°??????
int s : ?§????
int g : ??????
n*/
int dijkstra(const vector<vector<edge>> &graph, int s, int g ) {
  // ????????????????±?????????????????????????
  vector<int> d(graph.size(), INF);
  // ????§?????????????????????????0????????????
  d[s] = 0;
  // ??????????????????now
  set<P> now;
  // ????§?????????????now????????\
  now.insert(P(0, s));
  
  // now????????????????????§
  while(!now.empty()) {
    // ?????¨?????????????????????pos???????´?
    int pos = now.begin()->second;
    // ?????¨??????????????????????????????????±???????????????????????????????
    if (pos == g) {
      for(int i = 0; i <= g; i++) {
        cout << i << " " << d[i] << endl;
      }
      return INF;
    }
    // now????????¢?´¢?????????????¶????
    now.erase(now.begin());
    // ?????¨??????????????¨???????????¢?´¢
    for(auto ed: graph[pos]) {
      // ??\?¶??????????????????????????????????????????¨??????????????§????????????
      // ??¨??\?¶?????????§?????¨????????????????????????????????§???????????°
      if(d[ed.to] > d[pos] + ed.cost) {
        // now????????\?¶?????????????????±????????????????????????????
        now.erase(P(d[ed.to], ed.to));
        d[ed.to] = d[pos] + ed.cost;
        now.insert(P(d[ed.to], ed.to));
      }
    }
  }
  return INF;
}
  
signed 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;
	if(k == 0) continue;
    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;
}