Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const BIG_COST = 200000

type Path struct {
	node int
	cost int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)

	// 各頂点につながる頂点一覧を得るための隣接リスト
	graph := make([][]Path, nNodes)

	for i := 0; i < nNodes; i++ {
		u := scanInt(sc)
		k := scanInt(sc)
		graph[i] = make([]Path, k)
		for d := 0; d < k; d++ {
			graph[u][d] = Path{
				node: scanInt(sc),
				cost: scanInt(sc),
			}
		}
	}

	// 0 から各頂点への最小コストを保存する配列
	minCosts := make([]int, nNodes)

	minCosts[0] = 0
	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	// 各頂点が調査済みかどうかを保持する配列
	checked := make([]bool, nNodes)

	// 最小コストのパスを高速に取得するためのヒープ。
	// コストが小さいものほどルートに来るようにする。
	heap := NewHeap(func(a, b Path) bool {
		return a.cost < b.cost
	})

	heap.insert(Path{node: 0, cost: 0})
	for heap.size > 0 {
		u := heap.extractMax()

		// 隣接行列を使っていた時とは違い、ヒープには
		// 同じノードへのパスが複数含まれうる。そのため既によりコストの低い
		// パスが見つかっている場合は無視する。
		if minCosts[u.node] < u.cost {
			continue
		}

		checked[u.node] = true

		for _, v := range graph[u.node] {
			cost := v.cost + u.cost
			if !checked[v.node] && cost < minCosts[v.node] {
				minCosts[v.node] = cost
				heap.insert(Path{node: v.node, cost: cost})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i, cost := range minCosts {
		fmt.Fprintf(w, "%d %d\n", i, cost)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

// --- From ALDS1_9_C: Priority Queue

type Heap struct {
	size   int
	heap   []Path
	isHigh func(a, b Path) bool
}

func NewHeap(isHigh func(a, b Path) bool) Heap {
	heap := make([]Path, 1) // 先頭は不使用
	return Heap{size: 0, heap: heap, isHigh: isHigh}
}

func (h *Heap) insert(v Path) {
	h.size += 1
	if h.size > len(h.heap)-1 {
		h.heap = append(h.heap, v)
	} else {
		h.heap[h.size] = v
	}

	for i := h.size; i > 1; {
		parent := i / 2
		if h.isHigh(h.heap[parent], h.heap[i]) {
			break
		}
		h.heap[i], h.heap[parent] = h.heap[parent], h.heap[i]
		i = parent
	}
}

func (h *Heap) extractMax() Path {
	if h.size <= 0 {
		panic("cannot extract from empty heap")
	}

	max := h.heap[1]
	h.heap[1] = h.heap[h.size]
	h.size -= 1
	h.maxHeapify(1)

	return max
}

func (h *Heap) maxHeapify(idx int) {
	left := idx * 2
	right := left + 1
	largest := idx

	if left <= h.size {
		if h.isHigh(h.heap[left], h.heap[idx]) {
			largest = left
		}
		if right <= h.size && h.isHigh(h.heap[right], h.heap[largest]) {
			largest = right
		}
	}

	if largest != idx {
		h.heap[idx], h.heap[largest] = h.heap[largest], h.heap[idx]
		h.maxHeapify(largest)
	}
}

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

#define N 10001
#define INF 1500001 
int n;

vector<pair<int, int>> A[N];
int color[N];
int d[N];

void dijkstra(int root) {
    priority_queue<pair<int, int>> Q;
    Q.push(make_pair(0, root));

    int treeSize = 1;
    while(!Q.empty()) {
        pair<int, int> addedInfo = Q.top();
        Q.pop();
        int added = addedInfo.second;

        if (color[added] != 1) {
            d[added] = -1 * addedInfo.first;
            color[added] = 1;

            for (vector<pair<int, int>>::iterator it = A[added].begin(); it != A[added].end(); it++) {
                pair<int, int> node_pair = *it;
                if (color[node_pair.first] == 0 && (d[added] + node_pair.second) < d[node_pair.first]) {
                    d[node_pair.first] = d[added] + node_pair.second;
                    Q.push(make_pair(-1 * d[node_pair.first], node_pair.first));
                }
            }
        }
    }
}

int main() {
    cin >> n;

    int w;
    int node;
    int k;
    int ad;
    for (int i = 0; i < n; i++) {
        color[i] = 0;
        d[i] = INF;

        cin >> node >> k;
        for (int j = 0; j < k; j++) {
            cin >> ad >> w;
            A[node].push_back(make_pair(ad, w));
        }
    }

    dijkstra(0);

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

    return 0;
}

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

void dijkstra(vector<vector<int>> v, vector<vector<int>> c, vector<int> &d) {
	d[0] = 0;
	priority_queue<P, vector<P>, greater<P>> pq;
	pq.push(P(0, 0));
	while (!pq.empty()) {
		int s = pq.top().second;
		int dis = pq.top().first;
		pq.pop();
		if (dis > d[s]) continue;
		REP(j, v[s].size()) {
			int u = v[s][j];
			int w = c[s][j];
			if (d[u] > d[s] + w) {
				d[u] = d[s] + w;
				pq.push(P(d[u], u));
			}
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<vector<int>> v(n), c(n);
	REP(i, n) {
		int u, k;
		cin >> u >> k;
		v[u] = vector<int>(k);
		c[u] = vector<int>(k);
		REP(j, k) cin >> v[u][j] >> c[u][j];
	}
	vector<int> d(n, INF);
	dijkstra(v, c, d);
	REP(i, n) {
		cout << i << " " << d[i] << "\n";
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define N_MAX 100

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
  priority_queue<node, vector<node>, less_cost> pq;
  pq.push(node(0, 0));
  vector<int> d(n, INT_MAX);
  d.at(0) = 0;

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

    // 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(d[u.first] + v.second < d[v.first]){
        d[v.first] = d[u.first] + v.second;
        pq.push(node(v.first, d[v.first]));
      }
    }
  }

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

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>

#define MAX 10000

using namespace std;

int n;
int unvisited=0,visited=1,comp_visited=2;
int INFTY=(1<<20);
vector<pair<int,int> > adj[MAX];

void dijkstra(){
	priority_queue<pair<int,int> > PQ;
	int state[MAX];
	int d[MAX];
	for(int i=0;i<n;i++){
		d[i]=INFTY;
		state[i]=unvisited;
	}
	d[0]=0;
	PQ.push(make_pair(0,0));
	state[0]=visited;
	while(!PQ.empty()){
		pair<int,int> f = PQ.top(); PQ.pop();
		int u=f.second;
		state[u]=comp_visited;
		
		//????°??????????????????????, ?????????????????§???????????°????????????.
		if(d[u]<f.first*(-1)) continue;
		
		for(int j=0;j<adj[u].size();j++){
			int v=adj[u][j].first;
			if(state[v]==comp_visited) 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));
				state[v]=visited;
			}
		}
	}
	for(int	i=0;i<n;i++){
		printf("%d %d\n",i,(d[i]==INFTY ? -1:d[i]));
	}
}

int main(){
	int k,u,v,c;
	
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		for(int j=0;j<k;j++){
			scanf("%d %d",&v,&c);
			adj[u].push_back(make_pair(v,c));
		}
	}
	dijkstra();
}