Go: package main

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

const (
	white int = iota
	gray
	black
)

type node struct {
	id     int
	parent int
	edge   []int
	weight []int
	color  int
}

type graph struct {
	nodes    []node
	distance []int
}

var infty = 1 << 32
var null = -1

func constructgraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n := nextInt(sc)

	g := &graph{make([]node, n), make([]int, n)}

	for i := 0; i < n; i++ {
		id := nextInt(sc)
		edges := nextInt(sc)
		n := node{}
		n.id = id
		n.edge = make([]int, edges)
		n.weight = make([]int, edges)
		n.color = white
		for j := 0; j < edges; j++ {
			to := nextInt(sc)
			w := nextInt(sc)
			n.edge[j] = to
			n.weight[j] = w
		}
		g.nodes[id] = n
	}
	return g
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func (g *graph) preparedijkstra(s int) {
	for i := 0; i < len(g.nodes); i++ {
		g.nodes[i].color = white
		g.nodes[i].parent = null
		g.distance[i] = infty
	}
	g.distance[s] = 0
}

func (g *graph) dijkstra(s int) []int {
	g.preparedijkstra(s)

	for {
		var u int
		mincost := infty
		for i, d := range g.distance {
			if g.nodes[i].color != black && mincost > d {
				mincost = d
				u = i
			}
		}

		if mincost == infty {
			break
		}

		g.nodes[u].color = black

		for i, e := range g.nodes[u].edge {
			if g.nodes[e].color != black {
				if g.distance[e] > g.distance[u]+g.nodes[u].weight[i] {
					g.distance[e] = g.distance[u] + g.nodes[u].weight[i]
					g.nodes[e].parent = u
					g.nodes[e].color = gray
				}
			}
		}
	}
	return g.distance
}

func printShortestPath(d []int) {
	for i, v := range d {
		fmt.Printf("%d %d\n", i, v)
	}
}

func main() {
	g := constructgraph()
	d := g.dijkstra(0)
	printShortestPath(d)
}

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;
        }
       }
     }
  }
    if(u == -1) break;
    color[u] = BLACK;
   for(int v = 0; v < n; v++){
    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,v,c,u;
 for(int i = 0;i < n; i++){
  cin >> u >>c;
  M[u][v] = c;
  }
 }
  dijkstra();
   return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAXN = 100+7;
const int INF = 1e6+7;
int m[MAXN][MAXN];
int d[MAXN];
bool visit[MAXN];
int n;
set<int> s;
void djas()
{
    d[0] = 0;
    for(int i = 1; i < n; i++)
    {
        d[i] = INF;
    }
    while(s.size() != n)
    {
        int min_d = INF;
        int s1;
        for(int j = 0; j < n; j++)
        {
            if(d[j]<min_d && visit[j]==false)
            {
                min_d = d[j];
                s1 = j;
            }
        }

        s.insert(s1);
        visit[s1] = true;
        for(int j = 1; j < n; j++)
        {
            if(m[s1][j]!=-1 && d[s1]+m[s1][j]<d[j] && j!=s1 && visit[j]==false){
                d[j] = d[s1]+m[s1][j];
            }
        }
    }
}
int main()
{
//    freopen("in.txt", "r", stdin);
    memset(visit, false, sizeof(visit));
    memset(m, -1, sizeof(m));
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        int id;
        scanf("%d", &id);
        int degree;
        scanf("%d", &degree);
        for(int j = 0; j < degree; j++)
        {
            int v;
            scanf("%d", &v);
            int c;
            scanf("%d", &c);
            m[id][v] = c;
        }
    }
    djas();
    for(int i = 0; i < n; i++)
    {
        printf("%d %d\n", i, d[i]);
    }

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100;

int weight[N_MAX][N_MAX];
int dis[N_MAX];

void exhaust_search(const int cur, const int d, const int n, std::array<bool, N_MAX> isvisited) {
    isvisited[cur] = true;
    if (dis[cur] < 0 || d < dis[cur]) { dis[cur] = d; }
    for (int next = 0; next < n; ++next) {
        if (isvisited[next]) { continue; }
        if (weight[cur][next] < 0) { continue; }
        exhaust_search(next, d + weight[cur][next], n, isvisited);
    }
}

void set_shortest_distance(const int n) {
    std::array<bool, N_MAX> isvisited;
    isvisited.fill(false);
    exhaust_search(0, 0, n, isvisited);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    FOR(i,0,n) {
        dis[i] = -1;
        FOR(j,0,n) {
            weight[i][j] = -1;
        }
    }

    int u, k, v, c;
    FOR(i,0,n) {
        cin >> u >> k;
        FOR(j,0,k) {
            cin >> v >> c;
            weight[u][v] = c;
        }
    }

    set_shortest_distance(n);

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

    return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main() {
	int n, u, k, v, c, i, j, d[100][100], m[100][100], o, p[100] = { 0 }, e, q = 0;
	memset(m, 0x7f, sizeof(m));
	memset(d, 0x7f, sizeof(d));
	d[0][0] = 0;
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> u >> k;
		for (j = 0; j < k; j++) {
			cin >> v >> c;
			m[u][v] = c;
		}
	}
	p[0] = 1;
	for (i = 1; i < n; i++) {
		o = d[0][n - 1];
		for (j = 0; j < n; j++) {
			d[i][j] = min(d[i][j], m[q][j] + d[i - 1][q]);
			d[i][j] = min(d[i][j], d[i - 1][j]);
			if (o >= d[i][j] && p[j] == 0) {
				e = j;
				o = d[i][j];
			}
		}
		p[e] = 1;
		q = e;
	}
	for (i = 0; i < n; i++)cout << i << ' ' << d[n - 1][i] << endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#define MAX 101
#define INF 100001
using namespace std;

int n, isSelected[MAX], dist[MAX];
vector<pair<int, int>> ver[MAX];

void initDijkstra(void);
void dijkstra(void);
bool doesFinished(void);

int main() {
	int i, j, id, k, v, c;
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> id >> k;
		for (j = 0; j < k; j++) {
			cin >> v >> c;
			ver[id].push_back(make_pair(v, c));
		}
	}

	initDijkstra();

	while (!doesFinished())dijkstra();

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

	return 0;
}

void initDijkstra(void) {
	dist[0] = 0;
	isSelected[0] = 1;
	for (int i = 1; i <= n; i++)dist[i] = INF;
}

void dijkstra(void) {
	int i, minu, minp, mindist = INF;
	vector<pair<int, int>>::iterator itv;
	for (i = 0; i < n; i++) {
		if (isSelected[i] == 0)continue;
		for (itv = ver[i].begin(); itv != ver[i].end(); itv++){
			if (isSelected[itv->first] == 1)continue;
			if (itv->second < mindist) {
				mindist = itv->second;
				minu = itv->first;
				minp = i;
			}
		}
	}
	isSelected[minu] = 1;
	dist[minu] = dist[minp] + mindist;
}

bool doesFinished(void) {
	for (int i = 0; i < n; i++) {
		if (isSelected[i] == 0)return false;
	}
	return true;
}