Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	weight := make([]int, n)
	for i := range list {
		list[i] = make([]int, n)
		weight[i] = NIL
		for j := range list[i] {
			list[i][j] = NIL
		}
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		for j := 0; j < k; j++ {
			v := scanInt()
			w := scanInt()
			list[u][v] = w
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for i, w := range list[node.id] {
			if w == NIL {
				continue
			}
			heap.Push(h, Node{id: i, weight: w + node.weight})
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		fmt.Fprint(w, i)
		fmt.Fprint(w, " ")
		fmt.Fprint(w, weight[i])
		fmt.Fprintln(w)
	}
	w.Flush()
}

C++: #include <iostream>
using namespace std;
static const int maxs=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[maxs][maxs];

void dicstra(){
  int minv;
  int d[maxs],color[maxs];
  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;
    }
  }
  dicstra();
  return 0;
}

C++: #include<bits/stdc++.h>
#define N 100
#define INFTY (1<<21)
#define WH 0
#define GR 1
#define BL 2
using namespace std;

vector<int> color(N),d(N),p(N);
vector<vector<int>> M(N,vector<int>(N));
int n;

void dijkstra(){
	for(int i=0;i<n;i++){
		color.at(i) = WH;
		d.at(i) = INFTY;
	}
	d.at(0) = 0;
	color.at(0) = GR;
	
	while(1){
		int mincost = INFTY;
		int u = -1;
		for(int i=0;i<n;i++){
			if(color.at(i) != BL && d.at(i) < mincost){
				mincost = d.at(i);
				u = i;
			}
		}
		if(u == -1) break;
		
		color.at(u) = BL;
		
		for(int v=0;v<n;v++){
			if(color.at(v) != BL && M.at(u).at(v) != INFTY){
				if(d.at(u) + M.at(u).at(v) < d.at(v)){
					d.at(v) = d.at(u) + M.at(u).at(v);
					p.at(v) = u;
					color.at(v) = GR;
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		cout << i << " ";
		if(d.at(i) == INFTY) cout << -1 << endl;
		else cout << d.at(i) << endl;
	}
	
}

int main(){
	cin >> n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			M.at(i).at(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.at(u).at(v) = c;
		}
	}
	dijkstra();
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF (1<<21)
#define white 0
#define gray 1
#define black 2

int n;
int W[100][100];
int color[100],d[100],p[100];

void dijkstra(int s) {
  d[s] = 0;

  while(1) {

    int mini = INF,u;
    for(int i = 0;i < n;i++) {
      if(color[i] != black && mini > d[i]) {
        mini = d[i];
        u = i;
      }
    }

    if(mini == INF) break;
    color[u] = black;

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

  }

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


int main() {

  cin >> n;
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < n;j++) {
      W[i][j] = INF;
    }
  }

  for(int i = 0;i < n;i++) {
    int u,k;
    cin >> u >> k;
    color[i] = white;
    d[i] = INF;
    for(int j = 0;j < k;j++) {
      int v,c;
      cin >> v >> c;
      W[u][v] = c;
    }
  }

  dijkstra(0);

}

C++: #include <vector>
#include <iostream>
#include <algorithm>
#include <limits>

using namespace std;
using graph = vector<vector<int>>;


int main() {
    int n;
    cin >> n;

    graph G(n, vector<int>(n, -1));
    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;
            G[u][v] = c;
        }
    }

    // Dijkstra's algorithm
    vector<int> d(n, -1);
    vector<bool> visited(n, false);

    d[0] = 0;
    visited[0] = true;
    while (1) {
        int u = -1;
        int v = -1;
        int minw = numeric_limits<int>::max();
        for (int i = 0; i < n; ++i) {
            if (!visited[i])
                continue;
            for (int j = 0; j < n; ++j) {
                if (visited[j] || G[i][j] < 0)
                    continue;
                int w = G[i][j] + d[i];
                if (w < minw) {
                    u = i;
                    v = j;
                    minw = w;
                }
            }
        }
        if (v < 0) {
            break;
        }
        d[v] = minw;
        visited[v] = true;
    }

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

    return 0;
}

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

int cost[100][100];
int d[100];//??????s?????????????°??????¢
bool used[100];
int arrow;//????????°

const int INF = 10000000;

void dijkstra(){
  fill(d,d+arrow,INF);
  fill(used,used + arrow,false);
  d[0] = 0;

  while(1){
    int v=-1;
    // ???????????????????????????????????????????????¢???????°???????????????¢???
    for(int u=0;u<arrow;u++){
      if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
    }

    if(v == -1) break;

    used[v] = true;

    for(int u=0; u<arrow; u++){
      if(cost[v][u] != INF){
        d[u] = min(d[u],d[v] + cost[v][u]);
      }
    }
  }

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

}

int main(){

    cin >> arrow;

    int n,a,v,c;



    for(int i=0;i<arrow;i++){
      for(int j=0;j<arrow;j++){
        cost[i][j] = INF;
      }
    }

    /*for(int i=0; i<arrow; i++){
      cost[i][i] = 0;
    }*/

    for(int u=0;u<arrow;u++){

      cin >> n >> a;

      for(int i=0; i<a;i++){
        cin >> v >> c;
        cost[n][v] = c;
      }
    }

    dijkstra();

    return 0;
}

C++: #include <iostream>
#include <cmath>
#define INF 1000000000
using namespace std;

int main(){
	int n;
	int cost[100][100];
	cin >> n;
	for(int i = 0;i < n;i++){
		for(int j = 0;j < n;j++) cost[i][j] = INF;
		cost[i][i] = 0;
	}
	for(int i = 0;i < n;i++){
		int now,m;
		cin >> now >> m;
		for(int j = 0;j < m;j++){
			int a,b;
			cin >> a >> b;
			cost[i][a] = b;
		}
	}
	for(int k = 0;k < n;k++){
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
			}
		}
	}
	
	for(int i = 0;i < n;i++) cout << i << " " << cost[0][i] << endl;
	return 0;
}