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<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

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];

int Dijkstra()
{
    int u, minv;
    int d[MAX], p[MAX], color[MAX];

    for(int i = 0; i < n; i++)
    {
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE;
    }

    // 初期値の設定
    d[0] = 0;
    color[0] = GRAY;

    while(1)
    {
        minv = INFTY;
        // 次に移動するべき頂点
        u = -1;

        for(int 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(int v = 0; v < n; v++)
        {
            if( color[v] != BLACK && M[u][v] != INFTY )
            {
                // u → v の辺の中で, 最小コストの辺のコストを記録する
                if( d[v] > d[u] + M[u][v] )
                {
                    // コストの記録
                    d[v] = d[u] + M[u][v];
                    // 頂点 v の親を記録する
                    p[v] = u;
                    // 訪問状態の記録
                    color[v] = GRAY;
                }
            }
        }
    }

    /*
    int sum = 0;

    for(int i = 0; i < n; i++)
    {
        if( p[i] != -1 )
        {
            // 最小コストの辺の和を求める
            sum += M[i][p[i]];
        }
    }
    */

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

    return 0;
}

int main(void)
{

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            // 配列の初期化
            M[i][j] = INFTY;
        }
    }

    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;

            M[u][v] = c;
        }
    }

    Dijkstra();

    return 0;

}

C++: #include <bits/stdc++.h>
using namespace std;

constexpr int MAX = 110;
constexpr int INF = 1e9;

int N;
int G[MAX][MAX];
bool seen[MAX];
int d[MAX];

int main(){
  for( int i = 0; i < MAX; i++ ){
    for( int j = 0; j < MAX; j++ ){
      G[i][j] = INF;
    }
  }
  
  cin >> 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;
      G[u][v] = c;
    }
  }

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

  while( 1 ){
    int mincost = INF;
    int u;
    for( int i = 0; i < N; i++ ){
      if( d[i] < mincost && !seen[i] ){
	mincost = d[i];
	u = i;
      }
    }

    if( mincost == INF ) break;
    seen[u] = true;
    
    for( int i = 0; i < N; i++ ){
      if( G[u][i] != INF && !seen[i] ){
	if( G[u][i] + d[u] < d[i] ){
	  d[i] = G[u][i] + d[u];
	}
      }
    }
  }

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

}

C++: #include <iostream>

using namespace std;

const int MAX=100;
const int INFTY=(1<<21);
const int WHITE=0;
const int GRAY=1;
const int BLACK=2;

int M[MAX][MAX];
int n;

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

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

    d[0]=0;

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

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

    return;
}

int main()
{
    cin>>n;

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

    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;
            M[u][v]=c;
        }
    }



    dijkstra();
}

C++: /*
* @Author: Samson
* @Date:   2018-06-14 14:39:51
* @Last Modified by:   Samson
* @Last Modified time: 2018-06-14 15:04:58
*/
//   @URL : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_B
#include<bits/stdc++.h>
#include<algorithm>
#include<queue>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;

int n,color[105],d[105];
vector<pair<int,int> > a[105];

void dijkstra()
{
	priority_queue<pair<int,int> > que;
	memset(color,0,sizeof color);
	memset(d,INF,sizeof d);
	d[0] = 0;
	que.push(make_pair(0,0));
	while(que.size())
	{
		pair<int,int> p = que.top();
		que.pop();
		int u = p.second;
		color[u] = 1;

		if(d[u] < p.first*(-1))	continue;

		for(int i = 0; i < a[u].size(); ++i)
		{
			int v = a[u][i].first;
			if(color[v])	continue;
			if(d[v] > d[u]+a[u][i].second)
			{
				d[v] = d[u]+a[u][i].second;
				que.push(make_pair(d[v]*(-1),v));
			}
		}
	}
	for(int i = 0; i < n; ++i)
		cout<<i<<' '<< (d[i]==INF?-1:d[i])<<'\n';
}
int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	cin>>n;
	int u,k,v,w;
	for(int i = 0; i < n; ++i)
	{
		cin>>u>>k;
		for(int j = 0; j < k; ++j)
		{
			cin>>v>>w;
			a[u].push_back(make_pair(v,w));
		}
	}
	dijkstra();
	return 0;
}