Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
const INF = 1 << 60

type Edge struct {
	to, cost int
}

type Node struct {
	id, cost int
}

type PQ []Node

func (pq PQ) Len() int           { return len(pq) }
func (pq PQ) Less(i, j int) bool { return pq[i].cost < pq[j].cost }
func (pq PQ) Swap(i, j int)      { pq[i], pq[j] = pq[j], pq[i] }

func (pq *PQ) Push(x interface{}) {
	*pq = append(*pq, x.(Node))
}

func (pq *PQ) Pop() interface{} {
	old := *pq
	n := len(old)
	x := old[n-1]
	*pq = old[0 : n-1]
	return x
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	E := make([][]Edge, N)
	for i := 0; i < N; i++ {
		id, k := sc.nextInt(), sc.nextInt()
		E[id] = make([]Edge, k)
		for j := 0; j < k; j++ {
			to, cost := sc.nextInt(), sc.nextInt()
			E[id][j] = Edge{to, cost}
		}
	}

	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	done := make([]bool, N)
	pq := &PQ{}
	heap.Init(pq)
	d[0] = 0
	heap.Push(pq, Node{0, 0})
	for pq.Len() > 0 {
		cur := heap.Pop(pq).(Node).id
		if done[cur] {
			continue
		}
		done[cur] = true
		for i := 0; i < len(E[cur]); i++ {
			to, cost := E[cur][i].to, E[cur][i].cost+d[cur]
			if !done[to] && cost < d[to] {
				d[to] = cost
				heap.Push(pq, Node{to, cost})
			}
		}
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i, d[i])
	}
	wtr.Flush()
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define erep(i,a,b) for(int i=a;i<=(int)(b);++i)
#define per(i,a,b) for(int i=(b);i>(a);--i)
#define eper(i,a,b) for(int i=((int)(a));i>=b;--i)
#define pb push_back
#define mp make_pair
#define INF 2000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a, b)*b;}

struct edge{int from, to, cost;};
int n, d[100005];
vector<edge> g;
int main() {
 ios::sync_with_stdio ( false );
 cin.tie ( 0 );
    cin >> n;
    rep(i, 0, n) {
        int u, k;
        cin >> u >> k;
        rep(j, 0, k) {
            int v, c;
            cin >> v >> c;
            edge e;
            e.from = u, e.to = v, e.cost = c;
            g.pb(e);
        }
    }
    rep(i, 0, n) d[i] = INF;
    d[0] = 0;
    while (1) {
        bool flag = false;
        rep(i, 0, g.size()) {
            edge e = g[i];
            if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
                d[e.to] = d[e.from] + e.cost;
                flag = true;
            }   
        }
        if (!flag) break;
    }
    rep(i, 0, n) {
        cout << i << " " << d[i] << endl;
    } 
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {
  int n; cin >> n;
  vector< pair<int, int> > adj[n];
  
  for(int i = 0; i < n; i++) {
    int ix, nn; cin >> ix >> nn;
    for(int j = 0; j < nn; j++) {
      int v, c; cin >> v >> c;
      adj[ix].push_back(make_pair(c, v));
    }
  }

  int inf = 10000000;
  int ans[n];
  fill(ans, ans+n, inf);

  ans[0] = 0;
  int ix = 0, sd[n] = {1};
  while(true) {

    for(int i = 0; i < adj[ix].size(); i++) {
      int cost = ans[ix] + adj[ix][i].first;
      int k = adj[ix][i].second;

      if(cost < ans[k]) {
        ans[k] = cost;
        sd[k] = 0;
      }

    }

    sd[ix] = 1;
    ix = -1;
    for(int i = 0; i < n; i++) {
      if(sd[i] == 0 && (ix == -1 || ans[ix] > ans[i])) {
        ix = i;
      }
    }
    if(ix == -1) {
      break;
    }
  }

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

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;
		printf("%d\n", cur.index);

		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 <bits/stdc++.h>
using ll = long long;
using namespace std;
static const int MAX = 1e4;
static const int INFTY = (1<<20);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
//first: 目的点,second: 距離
vector<pair<int,int> > adj[MAX];

void dijkstra(){
  //first: ある点へのマイナス距離, second: ある点
  priority_queue<pair<int,int> > pq;
  //d[i]: 始点からのiへの距離
  int d[MAX],color[MAX];
  for(int i=0;i<n;i++){
    d[i] = INFTY;
    color[i] = WHITE;
  }
  d[0] = 0;
  pq.push(make_pair(0,0));
  while(!pq.empty()){
    pair<int,int> key = pq.top();
    pq.pop();
    int u = key.second;
    color[u] = BLACK;
    if(d[u] < key.first*(-1)) continue;
    for(int j=0;j<adj[u].size();j++){
      int v = adj[u][j].first;
      if(color[v]==BLACK) continue;
      if(d[v] > d[u] + adj[u][j].second){
        //ここで最初に更新されているから、pqからtopを取り出すだけでOK
        d[v] = d[u] + adj[u][j].second;
        pq.push(make_pair((-1)*d[v],v));
        color[v] = GRAY;
      }
    }
  }
  for(int i=0;i<n;i++){
    cout << i << " " << (d[i]==INFTY ? -1 : d[i]) << endl;
  }
}

int main(){
  int k,u,v,c;
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> u >> k;
    for(int j=0;j<k;j++){
      cin >> v >> c;
      adj[u].push_back(make_pair(v,c));
    }
  }
  dijkstra();
}