Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

var infty = 1000000000

type DirectedGraphNodeID int

type DirectedGraphNode struct {
	key         DirectedGraphNodeID
	adjacents   []*DirectedGraphNode
	costs       []int
	distance    int
	predecessor *DirectedGraphNode
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]*DirectedGraphNode, nNodes)
	for i := 0; i < nNodes; i++ {
		node := DirectedGraphNode{}
		node.key = DirectedGraphNodeID(i)
		node.distance = infty
		graph.nodes[i] = &node
	}
	return &graph
}

type DirectedGraph struct {
	nodes []*DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(i, j DirectedGraphNodeID, cost int) {
	g.nodes[i].adjacents = append(g.nodes[i].adjacents, g.nodes[j])
	g.nodes[i].costs = append(g.nodes[i].costs, cost)
}

func (g *DirectedGraph) print() {
	for i := 0; i < len(g.nodes); i++ {
		node := g.nodes[i]
		key := node.key
		strings := []string{strconv.Itoa(int(key)), strconv.Itoa(node.distance)}
		for j := 0; j < len(node.adjacents); j++ {
			nodeJ := fmt.Sprintf("(%v, %v)", node.adjacents[j].key, node.costs[j])
			strings = append(strings, nodeJ)
		}
		fmt.Printf("%v\n", strings)
	}
}

func (g *DirectedGraph) relaxEdge(l, r *DirectedGraphNode, w int) {
	if r.distance > l.distance+w {
		// fmt.Printf("%v -> %v, %v\n", l.key, r.key, l.distance+w)
		r.distance = l.distance + w
		r.predecessor = l
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	graph := newDirectedGraph(nNodes)

	for i := 0; i < nNodes; i++ {
		iNode := DirectedGraphNodeID(nextInt())
		nAdjacent := nextInt()
		for j := 0; j < nAdjacent; j++ {
			jNode := DirectedGraphNodeID(nextInt())
			cost := nextInt()
			graph.setAdjacency(iNode, jNode, cost)
		}
	}

	graph.nodes[0].distance = 0
	for iNode := 0; iNode < nNodes; iNode++ {
		for iLeft := 0; iLeft < len(graph.nodes); iLeft++ {
			nodeLeft := graph.nodes[iLeft]
			for iRight := 0; iRight < len(nodeLeft.adjacents); iRight++ {
				nodeRight := nodeLeft.adjacents[iRight]
				cost := nodeLeft.costs[iRight]
				graph.relaxEdge(nodeLeft, nodeRight, cost)
			}
		}
	}

	// graph.print()

	for iNode := 0; iNode < len(graph.nodes); iNode++ {
		node := graph.nodes[iNode]
		fmt.Println(node.key, node.distance)
	}
}

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

const int MAX = 100;
const int INFINITY = 2 << 21;
int n;
int G[MAX][MAX];
int distances[MAX];
int states[MAX];

void dijkstra() {
  int id = 0;
  distances[id] = 0;

  while (1) {
    int min = INFINITY;

    for (int i = 0; i < n; i++) {
      if (states[i] == 0 && distances[i] < min) {
        min = distances[i];
        id = i;
      }
    }

    if (min == INFINITY) break;

    for (int i = 0; i < n; i++) {
      if (states[i] != 0 || G[id][i] == -1) continue;
      if (distances[id] + G[id][i] < distances[i]) {
        distances[i] = distances[id] + G[id][i];
      }
    }

    states[id] = 1;
  }

  for (int i = 0; i < n; i++) {
    cout << i << " " << (distances[i] == INFINITY ? -1 : distances[i]) << endl;
  }
}

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

  cin >> n;

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

  int u, k, v, c;
  for (int i = 0; i < n; i++) {
    distances[i] = INFINITY;
    states[i] = 0;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      G[i][v] = c;
    }
  }

  dijkstra();

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
// N 为邻接矩阵的大小
#define N 101
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1 << 21

using namespace std;

int color[N], d[N], p[N], M[N][N];

void dijkstra (int s, int n) {
  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    d[i] = INFTY;
  }

  d[s] = 0;
  p[s] = -1;

  while (1) {
    int mincost = INFTY, u;

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

    if (mincost == INFTY)
      break;

    color[u] = BLACK;
    
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && M[u][v] >= 0 && M[u][v] < INFTY) {

        if (d[u] + M[u][v] < d[v]) {
          d[v] = d[u] + M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }        
      }
    }
  }
}

int main () {
  int n, u, v, degree, cost;
  
  cin >> n;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      M[i][j] = INFTY;

  for (int i = 0; i < n; i++) {
    cin >> u >> degree;
    
    for (int j = 0; j < degree; j++) {
      cin >> v >> cost;

      M[u][v] = cost;
    }
  }

  dijkstra(0, n);

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

  return 0;
}

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

class DisjointSet{
public:
    //???????????????
    vector<int> rank;
    //??????????´??????????
    vector<int> p;

    DisjointSet(unsigned long size){
        //???????????´
        rank.resize(size,0);
        p.resize(size,0);
        //??????n?????????
        for(int i=0;i<size;i++)
            makeSet(i);
    }

    //???????????????????´?x?????°??????
    void makeSet(int x){
        p[x]=x;
        rank[x]=0;
    }

    //??????x???y????????¨???????????????
    bool same(int x,int y){
        return findSet(x)==findSet(y);
    }

    //?????¶x???y?????¨?????????
    void unite(int x,int y){
        link(findSet(x),findSet(y));
    }

    //?????\x???y
    void link(int x,int y){
        //?????????????????\??°????????????????????¢
        if(rank[x]>rank[y])
            p[y]=x;
        else{
            p[x]=y;
            //??????????????????????????????????????¶????????????????????????
            if(rank[x]==rank[y])
                rank[y]++;
        }
    }

    //?±???£??¨????´?
    int findSet(int x){
        //??????????¶??????¶??´???x???????????£??¨????´?
        if(x!=p[x])
            //????????????
            p[x]=findSet(p[x]);
        return p[x];
    }
};

int main(){
    int n,a,b,q,t;

    cin>>n>>q;
    DisjointSet ds(n);

    for(int i=0;i<q;i++){
        cin>>t>>a>>b;
        if(t==0)
            ds.unite(a,b);
        else if(t==1){
            if(ds.same(a,b))
                cout<<1<<endl;
            else
                cout<<0<<endl;
        }
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define loop(i,a,b) for(i=a;i<b;i++)
#define rloop(i,a,b) for(i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
const int inf=1000000001;
const ll INF=1e16;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int N=100;

int n;
vii m(N,vi(N));
vi d(N,inf);

void dijkstra();

int main(){
	int i,j;
	cin>>n;
	int u,k,v,c;
	loop(i,0,n){
		loop(j,0,n){
			m[i][j]=inf;
		}
	}
	loop(i,0,n){
		cin>>u>>k;
		loop(j,0,k){
			cin>>v>>c;
			m[u][v]=c;
		}
	}
	dijkstra();
	loop(i,0,n){
		cout<<i<<" "<<d[i]<<endl;
	}
}

void dijkstra(){
	int i;
	d[0]=0;
	vi color(n,WHITE);
	color[0]=BLACK;
	int u=0,v;
	while(1){
		int minv=inf;
		loop(i,0,n){
			if(color[i]!=BLACK){
				d[i]=min(d[i],d[u]+m[u][i]);
				if(minv>d[i]){
					minv=d[i];
					v=i;
				}
			}
		}
		if(minv==inf){
			break;
		}
		color[v]=BLACK;
		u=v;
	}
	
}