Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }
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 = i
	pq[j].index = j
}
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
}

type Q struct {
	pq *PriorityQueue
	ds map[int]int // It has always same value as pq. For random access.
}

func newQ() *Q {
	pq := new(PriorityQueue)
	heap.Init(pq)
	return &Q{
		pq: pq,
		ds: make(map[int]int),
	}
}
func (q *Q) pop() (u, d int) {
	item := heap.Pop(q.pq).(*Item)
	delete(q.ds, item.value)
	return item.value, item.priority
}
func (q *Q) get(u int) (int, bool) { // returns d
	d, ok := q.ds[u]
	return d, ok
}
func (q *Q) set(u, d int) {
	q.ds[u] = d
	for _, item := range *q.pq {
		if item.value != u {
			continue
		}

		item.priority = d
		heap.Fix(q.pq, item.index)
		return
	}
	heap.Push(q.pq, &Item{
		priority: d,
		value:    u,
	})
}
func (q *Q) len() int {
	return len(q.ds)
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := newQ()
	q.set(0, 0)
	visited := make(map[int]struct{})
	out := make([]int, n)

	for q.len() > 0 {
		u, d := q.pop()
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			dd := d + c
			if d, ok := q.get(uu); !ok || d > dd {
				q.set(uu, dd)
			}
		}

		out[u] = d
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, d)
	}
}

C++: //ALDS1_12_cにて、AC
#include<bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
int main(){
  int n;
  cin>>n;
  vector<pair<int,int> > adj[10005]; //重み付き有向グラフの隣接リスト表現
  //隣接リストの作成
  int u,k;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
      int v,c;
      cin>>v>>c;
      adj[u].emplace_back(v,c);
    }
  }

  //ここからダイクストラ
  priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
  vector<bool> fg(n+1000);
  vector<int> cost(n+1000);
  //初期化
  for(int i=0;i<n;i++){
    cost[i]=INF;
    fg[i]=false;
  }
  //出発地点はコスト0
  cost[0]=0;
  pq.emplace(0,0);
 
  //最短経路を検出
  while(!pq.empty()){
    auto now=pq.top(); pq.pop();
    int u=now.second;
    fg[u]=true;

    //最小値を取り出し、それが最短でなければ無視
    if(cost[u]<now.first) continue;
    
    //次の辺へ移動
    for(int j=0;j<adj[u].size();j++){
      int v=adj[u][j].first;
      if(fg[v]) continue;
      if(cost[v]>cost[u]+adj[u][j].second){
        cost[v]=cost[u]+adj[u][j].second;
        pq.emplace(cost[v],v);
      }
    }
  }

  for(int i=0;i<n;i++){
    cout<<i<<" "<<(cost[i]==INF ? -1 : cost[i])<<'\n';
  }
  return(0);
}

C++: #include <stdio.h>
#include <queue>
#include <vector>
#include <functional>

#define N 100
#define INF 100000000
#define MIN(a,b) ((a>b)?(b):(a))

using namespace std;


class list {
public:
	int cost;
	int node;
	int from;
	list(void) {
		cost = -1;
		node = -1;
		from = -1;
	}
};

bool operator<(const list& y,const list& x) {
	return (y.cost < x.cost);
};
bool operator>(const list& y,const list& x) {
	return (y.cost>x.cost);
};

list m[N][N];
bool visited[N];
int sum = 0;
int dist[N];

int main(void) {
	priority_queue<list, vector<list>, greater<list> > pq;
	int n;
	int now;
	int work;
	int k[N];
	int u;
	scanf("%d", &n);
	for (int i = 0; i<n; i++) {
		visited[i] = false;
		dist[i] = INF;
		scanf("%d", &u);
		scanf("%d", &k[u]);
		for (int j = 0; j < k[u]; j++) {
			int a, b;
			scanf("%d", &a);
			scanf("%d", &b);
			m[u][j].cost = b;
			m[u][j].node = a;
		}
	}
	now = 0;
	dist[now] = 0;
	while (1) {
		//printf("I'm in %d\n", now);
		bool f = visited[now];
		visited[now] = true;
		for (int j = 0; j<k[now] && !f; j++) {
				list a;
				a.cost = m[now][j].cost;
				a.node = m[now][j].node;
				a.from=now;
				pq.push(a);
				//printf("I pushed %d %d\n", a.node, a.cost);
		}
		//system("pause");
		list x;
		while (!pq.empty()) {
			if (visited[(pq.top()).node] == false || visited[(pq.top()).node]==true) {
				x = pq.top();
				//printf("I got %d %d %d %d\n", x.node, x.cost,sum,x.from);
				pq.pop();
				now = x.node;
				sum += x.cost;
				dist[now] = MIN(dist[x.from]+x.cost,dist[now]);
				break;
			}
			else pq.pop();
		}
		if (x.node == -1)break;
	}
	for (int i = 0; i < n; i++) printf("%d %d\n", i, dist[i]);
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+7;
const int INF = 1e6+7;
vector<pair<int, int> > adj[MAXN];
priority_queue<pair<int, int> > q;

int d[MAXN];
bool visit[MAXN];
int n;
set<int> s;
void djas() {
    d[0] = 0;
    for(int i = 1; i < n; i++) {
        d[i] = INF;
    }
    q.push(make_pair(0, 0));
    while(!q.empty()) {
        pair<int, int> s1 = q.top(); q.pop();
        int u = s1.second;
        visit[u] = true;
        for(int j = 0; j < adj[u].size(); j++) {
            int v = adj[u][j].second;
            if(visit[v]==false) {
                if(d[v]>d[u]+adj[u][j].first) {
                    d[v] = d[u]+adj[u][j].first;
                    q.push(make_pair((-1)*d[v],v));
                }
            }
        }
    }
}
int main() {
//    freopen("in.txt", "r", stdin);
    memset(visit, false, sizeof(visit));
    scanf("%d", &n);
    int v;
    int c;
    int id;
    int degree;
    for(int i = 0; i < n; i++) {
        scanf("%d", &id);
        scanf("%d", &degree);
        for(int j = 0; j < degree; j++) {
            scanf("%d", &v);
            scanf("%d", &c);
            adj[id].push_back(make_pair(c,v));
        }
    }
    djas();
    for(int i = 0; i < n; i++) {
        printf("%d %d\n", i, d[i]);
    }

    return 0;
}