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++: // ALDS1_12_C.cpp
// Graph II - Single Source Shortest Path II
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;

struct Node {
    int v;
    int c;
    Node(int v, int c): v(v), c(c) {}
};

const int maxN = 10000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int INF = (1<<21);
vector<Node> G[maxN];
vector<int> dist(maxN, INF);
vector<int> p(maxN);
vector<int> color(maxN, WHITE);
priority_queue<pair<int, int>> mq;
int n;

void dijkstra(int x)
{
    dist[x] = 0;
    p[x] = -1;
    mq.push({-dist[x], x});
    while(!mq.empty()) {
        pair<int, int> P = mq.top();
        mq.pop();
        int mindist = -P.first;
        int u = P.second;
        if (color[u] == BLACK) continue;

        for (int i = 0; i < G[u].size(); i++) {
            if (color[u] == BLACK) continue;
            int v = G[u][i].v;
            int c = G[u][i].c;
            if (dist[u] + c < dist[v]) {
                dist[v] = dist[u] + c;
                p[v] = u;
                color[v] = GRAY;
                mq.push({-dist[v], v});
            }
        }
        color[u] = BLACK;
    }
}

int main()
{
//    freopen("in7.txt", "r", stdin);
//    freopen("out7.txt", "w", stdout);
    cin >> n;
    int u, k;
    int v, c;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            cin >> v >> c;
            G[i].push_back(Node(v, c));
        }
    }
    dijkstra(0);
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i, dist[i]);
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>

using WNode = std::pair<uint32_t, uint32_t>;
using WGraph = std::vector<std::vector<WNode>>;
using rWNode = std::pair<uint32_t, uint32_t>;

int main() {
  uint32_t N = 0;
  std::cin >> N;
  WGraph G(N);
  for (size_t i = 0; i < N; i++) {
    uint32_t K = 0;
    std::cin >> i >> K;
    for (size_t j = 0; j < K; j++) {
      uint32_t node = 0, cost = 0;
      std::cin >> node >> cost;
      G[i].push_back({node, cost});
    }
  }

  const uint32_t start = 0;
  std::priority_queue<rWNode, std::vector<rWNode>, std::greater<rWNode>> Q;
  Q.push({0, start});
  std::vector<uint32_t> result(N, -1);
  std::vector<uint32_t> temp(N, -1);
  while (!Q.empty()) {
    uint32_t cost = Q.top().first, current = Q.top().second;
    Q.pop();

    if (result[current] <= cost) continue;
    result[current] = cost;

    for (auto &&g : G[current]) {
      if (result[g.first] != static_cast<uint32_t>(-1)) continue;
      if (temp[g.first] <= g.second + result[current]) continue;
      temp[g.first] = g.second + result[current];
      Q.push({g.second + result[current], g.first});
    }
  }

  for (size_t i = 0; i < N; i++) {
    std::cout << i << " " << result[i] << std::endl;
  }

  return 0;
}

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

static const int MAX = 10000;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;

vector<pair<int, int>> adj[MAX]; // 重み付き有向グラフの隣接リスト表現

void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int color[MAX];
  int d[MAX];
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = 0;
  }

  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = BLACK;

    // priority_queueはデフォルト大きい値を優先しているため-1して保存している
    // if (d[u] < f.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) {
        d[v] = d[u] + adj[u][j].second;
        // priority_queueはデフォルトで大きい値を優先するため-1をかける
        PQ.push(make_pair(d[v] * (-1), 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();
};

C++: #include <stdio.h>
#include <vector>
using namespace std;
#define MAX_N (10010)
#define INF (1000000100)
typedef struct {
    int to;
    int w;
} Edge;

vector<Edge> G[MAX_N];
int n;

int dist[MAX_N];
bool arrived[MAX_N];

void dijkstra(int s)
{
    for ( int i = 0; i < n; i++ ) {
	dist[i] = INF;
	arrived[i] = false;
    }

    dist[s] = 0;

    while ( true ) {
	int min_dist = INF;
	int index = -1;

	for ( int i = 0; i < n; i++ ) {
	    if ( !arrived[i] && (dist[i] < min_dist) ) {
		min_dist = dist[i];
		index = i;
	    }
	}
	
	if ( index == -1 ) break;
	
	arrived[index] = true;

	for ( int i = 0; i < G[index].size(); i++ ) {
	    Edge e = G[index][i];
	    if ( !arrived[e.to] && dist[e.to] > (dist[index] + e.w) ) {
		dist[e.to] = dist[index] + e.w;
	    }
	}
    }

}

int main()
{
    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, c;
	    Edge e;
	    scanf("%d %d", &v, &c);
	    e.to = v;
	    e.w = c;
	    G[u].push_back(e);
	}
    }

    dijkstra(0);

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

    return 0;
}