Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

//make priority queue
type Item struct {
	id       int
	priority int // this state is priority and distans from start
	index    int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

// we want the ascending-priority-queue.
// So we use less than here.
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = j
	pq[j].index = i
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(dist int, id int) {
	for _, i := range *pq {
		if i.id == id {
			if i.priority > dist {
				i.priority = dist
			}
		}
	}
}

// the main code
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	return n
}

func dikstra(dist [][]int, n int) []int {
	// preparate priority queue
	inf := 1 << 30
	pq := make(PriorityQueue, n)
	shortests := make([]int, n)
	for i := 0; i < n; i++ {
		pq[i] = &Item{
			id:       i,
			priority: inf,
			index:    i,
		}

		shortests[i] = inf
	}
	pq[0].priority = 0
	shortests[0] = 0
	heap.Init(&pq)

	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		shortests[item.id] = item.priority
		for id, d := range dist[item.id] {
			if d != -1 {
				pq.update(item.priority+d, id)
			}
		}
		heap.Init(&pq)
	}
	return shortests
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// init
	n := nextInt(sc)
	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dist[i][j] = -1
		}
	}

	// std Input
	for i := 0; i < n; i++ {
		u := nextInt(sc)
		k := nextInt(sc)
		for j := 0; j < k; j++ {
			v, c := nextInt(sc), nextInt(sc)
			dist[u][v] = c
		}
	}

	shortests := dikstra(dist, n)
	for i, d := range shortests {
		fmt.Printf("%d %d\n", i, d)
	}
}

C++: #include <iostream>
using namespace std;
int n;
static const int MAX = 100;
static const int INF = (1 << 21);
int M[MAX][MAX];
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;


void dijkstra(){
    int minv;
    int d[MAX], color[MAX];

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

    d[0] = 0;
    // color[]

    while(1){
        minv = INF;
        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 && d[i] > M[u][i] != INF){
                if (d[i] > d[u] + M[u][i]){
                    color[i] = GRAY;
                    d[i] = d[u] + M[u][i];
                }
            }
        }
    }
    for (int i = 0; i < n; i++){
            cout << i << " ";
            if (d[i] == INF){
                cout << -1 << endl;
            }else{
                cout << d[i] << endl;
            }
            
        }
}

int main(void){
    int tmp;

    cin >> n;

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            M[i][j] = INF;
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

int d[100];

int main(void)
{
    int n;
    cin >> n;

    int graph[n][n];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            graph[i][j] = -1;

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

    for (int i = 0; i < n; i++)
        d[i] = 100000000;
    d[0] = 0;

    vector<int> queue;
    for (int i = 0; i < n; i++)
        queue.push_back(i);

    while (!queue.empty()) {
        int u = queue[0];
        size_t j = 0;
        for (size_t i = 0; i < queue.size(); i++)
            if (d[queue[i]] < d[u]) {
                u = queue[i];
                j = i;
            }
        queue.erase(queue.begin() + j);

        for (int v = 0; v < n; v++)
            if (graph[u][v] > 0)
                if (d[v] > d[u] + graph[u][v])
                    d[v] = d[u] + graph[u][v];
    }

    for (int i = 0; i < n; i++)
        cout << i << " " << d[i] << endl;

    
    return 0;
}

C++: #include<stdio.h>
#include<limits.h>
#define NIL -1
static const int MAX = 1005;
static const int INFTY = (1<<21);

main(){
  int n, i, j, v, c, u, k, minV;
  int M[MAX][MAX], A[MAX], key[MAX], p[MAX];
  /*your code*/

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

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

  /*your code*/
  for(i = 0; i < n; i++){
    key[i] = INT_MAX;
    p[i] = NIL;
    A[i] = NIL;
  }
  
  key[0]=0;
  
  while(1){
    
    u = NIL;
    
    minV = INT_MAX;
    
    for(i = 0;  i < n; i++){
      
      if(key[i] < minV && A[i] == NIL){
	
	u = i;
	
	minV = key[i];
	
      }
      
    }  
    if(u == NIL)break;
    
    A[u] = u; 
    
    for(v = 0; v < n; v++){
      if((M[u][v]+key[u]) < key[v] && A[v] == NIL){
	key[v] = (M[u][v]+key[u]);
	p[v] = u;
      }
    }
  }
  
  for ( i = 0; i < n; i++ ){
    printf("%d %d\n", i, (key[i]==INFTY?-1:key[i]));
  }

  return 0;

}

C++: #include <iostream>
using namespace std;
#define MAX 100
#define INFTY (1<<30)
#define rep(i,n) for(int i=0;i<n;i++)
enum{
	white,
	gray,
	black,
};

int n,m[MAX][MAX];

 void dijkstra(){
 	int minv;
 	int d[MAX],color[MAX];
 	rep(i,n){
 		d[i]=INFTY;
 		color[i]=white;
 	}
	d[0]=0;
 	color[0]=gray;
 	while(1){
 		minv=INFTY;
 		int u=-1;
 		rep(i,n){
 			if(minv>d[i]&&color[i]!=black){
 				u=i;
 				minv=d[i];
 			}
 		}
 		if(u==-1)	break;
 		color[u]=black;
 		rep(v,n){
 			if(color[v]!=black&&m[u][v]!=INFTY){
 				if(d[v]>d[u]+m[u][v]){
 					d[v]=d[u]+m[u][v];
 					color[v]=gray;
 				}
 			}
 		}
 	}
 	rep(i,n){
 		cout<<i<<" ";
 		if(d[i]==INFTY)	cout<<-1<<endl;
 		else	cout<<d[i]<<endl;
 	}
 }

 int main(){
 	cin>>n;
 	rep(i,n){
 		rep(j,n)		m[i][j]=INFTY;
 	}
 	int k,c,u,v;
 	rep(i,n){
 		cin>>u>>k;
 		rep(j,k){
 			cin>>v>>c;
 			m[u][v]=c;
 		}
 	}
 	dijkstra();
 	return 0;
 }

C++: #include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
typedef pair<int,int> P;
typedef pair<P,P> PP;

int main(void)
{
	int i,j,k,u,n,flg[10001],v,c,sum,ky[10001],t,min,g;
	PP ke[10001];
	scanf("%d",&n);
	sum=0;
	for(i=0;i<n;i++) ky[i]=M,flg[i]=0;
	ky[0]=0;
	for(i=0;i<n;i++){
		scanf("%d%d",&u,&k);
		for(j=sum;j<k+sum;j++){
			scanf("%d%d",&v,&c);
			ke[j].ff=u , ke[j].fs=v , ke[j].sf=c;
		}
		sum+=k;
	}
	sort(ke,ke+sum);
	//for(i=0;i<sum;i++) printf("ke[%d].ff=%d ke[%d].fs=%d ke[%d].sf=%d\n",i,ke[i].ff,i,ke[i].fs,i,ke[i].sf);
	for(i=0;i<sum;i++){
		/*min=M;
		for(j=0;j<sum;j++) if(flg[j]==0&&min>ky[j]) g=j,min=j;
		flg[g]=1;
		if(min==M) break;*/
		for(j=0;j<sum;j++){
			t=ky[ke[j].ff]+ke[j].sf;
			if(ky[ke[j].fs]>t) ky[ke[j].fs]=t;
		}
	}
	for(i=0;i<n;i++) printf("%d %d\n",i,ky[i]);
	return 0;
}