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<bits/stdc++.h>
using namespace std;

const int INF=(1<<20);

int n;
vector<pair<int,int> >l[10000];
 
void dijkstra();

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

  return(0);
}

void dijkstra(){
  priority_queue<pair<int,int> >pq;
  int sn[10000];
  int d[10000];
  for(int i=0;i<n;i++){
    d[i]=INF;
    sn[i]=0;
  }
  
  d[0]=0;
  pq.push(make_pair(0,0));

  while(!pq.empty()){
    pair<int,int> f=pq.top();
    pq.pop();
    int u=f.second;
    sn[u]=1;

    if(d[u]<f.first*(-1))continue;
      
      for(int j=0;j<l[u].size();j++){
	int v=l[u][j].first;
	if(sn[v]==1)continue;

	if(d[v]>d[u]+l[u][j].second){
	  d[v]=d[u]+l[u][j].second;
	  pq.push(make_pair(d[v]*(-1),v));
	}

	
      }
    
  }

  for(int i=0;i<n;i++){
    cout<<i<<" ";
    if(d[i]==INF)cout<<"-1"<<endl;
    else cout<<d[i]<<endl;
  }
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const int W = 0, B = 1;
vector<vector<pair<int, int>>>adj;
int n;
void dijkstra() {
	priority_queue<pair<int, int>>pq;
	vector<int>color(n, W), d(n, INF);
	d[0] = 0;
	pq.push(make_pair(0, 0));

	while (!pq.empty()) {
		pair<int, int>f(pq.top()); pq.pop();// d[v], v
		color[f.second] = B;

		if (d[f.second] < (-1)*f.first)continue;
		
		rep(i, 0, adj[f.second].size()) {
			pair<int, int>v(adj[f.second][i]);
			if (color[v.first] != B&&d[f.second] + v.second < d[v.first]) {
				d[v.first] = d[f.second] + v.second;
				pq.push(make_pair(d[v.first] * (-1),v.first));
			}
		}
	}
	rep(i, 0, n) {
		cout << i << " " << d[i] << endl;
	}
}
signed main() {
	cin >> n; adj = vector<vector<pair<int, int>>>(n, vector<pair<int, int>>());
	rep(i, 0, n) {
		int u, k; cin >> u >> k;
		rep(j, 0, k) {
			int v, c; cin >> v >> c;
			adj[u].push_back(make_pair(v, c));

		}
	}
	dijkstra();
	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <queue>
#define MAX_N (10010)
#define MAX_C (100010)
#define INF (MAX_N * MAX_C)

using namespace std;
typedef pair<int, int> P;

vector<P> G[MAX_N];
int d[MAX_N];
int par[MAX_N];
bool arrived[MAX_N];

int main()
{
    int n;
    scanf("%d", &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( make_pair( v, c ) );
	}
    }

    for ( int i = 0; i < n; i++ ) {
	d[i] = INF;
    }
    d[0] = 0;
    par[0] = -1;

    priority_queue<P, vector<P>, greater<P> > PQ;
    PQ.push( make_pair( 0, 0 ) );

    while ( !PQ.empty() ) {
	P p = PQ.top(); PQ.pop();
	if ( p.first > d[p.second] ) continue;
	int u = p.second;
	arrived[u] = true;

	for ( int i = 0; i < G[u].size(); i++ ) {
	    P v = G[u][i];
	    if ( !arrived[v.first] ) {
		if ( d[v.first] > d[u] + v.second ) {
		    d[v.first] = d[u] + v.second;
		    PQ.push( make_pair( d[v.first], v.first ) );
		    par[v.first] = u;
		}
	    }
	}
    }

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

C++: #include <cstdio>
#include <cstring>
#include <climits>
#include <queue>
#include <map>
using namespace std;

typedef map<int, int>::iterator mitr;

map<int, int> w[10000];
int d[10000];
char pos[10000];

priority_queue<pair<int,int> > others;
pair<int,int> p;

void findPath(){
    p = others.top();
    others.pop();
    while(pos[p.second]){
        p = others.top();
        others.pop();
    }
    pos[p.second]=1;

    int tmpu = p.second;
    int tmpd = -p.first;

    for(mitr i=w[tmpu].begin();i!=w[tmpu].end();i++){
        if(tmpd + i->second < d[i->first]){
            d[i->first] = tmpd + i->second;
            others.push(make_pair(-d[i->first],i->first));
        }
    }
}

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

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

    memset(pos,0,sizeof(char)*100);
    for(i=0;i<n;i++)d[i]=INT_MAX;
    d[0] = 0;
    others.push(make_pair(0,0));
    for(i=0;i<n;i++)findPath();
    for(i=0;i<n;i++)printf("%d %d\n",i,d[i]);

    return 0;
}