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>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
static const int MAX = 10000;
static const int INFTY = (1<<20);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
//???????????????????????\??¨??¨??????
vector<pair<int, int>>adj[MAX];

void dijkstra(){
    priority_queue<pair<int, int>>PQ;
    int color[MAX];
    int d[MAX];
    for(int i = 0; i<n; i++){
        d[i] = INFTY;
        color[i] = WHITE;
    }

    d[0] = 0;
    PQ.push(make_pair(0,0));
    color[0] = GRAY;

    while(!PQ.empty()){
        pair<int, int>f = PQ.top();
        PQ.pop();

        int u = f.second;

        color[u] = BLACK;

        // ??????????°???????????????????????????????????????\
        if(d[u]<f.first*(-1))
            continue;

        for(int j = 0;j<adj[u].size(); j++){
            int v = adj[u][j].first;
            if(color[v]==BLACK)continue;
            if(d[v]>d[u]+adj[u][j].second){
                d[v]=d[u]+adj[u][j].second;
                //priority_queue ?????????????????§??????????????????-1
                PQ.push(make_pair(d[v]*(-1),v));
                color[v]=GRAY;
            }
        }
    }

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

int main(){
    int k,u,v,c;

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

    dijkstra();
}

C++: #include <stdio.h>
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];

int dijkstra(int s) {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    
    d[s] = 0;
    
    while(1) {
        int u;
        int mincost = INFTY;
        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] != INFTY) {
                if(d[u] + M[u][v] < d[v]) {
                    d[v] = d[u] + M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    
    int sum = 0;
    for (int i=0; i<n; i++) {
        sum += d[i];
    }
    return sum;
}

int main() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            int tmp;
            scanf("%d", &tmp);
            M[i][j] = (tmp == -1) ? INFTY : tmp;
        }
    }
    
    int sum = dijkstra(0);
    printf("%d\n", sum);
    
    return 0;
}

C++: #include<bits/stdc++.h>
#define to first
#define wgt second 

using namespace std;

typedef pair<int,int> pii;
typedef vector<pii> vec;
typedef vector<vec> Graph;

Graph G(11000);
int n,d[11000];

void SPFA()
{
	bool used[11000];
	queue<int> que;
	memset(used,false,sizeof(used));
	fill(d,d+n,INT_MAX);
	d[0]=0;
	que.push(0);
	while(!que.empty())
	{
		int u=que.front();que.pop();
		for(int i=0;i<G[u].size();i++)
			if(d[G[u][i].to]>d[u]+G[u][i].wgt)
			{
				d[G[u][i].to]=d[u]+G[u][i].wgt;
				que.push(G[u][i].to);
			}
	}
}

int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		int u,t;
		scanf("%d%d",&u,&t);
		for(int j=0;j<t;j++)
		{
			int v,w;
			scanf("%d%d",&v,&w);
			G[u].push_back(make_pair(v,w));
		}
	}
	SPFA();
	for(int i=0;i<n;i++)
		printf("%d %d\n",i,d[i]);
	return 0;
}

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

const int MAX = numeric_limits<int>::max();

class Node{
public:
  int _id;
  bool _isFound;
  vector<int> _adj_index; // 隣接点のid
  vector<int> _adj_vertices;  // 隣接点との距離(重み)
  int _cost;

  void init(int id, int n){
    _id = id;
    _isFound = false;
    for(auto i = 0; i < n; i++){
      _adj_vertices.push_back(MAX);
    }
  }
};


void inputAdjList(vector<Node>& graph){
  for(auto i = 0; i < graph.size(); i++){
    int id, dim;
    cin >> id >> dim;
    graph[i].init(i, graph.size());
    for(auto j = 0; j < dim; j++){
      int vertex, distance;
      cin >> vertex >> distance;
      graph[i]._adj_index.push_back(vertex);
      graph[i]._adj_vertices[graph[i]._adj_index[j]] = distance;
    }
  }
}

void dijkstra(vector<Node>& nodes, int start){
  for(auto& node : nodes){
    node._isFound = false;
    node._cost = MAX;
  }

  nodes[start]._cost = 0;

  while(true){
//  cout << endl << "while start" << endl;
    Node *doneNode = nullptr;
    for(auto& node : nodes){
//  cout << "call for start" << node._cost << endl;
      if(node._isFound || node._cost == MAX){
//  cout << "continue" << node._cost << endl;
        continue;
      }
      if(doneNode == nullptr || node._cost < doneNode->_cost){

        doneNode = &node;
//  cout << "call" << node._cost << endl;
      }
    }
    if(doneNode == nullptr){
      break;
    }

//    cout << nodes[0]._isFound << endl;
    doneNode->_isFound = true;
//    cout << nodes[0]._isFound << endl;
    for(auto i = 0; i < doneNode->_adj_index.size(); i++){
//      cout << "i " << i << endl;
      auto nextIndex = doneNode->_adj_index[i];
      auto cost = doneNode->_cost + doneNode->_adj_vertices[doneNode->_adj_index[i]];
//    cout << endl << nextIndex << endl;
//    cout << cost << endl;
//    cout << nodes[nextIndex]._cost << endl;
      if(nodes[nextIndex]._cost == MAX || cost < nodes[nextIndex]._cost){
        nodes[nextIndex]._cost = cost;
//    cout << "called " << endl;
      }
    }
  }
}


int main(){
  int n;
  cin >> n;
  vector<Node> graph(n);

  inputAdjList(graph);
  dijkstra(graph, 0);

  for(auto g : graph){
    cout << g._id << " " << g._cost << endl;
  }

  return 0;
}