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>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long int;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)

#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;

static const int MAX = 100;
static const int White = 0;
static const int Gray = 1;
static const int Black = 2;


int n, M[MAX][MAX];
int d[MAX], p[MAX],color[MAX];

void dijkstra() {
	rep(i, n) {
		color[i] = White;
		d[i] = INF;
		p[i] = -1;
	}
	d[0] = 0;
	while (true) {
		int u, mincost = INF;
		rep(i, n) {
			if (color[i] != Black and d[i] < mincost) {
				u = i;
				mincost = d[i];
			}
		}
		if (mincost == INF) break;
		color[u]  = Black;
		int v;
		rep(v, n) {
			if (color[v] != Black and M[u][v] != INF) {
				if (d[u] + M[u][v] < d[v]) {
					d[v] = d[u] + M[u][v];
					p[v] = u;
					color[v] = Gray;
				}
			}
		}
	}
	rep(i, n) {
		cout << i << ' ' << d[i] << '\n';
	}
}

signed main() {
	ios::sync_with_stdio(false);
	int i, j, k;
	cin >> n;
	rep(i, n) {
		rep(j, n) {
			M[i][j] = INF;
		}
	}
	rep(i, n) {
		int u, k;
		cin >> u >> k;
		rep(j, k) {
			int c,v;
			cin >> v >> c;
			M[u][v] = c;
		}
	}
	dijkstra();
	return 0;
}

C++: #include<stdio.h>

#define N 103
#define INF 10000000
#define NIL -1
#define BLACK 1

int G[N][N];

int d[N],pi[N],color[N];
int ci[N];
int v[N];

int num;

void initialize(int);
void dij(int);

int main(){
  int i,j;
  int k,u;
  int start=0;

  scanf("%d",&num);

  for(i=0;i<num;i++){
    for(j=0;j<num;j++){
      G[i][j] = NIL;
    }
  }
  

  for(i=0;i<num;i++){
    scanf("%d%d",&u,&k);
      for(j=0;j<k;j++){
	scanf("%d%d",&v[j],&ci[j]);
	G[u][v[j]] = ci[j]; 
      }
  }

  dij(start);

  /*for(i=0;i<num;i++){
    for(j=0;j<num;j++){
      printf("%d ",G[i][j]);
    }
    printf("\n");
  }
  */

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

}

void initialize(int s){
  int v;
  for(v=0;v<num;v++){
    d[v] = INF;
    pi[v] = NIL;
  }

  d[s] = 0;
}

void dij(int s){
  int mincost;
  int i,v,u;
  
  initialize(s);

  while(1){
    
    mincost = INF;

    for(i=0;i<num;i++){
      
      if(color[i] != BLACK && d[i] < mincost){
	mincost = d[i];
	u = i;

      }
    }

    //printf("u=%d\n", u);
    if(mincost == INF) break;

    color[u] = BLACK;
    
    for(v=0;v<num;v++){
      
      if(G[u][v] == -1) continue;
      if(color[v] != BLACK && d[u] + G[u][v] < d[v]){
	pi[v]= u;
	d[v] = d[u] + G[u][v];
      }
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <queue>
// #include <string>
using namespace std;

vector<string> split(string s, string delimiter)
{
    int pos_start = 0, pos_end, delim_len = delimiter.length();
    string token;
    vector<string> res;

    while ((pos_end = s.find(delimiter, pos_start)) != string::npos)
    {
        token = s.substr(pos_start, pos_end - pos_start);
        pos_start = pos_end + delim_len;
        res.push_back(token);
    }

    res.push_back(s.substr(pos_start));
    return res;
}

vector<int> dijkstra(int n, vector<vector<int>> adj_matrix)
{
    vector<int> d(n, 1e9);
    vector<int> prev(n);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q;

    d[0] = 0;
    for (int i = 0; i < n; i++)
    {
        Q.push(make_pair(d[i], i));
    }

    while (!Q.empty())
    {
        int u = Q.top().second;
        // int c = Q.top().first;
        Q.pop();

        for (int i = 0; i < n; i++)
        {
            if (adj_matrix[u][i] > 0)
            {
                int tmp = d[u] + adj_matrix[u][i];
                if (tmp < d[i])
                {
                    d[i] = tmp;
                    prev[i] = u;
                    Q.push(make_pair(d[i], i));
                }
            }
        }
    }

    return d;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    cin.ignore();
    vector<vector<int>> adj_matrix;
    adj_matrix.resize(n);
    for (int i = 0; i < adj_matrix.size(); i++)
    {
        adj_matrix[i].resize(n);
    }

    for (int i = 0; i < n; i++)
    {
        string s;
        getline(cin, s);
        vector<string> tmp = split(s, " ");
        int u = stoi(tmp[0]);
        int k = stoi(tmp[1]);

        for (int j = 2; j < tmp.size(); j += 2)
        {
            int v = stoi(tmp[j]);
            adj_matrix[u][v] = stoi(tmp[j + 1]);
        }
    }

    vector<int> ans = dijkstra(n, adj_matrix);
    for (int i = 0; i < n; i++)
    {
        cout << i << " " << ans[i] << endl;
    }

    return 0;
}