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 <vector>
#include <limits>
#include <queue>

using namespace std;

struct Node {
  int label;
  int weight;

  bool operator>(const Node& other) const{
    return weight > other.weight;
  }
};

using ListGraph = vector<vector<Node>>;


const int max_num = numeric_limits<int>::max();


// priority_queueはpriorityが大きいのもから取り出す。
// デフォルトでは < (less<T>)を要素の比較に用いる。
// lessの場合要素が小さい時に入れ替える、つまりmaxHeapであり、priortyが大きいものから取り出される。

vector<int> shortest_cost(ListGraph &list_graph, int started_label) {
  vector<bool> visited_vertices(list_graph.size() ,false);
  vector<int> costs(list_graph.size(), max_num);
  priority_queue<Node, vector<Node>, greater<Node>> adjacency_costs;

  costs[started_label] = 0;
  adjacency_costs.push({started_label, costs[started_label]});

  while(!adjacency_costs.empty()) {
    Node nearest_node = adjacency_costs.top();
    adjacency_costs.pop();

    if(nearest_node.weight != costs[nearest_node.label]) {
      continue;
    }

    for(int i = 0; i < list_graph[nearest_node.label].size(); i++) {
      Node adjacency = list_graph[nearest_node.label][i];
      if(visited_vertices[adjacency.label]) {
        continue;
      }

      if(costs[nearest_node.label] + adjacency.weight < costs[adjacency.label]) {
        costs[adjacency.label] = costs[nearest_node.label] + adjacency.weight;
        adjacency_costs.push({adjacency.label, costs[adjacency.label]});
      }
    }
  }

  return costs;
}

int main() {
  int vertex_num;
  ListGraph list_graph;
  cin >> vertex_num;
  list_graph.resize(vertex_num);

  for(int i = 0; i < vertex_num; i++) {
    int label, degree;
    cin >> label >> degree;
    list_graph[i].resize(degree);

    for(int j = 0; j < degree; j++) {
      Node adjacency;
      cin >> adjacency.label >> adjacency.weight;

      list_graph[i][j] = adjacency;
    }
  }

  vector<int> costs = shortest_cost(list_graph, 0);

  for(int i = 0; i < costs.size(); i++) {
    cout << i << ' ' << costs[i] << endl;
  }
}

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

const int N=10000;
const long INF=1000000000;

int main(){
	int n;
	vector<pair<int,long> > adj[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;
			long c;
			scanf(" %d %ld", &v, &c);
			adj[u].push_back(make_pair(v,c)); //v??¨??£??\????????????????????¨??????????????????c
		} 	
	}
		
	//PriorityQueue?????¨??????Dijkstra???
	priority_queue<pair<long,int> > pq;
	
	int visit[N]={0};
	
	long cost[N];
	for(int i=0; i<N; ++i) cost[i]=INF;
	cost[0]=0; //?§?????????????????????§?§??????????	
	pq.push(make_pair(0, 0));
	visit[0]=-1; //?¨??????????
	
	while(!pq.empty()){
		pair<long,int> p=pq.top(); //pq??????????????§cost???????°?????????????????????????
		pq.pop();
		
		long u=p.second;
		visit[u]=-1; //????????????????¨???????????????????
		
		//????°??????¨???????????????????????§???????????°??????
		if(cost[u] < p.first*(-1)) continue;	
		
		//??£??\???????????????????????????
		for(int j=0; j<adj[u].size(); ++j){
			long v=adj[u][j].first; //u?????£??\????????????
			if(visit[v]==-1) continue;
			
			if(cost[v] > cost[u]+adj[u][j].second){
				cost[v] = cost[u]+adj[u][j].second;
				pq.push(make_pair(cost[v]*(-1), v));
				visit[v]=1;
			}
		}
		
	}
	
	for(int i=0; i<n; ++i)
		printf("%d %ld\n", i, (cost[i]==INF ? -1 : cost[i]));
}

C++: // ConanYu's https://vjudge.net/solution/13220749
// semon0x29a's https://vjudge.net/solution/13218148

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using pi = pair<int, int>;

const int INF = 0x3F3F3F3F;
const int maxN = 10010;

int n;
bool mark[maxN];
LL dis[maxN];
map<pi, LL> path;

void init() {
	memset(dis, 0x3F, sizeof(dis));
}

void dijkstra() {
	dis[0] = 0;
	for (int i=0; i<n; ++i) {
		int marker = 0, amin = INF;
		for (int i=0; i<n; ++i) if (!mark[i]) {
			if (dis[i] < amin) {
				amin = dis[i];
				marker = i;
			}
		}
		mark[marker] = true;
		for (int i=0; i<n; ++i) if (!mark[i]) {
			if (path.count({marker, i}))
			if (dis[i] > dis[marker] + path[{marker, i}])	
				dis[i] = dis[marker] + path[{marker, i}];
		}
	}
}

int main()
{
	init();
	
	scanf("%d", &n);
	for (int i=0; i<n; ++i) 
	{
		int u, k; scanf("%d%d", &u, &k);
		while (k--) {
			int v; LL c; scanf("%d%lld", &v, &c);
			if (path.count({u, v}))
				path[{u, v}] = min(path[{u, v}], c);
			else path[{u, v}] = c;
			path[{v, u}] = path[{u, v}];
		}
	}
	
	dijkstra();
	for (int i=0; i<n; ++i) {
		printf("%d %lld\n", i, dis[i]);
	}
}