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 <stdio.h>
#include <vector>
#include <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>
#include <numeric>
#include <queue>

using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf("%d", list[i]);
        if ( i + 1 != list.size() ) {
            printf(" ");
        }
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

static int MAX = 1000000;

class Node{
public:
    Node(int id, int numEdge)
     : id(id)
     , edges(vector<int>(numEdge, MAX))
    {
        // constructer
    }
    
    void Edge(int id, int weight){
        this->edges[id] = weight;
    }

    vector<int> Edges() {
        return this->edges;
    }

    int id;
    bool done = false;
    vector<int> edges;
};

class List{
public:
    // constructer
    List(int numNode)
        : results(numNode)
    {
        for (int i = 0; i < numNode; i++) {
            this->nodes.push_back(Node(i, numNode));
            this->results[i] = MAX;
        }
    }

    void SetWeight(int from, int to, int weight)
    {
        this->nodes[from].Edge(to, weight);
    }
    
    vector<int> ShortedPath() {
        // this->Enqueue(0);
        this->results[0] = 0;
        this->queue.push(0);
        this->Traverse();
        return this->results;
    }

    void Traverse() {
        while(!this->queue.empty()) {
            int queueIndex = queue.front();
            queue.pop();
            vector<int> edges = this->nodes[queueIndex].Edges();
            for (int i = 0; i < edges.size(); i++) {
                // update result
                int myWeight = this->results[queueIndex];
                this->results[i] = std::min(this->results[i], myWeight + edges[i]);
            }
            // printList(this->results);
            // enqueue
            this->Enqueue(queueIndex);
            // done flag
            this->nodes[queueIndex].done = true;
        }
    }

    void Enqueue(int index) {
        // copy weights
        if (this->nodes[index].done) return;
        std::list<std::pair<int, int>> weights;
        for (int i = 0; i <  this->nodes[index].Edges().size(); i++) {
            weights.push_back(std::make_pair(i, this->nodes[index].Edges()[i]));
        }
        // sort
        weights.sort([](auto& lhs, auto& rhs){
            return lhs.second < rhs.second;
        });
        // enqueue
        for (auto weight : weights){
            queue.push(weight.first);
        }
    }
    
    

    void Print() {
        for (int i = 0; i < this->nodes.size(); i++) {
            printf("%d: ", i);
            for (int j = 0; j < this->nodes[i].Edges().size(); j++) {
                printf("%d (%d) ", j, this->nodes[i].Edges()[j]);
            }
            printf("\n");
        }
    }
private:
    std::queue<int> queue;
    std::vector<int> results;
    vector<Node> nodes;
};

void solve() {

    int n;
    scanf("%d", &n);
    List list(n);

    for (int i = 0; i < n; i++){
        int key;
        scanf("%d", &key);
        int k;
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            int dest;
            int weight;
            scanf("%d %d", &dest, &weight);
            list.SetWeight(key, dest, weight);
        }
    }
    // list.Print();
    auto result = list.ShortedPath();
    for(int i = 0; i < result.size(); i++) {
        printf("%d %d\n", i, result[i]);
    }
}

void test(){

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

C++: #include <stdio.h>
#define INF 1000000
#define white 0
#define black 2
#define gray 1

int n,M[100][100];

void dijkstra();

int main(){
  int i,j,k,c,v,u;
  scanf("%d",&n);
  for(i=0;i<n;i++){ 
    for(j=0;j<n;j++){
      M[i][j]=INF;
    }
  }
  for(i=0;i<n;i++){
    scanf("%d%d",&u,&k);
    for(j=0;j<k;j++){
      scanf("%d%d",&v,&c);
      M[u][v]=c;
    }
  }
  dijkstra();
  
  return 0;
}
void dijkstra(){
  int i,d[100],mincost,color[100],v,u;
  for(i=0;i<n;i++){
    d[i]=INF;
    color[i]=white;
  }
  d[0]=0;
  color[0]=gray;

  while(1){
    mincost=INF;
    u=-1;
    for(i=0;i<n;i++){
      if(color[i]!=black&&d[i]<mincost){
	mincost=d[i];
	u=i;
      }
    }
    if(u==-1)break;
    
    color[u]=black;
    for(v=0;v<n;v++){
      if(color[v]!=black&&M[u][v]!=INF){
	if(d[v]>d[u]+M[u][v]){
	  d[v]=d[u]+M[u][v];
	  color[v]=gray;
	}
      }
    }
  }
  for(i=0;i<n;i++){
    printf("%d ",i);
    if(d[i]==INF)printf("-1");
    else printf("%d",d[i]);
    printf("\n");
  }
}