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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
static const int INFTY = (1 << 30);
int n;
void search(vector<vector<int> > &G1, vector<vector<int> > &G2, int *d, bool *v)
{
    int mini, minv;
    while (1)
    {
        bool iscomp = true;
        minv = INFTY;
        rep(i, 0, n)
        {
            if (!v[i] && d[i] < minv)
            {
                mini = i;
                minv = d[i];
                iscomp = false;
            }
        }
        if (iscomp)
            break;
        v[mini] = true;
        rep(i, 0, G1[mini].size())
        {
            int tmp = G1[mini][i];
            if (!v[tmp])
            {
                int tmptime = minv + G2[mini][i];
                if (tmptime < d[tmp])
                    d[tmp] = tmptime;
            }
        }
    }
}
int main()
{
    cin >> n;
    vector<vector<int> > G1(n, vector<int>(0)), G2(n, vector<int>(0));
    int d[n];
    bool v[n] = {};
    rep(i, 0, n) d[i] = INFTY;
    int utmp, ktmp, vtmp, ctmp;
    rep(i, 0, n)
    {
        cin >> utmp >> ktmp;
        rep(j, 0, ktmp)
        {
            cin >> vtmp >> ctmp;
            G1[utmp].push_back(vtmp);
            G2[utmp].push_back(ctmp);
        }
    }
    d[0] = 0;
    search(G1, G2, d, v);
    rep(i, 0, n)
    {
        printf("%d %d\n", i, d[i]);
    }
}

C++: #include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { -1, 0, 1, 0 };

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

	int n;
	cin >> n;

	vector<vector<pair<int, int>>> m(n);

	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;
			m[u].emplace_back(v, c);
		}
	}

	vector<int> d(n, INF), color(n);

	d[0] = 0;
	color[0] = 1;
	while (true) {
		int minv = INF;
		int u = -1;
		for (int i = 0; i < n; i++) {
			if (minv > d[i] && color[i] != 2) {
				u = i;
				minv = d[i];
			}
		}

		if (u == -1) {
			break;
		}

		color[u] = 2;
		for (auto e : m[u]) {
			if (color[e.first] != 2 && d[e.first] > d[u] + e.second) {
				d[e.first] = d[u] + e.second;
				color[e.first] = 1;
			}
		}
	}

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

C++: #include <stdio.h>
#define INF 10000000
typedef struct{
	int node;
	int cost;
} List;

int dist[101];
List list[101][101];
int kazu[101];

int n;

void walk(int x,int step){
	dist[x]=step;
	for(int i=0;i<kazu[x];i++){
		if(dist[list[x][i].node]>step+list[x][i].cost) walk(list[x][i].node,step+list[x][i].cost);
	}
}

int main(void){
	int i,j,w;
	int num;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&num);
		scanf("%d",kazu+num);
		for(j=0;j<kazu[num];j++)scanf("%d %d",&(list[num][j].node),&list[num][j].cost);
	}
	for(i=0;i<n;i++) dist[i]=INF;
	walk(0,0);
	
	for(i=0;i<n;i++){
		printf("%d %d\n",i,dist[i]);
	}
	return 0;
}

C++: #include <iostream>
#include <limits>
#define N 100

using namespace std;

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

int n,M[N][N],d[N];

void dijkstra(int s){
  bool visited[n];
  for(int i=0;i<n;i++){
    visited[i]=false;
    d[i]=INF;
  }
  d[s]=0;
  while(1){
    int mincost=INF;
    int u;
    //find minimum d from non-visited nodes
    for(int i=0;i<n;i++){
      if(!visited[i] && d[i]<mincost){
        mincost=d[i];
        u=i;
      }
    }
    if(mincost==INF) break;

    visited[u]=true;
    //update d
    for(int v=0;v<n;v++){
      if(!visited[v] && M[u][v]!=INF){
        if(d[u]+M[u][v]<d[v]){
          d[v]=d[u]+M[u][v];
        }
      }
    }
  }
}

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) M[i][j]=INF;
  }
  for(int i=0;i<n;i++){
    int u,k; cin >> u >> k;
    for(int i=0;i<k;i++){
      int v,c; cin >> v >> c;
      M[u][v]=c;
    }
  }
  dijkstra(0);
  for(int i=0;i<n;i++)
    cout << i << " " << d[i] << endl;
}