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<queue>
#define INF 100000000
using namespace std;
int main(){
  int n;cin>>n;
  int u,k,v,c;
  int mp[101][101],d[101];
  for(int i=0;i<=100;i++){
    d[i]=INF;
    for(int j=0;j<=100;j++)mp[i][j]=INF;
  }
    while(u!=n-1){
    cin>>u>>k;
    for(int i=0;i<k;i++){
      cin>>v>>c;
      mp[u][v]=c;
    }
  }
     
    int mn,s,t[101]={},cn=n-1;
    d[0]=0,t[0]=1;
    while(cn){
      mn=INF;
      for(int i=0;i<n;i++){
    if(t[i]==0){
      if(d[i]>d[s]+mp[s][i])d[i]=d[s]+mp[s][i];
      if(mn>d[i])mn=d[i],k=i;
    }
      }
      if(mn!=INF)cn--,t[k]=1;
    }
    for(int i=0;i<n;i++)cout<<i<<" "<<d[i]<<endl;
     
  return 0;
}

C++: #include <iostream>
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];

void search() {
	int d[MAX], p[MAX], color[MAX];
	for (int i = 0; i < n; i++) {
		p[i] = -1;
		d[i] = INFTY;
		color[i] = WHITE;
	}
	d[0] = 0;
	while (true) {
		int minv = INFTY;
		int u = -1;
		for (int i = 0; i < n; i++) {
			if (minv > d[i] && color[i] != BLACK) {
				minv = d[i];
				u = i;
			}
		}
		if (u == -1)break;
		color[u] = BLACK;
		for (int i = 0; i < n; i++) {
			if (color[i] != BLACK && M[u][i] != INFTY) {
				if (d[u] + M[u][i] < d[i]) {
					d[i] = d[u] + M[u][i];
					p[i] = u;
					color[i] = 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;
		}
	}
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			cin >> M[u][v];
		}
	}

	search();
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define INF 1<<20
using namespace std;
typedef unsigned long long ll;
int n;
ll A[1000][1000];
ll d[1000];
bool used[1000];
ll D[1000][1000];
 
void Init(int f){
  for(int i=0;i<f;i++){
    for(int j=0;j<f;j++)D[i][j] = INF;
  }
 
}
 
int main(){
  scanf("%d",&n);
  int u,k,C,V;
  Init(n);
  
  for(int i=0;i<n;i++){
      scanf("%d %d",&u,&k);     
      for(int j=0;j<k;j++){
    scanf("%d %d",&V,&C);
    D[u][V] = C;
    D[V][u] = C;
      }     
  }
   
  for(int i=0;i<n;i++)D[i][i] = 0;
  for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j] = min(D[i][j],D[i][k] + D[k][j]); 
  for(int i=0;i<n;i++){
    printf("%lld %lld\n",i,D[0][i]);
  }
 
  return 0;
}

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

int main()
{
	int n, m, i, j, a, w;
	int* adj[100];
	int* dist[100];
	cin >> n;
	for(i = 0; i < n; i++){
		cin >> i >> m;
		adj[i] = new int [m + 1];
		dist[i] = new int [m + 1];
		for(j = 0; j < m; j++){
			cin >> a >> w;
			adj[i][j] = a;
			dist[i][j] = w;
		}
		adj[i][m] = -1;
		dist[i][m] = -1;
	}
	int *d, *prev, *Q;
	d = new int [n];
	prev = new int [n];
	Q = new int [n];
	for(i = 0; i < n; i++){ d[i] = 4000; prev[i] = -1; Q[i] = i; }
	int rest = n; // 0????????£???????????????Q???????????????

	int u, v, min;
	d[0] = 0;  // 0??????????????????
	while(rest > 0){
		min = 4000;
		for(i = 0; i < n; i++){
			if(min > d[i] && Q[i] >= 0){ min = d[i]; u = i; }
		}
		Q[u] = -1;  // d[u]???Q???????????????????°????d??????????????????
		rest--;	    // Q????????????-1?????????rest?????????????????????
		// u?????£??\???????????????d?????´??°???
		for(j = 0; adj[u][j] >= 0; j++){
			v = adj[u][j];
			if(d[v] > d[u] + dist[u][j]){
				d[v] = d[u] + dist[u][j];
				prev[v] = u;
			}
		}
	};
	for(i = 0; i < n; i++){ cout << i << " " << d[i] << endl; }
	for(i = 0; i < n; i++){
		delete adj[i]; delete dist[i]; }
	delete d;
	delete prev;
	delete Q;
	cin >> n;

	return 0;
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <algorithm>

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define print_array(ary, size) cout << ary[0]; rep1(i, 1, size - 1) cout << ' ' << ary[i]; cout << endl;
typedef long long ll;
typedef unsigned long long ull;

int M[100][100];
int MinCost[100];
int done[100];

int main()
{
	int n, u, k, v, c;
	int next;
	int cost, sum = 0;

	cin >> n;

	rep(i, n) {
		MinCost[i] = 10000001;
		done[i] = 0;
		rep(j, n) M[i][j] = -1;
	}

	rep(i, n) {
		cin >> u >> k;
		rep(j, k) {
			cin >> v >> c;
			M[u][v] = c;
		}
	}

	MinCost[0] = 0;
	next = 0;
	done[0] = 1;
	sum = 0;
	
	rep(loop, n - 1) {
		rep(i, n) {
			if (M[next][i] != -1) {
				MinCost[i] = min(MinCost[i], MinCost[next] + M[next][i]);
			}
		}

		cost = 10000001;
		rep(i, n) {
			if (done[i] == 0 && cost > MinCost[i]) {
				cost = MinCost[i];
				next = i;
			}
		}

		done[next] = 1;
		sum += cost;
	}

	rep(i, n) {
		cout << i << ' ' << MinCost[i] << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n;
int res = 0;
struct node {
    int id;
    int dist;
    bool searched;
};

node v[100];
int edge[100][100];

int main() {
    cin >> n;
    REP(i, n) {
        int u, k;
        cin >> u >> k;
        v[u].id = u;
        v[u].dist = INF;
        v[u].searched = false;
        REP(j, n) edge[i][j] = INF;
        REP(j, k){
            int to, cost;
            cin >> to >> cost;
            edge[i][to] = cost;
        }
    }
    v[0].dist = 0;
    int next = 0;
    int prev = 0;
    REP(i, n){
        int minimum = INF;
        REP(j, n){
            if(!v[j].searched && edge[prev][j] != INF){
                v[j].dist = min(v[j].dist, v[prev].dist + edge[prev][j]);
                if(minimum >= v[j].dist){
                    next = j;
                    minimum = v[j].dist;
                }
            }
        }
        v[prev].searched = true;
        prev = next;
    }
    REP(i, n) cout << i << " " << v[i].dist << endl;
    return 0;
}