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;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
ll dx[4] = { 0, 1, 0, -1 };
ll dy[4] = { 1, 0, -1, 0 };
static const int INF = 1 << 21;
static const int MOD = 1e9 + 7;
static const int MAX = 10000;
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] = WHITE;
    }

    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[u] + adj[u][j].second < d[v]) {
                d[v] = d[u] + adj[u][j].second;
                // priority_queueはデフォルトで大きい値を優先するため-1を掛ける
                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()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    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<iostream>
#include<climits>
#include<cstdio>
using namespace std;

typedef struct{
  int A[10001];
  int ans;
}node;
node T[10001];
int head=0;
int tail=1;
int n;

void function(int num){
  int *Q=new int[50000];
  Q[0]=0;
  while(true){
    if(head==tail)break;
    num=Q[head++];
    if(head==49999)head=1;
    for(int i=0;i<n;i++){
      if(T[num].A[i]!=-1){
	if(T[i].ans>T[num].ans+T[num].A[i]){
	  T[i].ans=T[num].ans+T[num].A[i];
	  Q[tail++]=i;
	  if(tail==49999)tail=1;
	}
      }
    }
  }
  delete [] Q;
}

int main(){
  int num,son,z,w;
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      T[i].A[j]=-1;
    }
  }


  for(int i=0;i<n;i++){
    scanf("%d%d",&num,&son);
    T[num].ans=INT_MAX;
    for(int j=0;j<son;j++){
      scanf("%d%d",&z,&w);
      T[num].A[z]=w;
      }
  }

  T[0].ans=0;
  function(0);
 
  for(int i=0;i<n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;

#define NIL -1
#define INF 100000000
#define WHITE 1
#define GREY 2
#define BLACK 3
#define N_MAX 10000

int G[N_MAX][N_MAX];
int V[N_MAX];
int V_value[N_MAX];

void init(){
    for(int i = 0; i < N_MAX; i++){
        V[i] = WHITE;
        V_value[i] = INF;
    }

    for(int i = 0; i < N_MAX; i++){
        for(int j = 0; j < N_MAX; j++){
            G[i][j] = NIL;
        }
    }
}

int main(void){
    init();
    int n;
    scanf("%d\n", &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][v] = c;
        }
    }

    priority_queue<pair<int, int>> PQ;

    V[0] = BLACK;
    V_value[0] = 0;
    int new_node = 0;
    
    while(1){
        //printf("new_node: %d\n", new_node);
        for(int i = 0; i < n; i++){
            if(G[new_node][i] != NIL && V[i] == WHITE){
                V_value[i] = min(V_value[i], V_value[new_node] + G[new_node][i]);
                //printf("push %d %d\n",i,  V_value[i]);
                PQ.push(make_pair(V_value[i] * (-1), i));
            }
        }

        int min_value;
        int min_index = NIL;
        while(!PQ.empty()){
            pair<int, int> p = PQ.top(); PQ.pop();
            //printf("pop %d %d\n", p.second, p.first * (-1));
            if(p.first * (-1) == V_value[p.second] && V[p.second] == WHITE){
                min_value = p.first * (-1);
                min_index = p.second;
                break;
            }
        }

        if(min_index == NIL)
            break;

        new_node = min_index;
        V[new_node] = BLACK;
        V_value[new_node] = min_value;
    }
    
    for(int i = 0; i < n; i++){
        printf("%d %d\n", i, V_value[i]);
    }

    return 0;
}

C++: //By Vlgd
#include<bits/stdc++.h>
using namespace std;
const int N=10000;
const int INF=(1<<21);
struct edge{int w,v;};
struct path{int l,v;};
vector<edge>G[N];
int n,vis[N]={0},dis[N];
//priority_queue<path>pq;
path H[10*N];int h=0;
bool isempty(){return h==0;}
void insert(path p){
	int i=++h;
	H[i]=p;
	while(i>1&&H[i].l<H[i/2].l){
		swap(H[i],H[i/2]);
		i/=2;
	}
}
void heapify(int i){
	int les;
	if(i*2<=h&&H[i].l>H[2*i].l) les=2*i;
	else les=i;
	if(i*2+1<=h&&H[les].l>H[i*2+1].l) les=2*i+1;
	if(les!=i){
		swap(H[i],H[les]);
		heapify(les);
	}
}
path extract(){
	path p=H[1];
	H[1]=H[h--];
	if(h>1) heapify(1);
	return p;
} 
//
void dij(int s){
	for(int i=0;i<n;i++) dis[i]=INF;
	path p;
	dis[s]=0;
	p.l=0;p.v=0;
	insert(p);
	while(!isempty()){
		p=extract();
		int u=p.v;
		vis[u]=1;
		for(int i=0;i<G[u].size();i++){
			int v=G[u][i].v;
			int w=G[u][i].w;
			if(!vis[v]){
				if (dis[v]>dis[u]+w){
					p.l=dis[v]=dis[u]+w;
					p.v=v;
					insert(p);
				}
			}
		}
	}
}
int main(){
	scanf("%d",&n);
	int u,deg;edge e;
	for (int i=0;i<n;i++){
		scanf("%d%d",&u,&deg);
		for(int j=0;j<deg;j++){
			scanf("%d%d",&e.v,&e.w);
			G[u].push_back(e);
		}
	}
	dij(0);
	for (int i=0;i<n;i++) printf("%d %d\n",i,dis[i]);
	return 0;
}