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++: #include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;

typedef pair<int,int> vec;

vector<vec> adj[10000];
int d[10000];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=0; i<n; i++) d[i]=numeric_limits<int>::max();
    for(int i=0; i<n; i++) {
        int u,k;
        cin>>u>>k;
        adj[u].reserve(k);
        for(int j=0; j<k; j++) {
            int v,c;
            cin>>v>>c;
            adj[u].push_back(vec(v,c));
        }
    }
    priority_queue<vec, vector<vec>, greater<vec> >q;
    q.push(vec(0,0));
    d[0]=0;
    while(!q.empty()) {
        vec e=q.top();
        q.pop();
        int id=e.first;
        int cost=e.second;
        if(d[id]<cost) continue;
        for(int i=0, an=adj[id].size(); i<an; i++) {
            int adj_id=adj[id][i].first;
            int adj_cost=adj[id][i].second+cost;
            if(d[adj_id] <= adj_cost) continue;
            d[adj_id]=adj_cost;
            q.push(vec(adj_id, adj_cost));
        }
    }
    for(int i=0; i<n; i++) cout<<i<<' '<<d[i]<<'\n';
    return 0;
}

C++: #include <cstdio>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <functional>
using namespace std;

typedef pair<int,int> pii;

int main(){
	int n;
	scanf("%d", &n);
	vector<vector<pii> > G(n);
	for(int i = 0; i < n; ++i){
		int u, k, v, c;
		scanf("%d%d", &u, &k);
		for(int j = 0; j < k; ++j){
			scanf("%d%d", &v, &c);
			G[u].push_back(pii(c, v));
		}
	}

	vector<int> d(1 << 30);
	d[0] = 0;
	priority_queue<pii,vector<pii>,greater<pii> > pq;
	pq.push(pii(0, 0));
	while(!pq.empty()){
		int c = pq.top().first;
		int u = pq.top().second;
		if(d[u] != c){ continue; }
		for(int i = 0; i < G[u].size(); ++i){
			int b = c + G[u][i].first;
			int v = G[u][i].second;
			if(d[v] > b){
				d[v] = b;
				pq.push(pii(b, v));
			}
		}
	}
	
	for(int i = 0; i < n; ++i){
		printf("%d %d\n", i, d[i]);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
vector<vector<P> > G;
signed main(){
  int n;
  cin>>n;
  int INF=1LL<<55LL;
  G.resize(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].push_back(P(v,c));
    }
  }
  int d[n];
  for(int i=0;i<n;i++) d[i]=INF;
  d[0]=0;

  priority_queue<P,vector<P>,greater<P> > q;
  d[0]=0;
  q.push(P(0,0));

  while(!q.empty()){
    P p=q.top();q.pop();
    int u=p.second,c=p.first;
    //cout<<u<<" "<<c<<endl;
    if(d[u]<c) continue;
    for(P e:G[u]){
      int v=e.first,w=e.second;
      if(d[v]>d[u]+w){
	d[v]=d[u]+w;
	q.push(P(d[v],v));
	//cout<<v<<":"<<d[v]<<endl;
      }
    }
  }
  
  for(int i=0;i<n;i++) cout<<i<<" "<<d[i]<<endl;
  return 0;
}

C++: #include <stdio.h>
#define N 10000
#define INF 100001
#define WHITE 0
#define GRAY 1
#define BLACK 2

int G[N][N], n;

void dijkstra(void) {
  int d[N], color[N];
  int i, u, mincost;

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

  while(1){
    mincost = INF;
    for(i = 0; i < n; i++) {
      if(color[i] != BLACK && d[i] < mincost){
        mincost = d[i];
        u = i;
      }
    }
    if(mincost == INF) break;
    color[u] = BLACK;
    for(i = 0; i < n; i++) {
      if(color[i] != BLACK && G[u][i] != INF && d[u] + G[u][i] < d[i]) {
        d[i] = d[u] + G[u][i];
      }
    }
  }
  for(i = 0; i < n; i++) {
    printf("%d %d\n", i, d[i]);
  }
}

int main(){
  int i, j, u, v, k, c;

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

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