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>
#include<limits.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)

#define INF INT_MAX

#define MAX 10000
vector<vector<pair<int, int>>> adj_list(MAX);

void dijkstra(int start_node, int node_size){
    vector<int> visited(node_size, 0);
    vector<int> cost_table(node_size, INF);

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
    
    visited.at(start_node) = 1;
    cost_table.at(start_node) = 0;
    //queueの初期化 startノードに隣接するノードとそのコストをpush
    // cout << "queue pushed : ";
    int i = 0;
    while(adj_list.at(start_node).at(i).first != -1){
        pair<int, int> x = make_pair(adj_list.at(start_node).at(i).second, adj_list.at(start_node).at(i).first);
        q.push(x);
        // cout << x.first << " " << x.second << "  ";
        cost_table.at(x.second) = x.first;
        i++;
    }
    // cout << endl;

    // cout << "cost_table ====" << endl;
    // rep(i, node_size) cout << i << " " << cost_table.at(i) << endl;
    // cout << endl;


    while(q.size() != 1){
        pair<int, int> Nnode = q.top(); q.pop();
        int nearestNode = Nnode.second;

        visited.at(nearestNode) = 1;

        int i = 0;

        while(adj_list.at(nearestNode).at(i).first != -1){
            int v = adj_list.at(nearestNode).at(i).first; // 構築中の木に隣接するノードvと、追加したノード(nearestNode)からのコストc
            int c = adj_list.at(nearestNode).at(i).second;// 

            // cout << "nearest node : " << nearestNode << " " << v << " " << c << endl;

            if(visited.at(v) == 1){
                // cout << "old" << endl;
                i++;
                continue;
            }else{
                // cout << "new : cost_table " << cost_table.at(v) << " " <<  cost_table.at(nearestNode) << endl;

                if(cost_table.at(v) > cost_table.at(nearestNode) + c){
                    cost_table.at(v) = cost_table.at(nearestNode) + c;
                    q.push(make_pair(cost_table.at(v), v));
                    // cout << "queue pushed : cost " << cost_table.at(v) << " node " << v << endl; 
                }
            }
            i++;
        }
    }

    rep(i, node_size){
        cout << i << " " << cost_table.at(i) << endl;
    }
}



int main(){
    int n; cin >> n;

    //　隣接リストの作成
    rep(i, n){
        int from, k; cin >> from >> k;

        rep(j, k){
            int to, weight; cin >> to >> weight;
            adj_list.at(from).push_back(make_pair(to, weight));
        }
    }

    //番兵の挿入
    rep(i, n){
        adj_list.at(i).push_back(make_pair(-1, -1));
    }

    // cout << endl;
    // rep(i, n){
    //     int j = 0;
    //     while(adj_list.at(i).at(j).first != -1){
    //         cout << adj_list.at(i).at(j).first << " " << adj_list.at(i).at(j).second << " ";
    //         j++;
    //     }
    //     cout << endl;
    // }

    dijkstra(0, n);

}

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

typedef vector<pair<int, int> > adj;

struct current_cost
{
	int index, cost;
	bool operator < (const current_cost &cur) const
	{
		return cost < cur.cost;
	}
};

current_cost init(int i, int c)
{
	current_cost cur;
	cur.index = i, cur.cost = c;
	return cur;
}

int main()
{
	int i, j, k, n, m, next, cost;
	scanf("%d", &n);
	adj *adlist;
	adlist = new adj [n];

	for(k = 0; k < n; k++){
		scanf("%d %d", &i, &m);
		for(j = 0; j < m; j++){
			scanf("%d %d", &next, &cost);
			adlist[i].push_back(make_pair(next, cost));
		}
	}
	
	int *d;
	d = new int [n];
	d[0] = 0;
	for(i = 1; i < n; i++){
		d[i] = 200000;
	}

	priority_queue<current_cost> C;
	current_cost cur = init(0, 0);
	C.push(cur);

	int size;
	bool *check;
	check = new bool [n];
	for(i = 0; i < n; i++){ check[i] = false; }

	while(!C.empty())
	{
		cur = C.top(); C.pop();
		if(check[cur.index]) continue;
		check[cur.index] = true;

		i = cur.index;
		size = adlist[i].size();
		for(j = 0; j < size; j++){
			k = adlist[i][j].first;
			if(d[i] + adlist[i][j].second < d[k]){
				d[k] = d[i] + adlist[i][j].second;
				C.push(init(k, d[k]));
			}
		}
	};
	for(i = 0; i < n; i++){ printf("%d %d\n", i, d[i]); }

	return 0;
}

C++: #include<stdio.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
int maxn=1<<30;
int n;
int color[10005];
int d[10005];
vector<pair<int, int> > bian[10005];
int f()
{
    priority_queue<pair<int ,int > > pq;
    for(int i=0;i<n;i++)
    {
        d[i]=maxn;
    }
    d[0]=0;
    pq.push(make_pair(0,0));
    while(!pq.empty())
    {
        pair<int ,int > x=pq.top();
        pq.pop();
        int u=x.second;
        color[u]=1;
        if(d[u]<x.first *(-1)) continue;
        for(int i=0;i<bian[u].size();i++)
        {
            int v=bian[u][i].first;
            if(color[v]==1) continue;
            if(d[v]>(d[u]+bian[u][i].second))
            {
                d[v]=d[u]+bian[u][i].second;
                pq.push(make_pair(d[v]*(-1),v));
                color[v]=0;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%d ",i);
        printf("%d\n",(d[i]==maxn)?-1:d[i]);
    }
}
int main()
{
    int k,u,c,g;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&k,&u);
        for(int j=0;j<u;j++)
        {
            scanf("%d%d",&c,&g);
            bian[k].push_back(make_pair(c,g));
        }
    }
    f();
    return 0;
}