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<vector>
#include<algorithm>
#include<queue>
using namespace std;
static const int INFTY=(1<<21);
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX 10002
int n,M[MAX][MAX],color[MAX],d[MAX];
vector <pair<int,int> > adj[MAX];
void dijkstra(){
    priority_queue <pair<int,int> > PQ;
    d[0]=0;
    color[0]=GRAY;
    PQ.push(make_pair(0,0));
    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;

        for(int j=0;j<adj[u].size();j++){
            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;
            }
        }
    }
    for(int i=0;i<n;i++){
        cout<<i<<" "<<(d[i]==INFTY ? -1:d[i])<<endl;
    }
}
int main(){
   int u,k,v,c;
   cin>>n;
   for(int i=0;i<n;i++){
       cin>>u>>k;
       for(int j=0;j<k;j++){
           cin>>v>>c;
           adj[u].push_back(make_pair(v,c));
       }
   }
   for(int i=0;i<n;i++){
       color[i]=WHITE;
       d[i]=INFTY;
   }
   dijkstra();
   return 0;
}

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

typedef pair<int, int> node;
typedef vector<node> Adj_list;
typedef vector<Adj_list> Adj_lists;

class less_cost{
public:
  bool operator()(const pair<int, int> lhs, const pair<int, int> rhs) const{
    return lhs.second < rhs.second;
  }
};

int main(){

  int n;
  scanf("%d", &n);

  Adj_lists adj_lists(n);

  // read weigts
  for(int i=0; i<n; i++){
    int u, k;
    scanf("%d %d", &u, &k);
    for(int j=0; j<k; j++){
      int v,c;
      scanf("%d %d", &v, &c);
      adj_lists.at(u).push_back(node(v, c));
    }
  }

  // initialize cost
  vector<int> d(n, INT_MAX);
  d.at(0) = 0;
  priority_queue<node, vector<node>, less_cost> pq;
  pq.push(node(0, d.at(0)));

  //dijkstra's algorithm
  while(!pq.empty()){
    // extract node u in V-S that has minimum cost
    node& u = pq.top();

    // if u's cost is larger than d[u],
    // than ignore this one
    if(u.second < d.at(u.first)) continue;

    // update node in V-S and adjacent to u
    for(const node& v : adj_lists.at(u.first)){
      if(d.at(u.first) + v.second < d.at(v.first)){
        d.at(v.first) = d.at(u.first) + v.second;
        pq.push(node(v.first, d.at(v.first)));
      }
    }

    pq.pop();
  }

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

  return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100

using Weight = int;
struct Edge {
	int s; //source
	int d; //destination
	Weight w; //weight
	Edge(int s = 0, int d = 0, int w = INF) : s(s), d(d), w(w) {};
};
using Edges = vector<Edge>;
//???????????\???
//g[u].push_back(Edge(u, v, c));
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

//?????¢?´¢, ??¢?´¢???, ??¢?´¢???
enum { WHITE, GRAY, BLACK };

//???????§????????????? (Dijkstra)
//?????????: ??????????????§????????¢
vector<Weight> dijkstra(Graph g, int s) {
	typedef pair<Weight, int> State; //?§?????????????????°?????????? ??????id
	priority_queue<State, vector<State>, greater<State> >PQ;
	vector<int> color(g.size(), WHITE);
	vector<Weight> dist(g.size(), INF); //distance
	dist[s] = 0; PQ.push(State(0, s)); color[s] = GRAY; //?§????
	while (!PQ.empty()) {
		Weight d; int v;
		tie(d, v) = PQ.top(); PQ.pop(); //dist???????°??????¨??????????´????????????????	
		if (dist[v] < d) continue; //?????§???????????§???????????°??????
		color[v] = BLACK;
		for (auto e : g[v]) { //??£??\????????????????????¨????????????		
			if (color[e.d] == BLACK)continue; //??¢?´¢????????????
			if (dist[e.d] > dist[v] + e.w) {
				dist[e.d] = dist[v] + e.w;
				PQ.push(State(dist[e.d], e.d));
				color[e.d] = GRAY;
			}
		}
	}
	return dist;
}

int main() {
	int n; cin >> n;
	Graph g(n);
	for (int i = 0; i < n; i++) {
		int u, k; scanf("%d%d", &u, &k);
		for (int j = 0; j < k; j++) {
			int v, c; scanf("%d%d", &v, &c);
			g[u].push_back(Edge(u, v, c));
		}
	}
	vector<Weight> ans = dijkstra(g, 0);
	for (int i = 0; i < n; i++) {
		printf("%d %d\n", i, ans[i]);
	}
	return 0;
}