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>
#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;
}