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

	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++: /*???????????????????????¢?????´???????????§?????????????°???¨??????????????????????????¢?´¢??????*/
/*????????????????????\????????¨???????????¨??§???????????????O((|E|+|V|)log|V|)?????§??????*/
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep0(i,i0,n) for(int i = i0;i<=n;i++)
using namespace std;
#define inf (1<<20)
#define MAX 100000

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

int n;
vector<pair<int, int> > adj[MAX]; //??£??\?????????

void Dijkstra(){
    priority_queue<pair<int, int> > PQ;
    int color[MAX];
    int D[MAX];
    rep(i, n){
        D[i]=inf;
        color[i]=inf;
    }
    D[0]=0;
    PQ.push(make_pair(0, 0));
    color[0]=Gray;

    while(!PQ.empty()){
        pair<int, int> f=PQ.top(); PQ.pop();
        int u=f.second;
        color[u]=Black;

        //????°??????????????????????????????§???????????°????????????
        if(D[u] < f.first * (-1) ) continue;

        rep(j, adj[u].size()){
            int v =adj[u][j].first;
            if(color[v]==Black) continue;
            if(D[v] > D[u] + adj[u][j].second){
                D[v] = D[u] + adj[u][j].second;
                //????????????????????\????????§?????????????????????????????§????????????
                PQ.push(make_pair(D[v] * (-1), v));
                color[v]=Gray;
            }
        }
    }

    rep (i,n) cout << i << " " << ( D[i] == inf ? -1 : D[i]) << endl;
}

int main(void){
    int k,u,v,c;
    cin >> n;

    rep(i, n){
        cin >> u >>k;
        rep(j, k){
            cin >> v >> c;
            adj[u].push_back(make_pair(v, c));
        }
    }

    Dijkstra();

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1000 * 100000;

int main(void){
  int n;
  cin >> n;
  vector< vector<pair<int, int> > > g(n);
  for(int i=0; i < n; i++){
    int v; cin >> v;
    int k; cin >> k;
    for(int j=0; j < k; j++){
      int v0, c; cin >> v0 >> c;
      g[v].push_back(make_pair(v0, c));
    }
  }
  vector<ll> d(n, INF);
  vector<bool> used(n, false);
  priority_queue<pair <int, int> > pq;
  d[0] = 0;

  pq.push(make_pair(0, 0));

  while(!pq.empty()){
    pair<int, int> f = pq.top(); pq.pop();
    int u = f.second;
    used[u] = true;
    if(d[u] < f.first * (-1)) continue;

    for(int i=0; i < g[u].size(); i++){
      int v = g[u][i].first;
      if(used[v] == true) continue;
      if(d[v] > d[u] + g[u][i].second){
        d[v] = d[u] + g[u][i].second;
        pq.push(make_pair(d[v] * (-1), v));
      }
    }
  }
  for(int i=0; i < n; i++){
    cout << i << " " << d[i] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define MAX 100
#define INF (1<<30)

using namespace std;
enum state{
  BLACK,
  WHITE
};

int main(){
  vector<int> v[MAX];
  vector<int> c[MAX];
  state s[MAX];
  int d[MAX];
  int n; cin>>n;

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

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

  vector<int> blacklist;
  blacklist.push_back(0);

  while(1){
    int& last = blacklist.back();
    for(int i = 0;i<v[last].size();i++){
      if(d[v[last][i]]>d[last]+c[last][i]){
        d[v[last][i]]=d[last]+c[last][i];
      }
    }

    int minv=-1;
    int mind=INF;
    for(int b:blacklist){
      for(int i=0;i<v[b].size();i++){
        if(s[v[b][i]]!=BLACK && d[v[b][i]]<mind){
          minv=v[b][i];
          mind=d[v[b][i]];
        }
      }
    }
    if(minv!=-1){
      s[minv]=BLACK;
      blacklist.push_back(minv);
    }else{
      break;
    }
  }

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