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>
using namespace std;
static const int N = 100;
static const int INF = 10000000;
static const int WHITE =0;
static const int GRAY =1;
static const int BLACK =2;

int n,A[N][N]; //隣接行列

void dijkstra(){
    int min ;
    int d[N],color[N];

    for(int i =0;i<n;i++){
        d[i] = INF;
        color[i] =WHITE;
    }
    d[0] =0;
    color[0] = GRAY;
    while(1){
        min = INF;
        int u = -1;
        for(int i =0;i <n;i++){
            if(min > d[i] && color[i] != BLACK){
                u = i;
                min  = d[i];
            }
        }
        if(u ==-1) break;
        color[u] = BLACK;
        for(int v =0;v<n;v++){
            if(color[v] != BLACK && A[u][v] != INF){
                if(d[v] > d[u] + A[u][v]){
                    d[v] = d[u] + A[u][v];
                    color[v] =GRAY; 
                }
            }
        }
    }

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

int main(){
    cin >> n;
    for(int i=0;i<n;i++){
        for(int j =0;j<n;j++){
            A[i][j] = INF;
        }
    }
    
    int k,c,u,v;
    for(int i =0;i<n;i++){
        cin >> u >> k;
        for(int j =0;j<k;j++){
            cin >> v >> c;
            A[u][v] = c;
        }
    }

    dijkstra();

    return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>


#define MAX_E 20
#define MAX_V 7
#define INF 100000

typedef struct edge{
    int from;
    int to;
    int cost;
}edge;


edge es[MAX_E];

int distance[MAX_V];

void bellman_ford(int n, int s, int e) {
    int i, update;
    
    for (int i = 0; i < MAX_V; i++) distance[i] = INF;
    distance[s] = 0;
    while (1) {
        update = 0;
        for (i = 0; i < e; i++) {
            edge e = es[i];
            if (distance[e.from] != INF && distance[e.to] > distance[e.from] + e.cost) {
                distance[e.to] = distance[e.from] + e.cost;
                update = 1;
            }
        }
        if (!update) break;
    }
}


int main( ){
    
    int n, i, j, l, u, v, k, c;
    scanf("%d", &n);
    
    
    l = 0;
    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        
        for (j =0 ; j < k; j++) {
            scanf("%d %d", &v, &c);
            es[l].from = u;
            es[l].to = v;
            es[l].cost = c;
            l++;
        }
    }
    
    bellman_ford(n, 0, l);
    
    for (i=0; i<n; i++) {
        printf("%d %d\n", i, distance[i]);
    }
    
    return 0;
}

C++: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Graph vector< vector<P> >
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;

int main(){
    int N;
    cin >> N;
    Graph M(N);
    rep(i, N){
        int u, k;
        cin >> u >> k;
        rep(j, k){
            int v, c;
            cin >> v >> c;
            M[u].push_back(P(v, c));
        }
    }

    vector<int> d(N, INF), p(N, -1);
    vector<bool> seen(N, false);
    d[0] = 0;

    while(1){
        int mincost = INF;
        int u;

        rep(i, N){
            if(!seen[i] && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }

        if(mincost == INF) break;
        seen[u] = true;

        rep(i, M[u].size()){
            int v = M[u][i].first, c = M[u][i].second;
            if(!seen[v] && d[u] + c < d[v]){
                d[v] = d[u] + c;
                p[v] = u;
            }
        }
    }

    rep(i, N){
        cout << i << ' ' << d[i] << endl;
    }

}

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

#define MAX 100
#define INF 2097152
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n,M[MAX][MAX];


int main(void){

	int i,j;
	int minv,d[MAX],color[MAX];
	int u,v;

	scanf("%d",&n);
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			M[i][j] = INF;
		}
	}

	int k,l,m,p;
	for(i=0;i<n;i++){
		scanf("%d %d",&m,&k);
		for(j=0;j<k;j++){
			scanf("%d %d",&p,&l);
			M[m][p] = l;
		}
	}

	for(i=0;i<n;i++){
		d[i] = INF;
		color[i] = WHITE;
	}

	d[0] = 0;
	color[0] = GRAY;

	while(1){
		minv = INF;
		u = -1;
		for(i=0;i<n;i++){
			if(minv > d[i] && color[i] != BLACK){
				u = i;
				minv = d[i];
			}
		}
		if(u == -1) break;
		color[u] = BLACK;
		for(v=0;v<n;v++){
			if(d[v]>d[u] + M[u][v]){
				d[v] = d[u] + M[u][v];
				color[v] = GRAY;
			}
		}
	}

	for(i=0;i<n;i++){
		if(d[i] == INF){
			printf("%d -1\n",i);
		}else{
			printf("%d %d\n",i,d[i]);
		}
	}

return 0;

}

C++: #include <stdio.h>
#define INF (1<<21)

int main(void)
{
 int i,j,n,m,u,k,l,v,c,a[10000],b[10000],d[10000][10000];

 scand("%d",&n);
 
 for(i=0;i<n;i++){
 	scanf("%d %d",&u,&k);
 	a[i]=INF;
 	b[i]=0;
 	for(j=0;j<k;j++){
 		scanf("%d %d",&v,&c);
 		d[u][v]=c;
 	}
 }
 a[0]=0;
 
 while(1){
 	
 	l=-1;
 	m=INF;
 	
 	for(i=0;i<n;i++){
 		if(a[i]<m && b[i]=0){
 			m=a[i];
 			l=i;
 		}
 	}
 	
 	if(l==-1) break;
 	
 	b[l]=1;
 	
 	for(i=0;i<n;i++){
 		if(a[i]>=d[l][i]+a[l]) a[i]=d[l][i]+a[l];
 	}
 }
 
 for(i=0;i<n;i++){
 	if(a[i]==INF) printf("%d -1\n",i);
 	else printf("%d %d\n",i,a[i]);
 }
 
 return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 200001 
using namespace std;
int n;
int A[1000][1000];
int 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] = D[j][i] = min(D[i][j],D[i][k] + D[k][j]);

  for(int i=0;i<n;i++){
    printf("%d %d\n",i,D[0][i]);
  }
 
  return 0;
}