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 <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)));
  vector<bool> is_belong_to_sssp(n, false);

  //dijkstra's algorithm
  while(!pq.empty()){
    // extract node u in V-S that has minimum cost
    const node u = pq.top();
    pq.pop();
    is_belong_to_sssp.at(u.first) = true;

    // 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(!is_belong_to_sssp.at(v.first) && 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)));
      }
    }
  }

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

  return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main(){
	
	int n,aaa;
	cin>>n;
	vector<vector<int>> v(n),c(n);
	vector<int> k(n),ans(n),d(n);
	rep(i,n){
		cin>>aaa>>k[i];
		v[i].resize(k[i]);
		c[i].resize(k[i]);
		rep(j,k[i]){
			cin>>v[i][j]>>c[i][j];
		}
		ans[i]=-1;
		d[i]=INF;
	}
	d[0]=0;
	int m=INF,mi;
	rep(aaaa,n){
		m=INF;
		rep(i,n){
			if(ans[i]==-1 && d[i]<m){
				m=d[i];
				mi=i;
			}
		}
		ans[mi]=m;
		rep(j,k[mi]){
			d[v[mi][j]]=min(ans[mi]+c[mi][j],d[v[mi][j]]);
		}
	}



	rep(i,n){
		cout<<i<<" "<<ans[i]<<endl;
	}
	
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
static const int Max = 10000;
static const int Infty = (1<<20);
static const int W = 0;
static const int G = 1;
static const int B = 2;

int n;
vector<pair<int, int> > adj[Max];

void dijkstra(){
  priority_queue<pair<int, int> > PQ;
  int i, j, u, v, color[Max], d[Max];

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

  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = G;

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

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

    for(j = 0 ; j < adj[u].size() ; j++){
      v = adj[u][j].first;
      if(color[v] == B) 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] = G;
      }
    }
  }

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



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

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

  dijkstra();

  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define llong long long 
typedef pair<long int, int> P;
long int d[10001];
vector<P> edges[10001];
priority_queue<P, vector<P>, greater<P> > q;

void solve(){
	fill_n(d, 10001, 9999999);
	d[0] = 0;
	q.push(P(0,0));

	while(!q.empty()){
		P p = q.top(); q.pop();
		if(d[p.second] < p.first) continue;
		d[p.second] = p.first;

		for(int i = 0; i < edges[p.second].size(); i++){
			P e = edges[p.second][i];
			if(d[e.second] > p.first + e.first){
				d[e.second] = p.first + e.first;
				q.push(P(p.first + e.first, e.second));
			}
		}
	}

	return;
}

int main(){
	int N;
	cin >> N;
	for(int i = 0; i < N; i++){
		int num;
		int howedge;
		cin >> num >> howedge;
		for(int j = 0; j < howedge; j++){
			int to;
			long int cost;
			cin >> to >> cost;
			edges[num].push_back(P(cost, to));
		}
	}

	solve();

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

	return 0;
}