Go: package main

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

type Node struct {
	Id int
	Dis int
}

type PriorityQueue []*Node

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].Dis < pq[j].Dis
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

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

func (pq *PriorityQueue) Pop() interface{} {
	n := len(*pq)
	pop := (*pq)[n-1]
	*pq = (*pq)[0:n-1]
	return pop
}

func main() {
	var n int
	fmt.Scan(&n)

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

	g := make([][]int, n)
	c := make([][]int, n)
	pq := make(PriorityQueue, 0, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		g[u] = make([]int, k)
		c[u] = make([]int, k)
		for j := 0; j < k; j++ {
			sc.Scan()
			g[u][j], _ = strconv.Atoi(sc.Text())
			sc.Scan()
			c[u][j], _ = strconv.Atoi(sc.Text())
		}

		if u == 0 {
			d[u] = 0
		} else {
			d[u] = 1 << 30
		}
		pq.Push(&Node{u, d[u]})
	}
	heap.Init(&pq)

	for pq.Len() > 0 {
		node := heap.Pop(&pq).(*Node)
		for i := 0; i < len(g[node.Id]); i++ {
			nextId := g[node.Id][i]
			nextDis := node.Dis + c[node.Id][i]
			if nextDis < d[nextId] {
				d[nextId] = nextDis
				heap.Push(&pq, &Node{nextId, nextDis})
			}
		}
	}

	wr := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		wr.WriteString(strconv.Itoa(i))
		wr.WriteString(" ")
		wr.WriteString(strconv.Itoa(d[i]))
		wr.WriteString("\n")
	}
	wr.Flush()
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef vector <int> vi;
typedef vector <vi> vvi;

class blanch{
	public:
		int weight;
		int total;
		int from;
		int to;
		blanch():
			weight(0), from(0), to(0){};
		blanch(int newweight, int newfrom, int newto):
			weight(newweight), from(newfrom), to(newto){};
};
typedef vector <blanch> vb;


bool operator<( const blanch &blanch1, const blanch &blanch2){
	return (blanch1.weight > blanch2.weight);
	}

bool operator>( const blanch &blanch1, const blanch &blanch2){
	return (blanch1.weight < blanch2.weight);
	}

int make_graph(vvi &graph, vi &stamp){
	int n=graph.size();
	for (int i=0; i<n; i++){
		for (int j=0; j<n; j++){
			graph[i][j]=0;
		}
	}

	for (int j=0; j<n; j++){
		int i2,n2;
		cin>>i2;
		cin>>n2;
		for (int i=0; i<n2; i++){
			int j2;
			cin>>j2;
			cin>>graph[i2][j2];
		}
		stamp[i2] = -1;
	}
	return 0;
}

int Shortest_Path(vvi &graph, vi &stamp){

	priority_queue<blanch, vb, less<vb::value_type> > pq;
	int n = graph.size(), p1=0;
	stamp[p1] = 0;

	while (p1!=-1){
		for (int p2=0; p2<n; p2++){
			int *tmp = &graph[p1][p2];
			if (*tmp!=0 && stamp[p2]==-1){
				pq.push(blanch(*tmp+stamp[p1], p1, p2));
			}
		}
		p1 = -1;
		while (! pq.empty()){
			blanch tmp = pq.top();
			if (stamp[tmp.from]!=-1 && stamp[tmp.to]==-1){
				p1 = tmp.to;
				stamp[p1] = tmp.weight;
				pq.pop();
				break;
			} else
				pq.pop();
		}
	}
	return 0;
}

int resultout(vi &stamp){
	int n = stamp.size();
	for (int i=0; i<n; i++){
		cout<<i<<" "<<stamp[i]<<endl;
	}
	return 0;
}

int main(void){

	int n;
	cin>>n;
	vvi G(n, vi(n));
	vi stamp(n);

	make_graph(G, stamp);

	Shortest_Path(G, stamp);

	resultout(stamp);

	return 0;
}

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

typedef pair<long, long> P;

long N, d[MAX_N];
vector<P> edge[MAX_N];

void dijkstra(){
    bool seen[N];
    fill(seen, seen + N, false);
    priority_queue<P, vector<P>, greater<P> > todo;
    todo.push(P(0, 0));
    
    while(!todo.empty()){
        long dist = todo.top().first, v = todo.top().second;
        todo.pop();
        if(seen[v]) continue;
        d[v] = dist;
        seen[v] = true;
        for(long i = 0; i < edge[v].size(); i++){
            long w = edge[v][i].second, u = edge[v][i].first;
            if(d[v] + w < d[u]){
                d[u] = d[v] + w;
                todo.push(P(d[u], u));
            }
        }
    }
}

int main(){
    cin >> N;
    fill(d, d + MAX_N, LONG_MAX);
    for(long i = 0; i < N; i++){
        long v, k, u, c;
        cin >> v >> k;
        for(long j = 0; j < k; j++){
            cin >> u >> c;
            edge[v].push_back(P(u, c));
        }
    }
    
    dijkstra();
    
    for(long i = 0; i < N; i++)
        cout << i << " " << d[i] << endl;
    
    return 0;
}

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

int INF;
int n;
vector<pair<int, int>> es[10000];

// sから各頂点への最短経路を求める
void dijkstra () {
  
  priority_queue<pair<int, int>> pq;
  int d[10000];

  // 0:未調査 1:調査中 2:調査完了
  int status[10000];

  for (int i = 0; i < n; i++) {
    d[i] = INF;
    status[i] = 0;
  }

  d[0] = 0;
  status[0] = 1;
  pq.push(make_pair(0,0));

  while (!pq.empty()) {

    pair<int, int> p = pq.top(); pq.pop();
    int u = p.second;
    status[u] = 2;

    // 最小値を取り出し、それが最短でなければ無視
    if (d[u] < p.first * (-1)) {
      continue;
    }
    
    for (int i = 0; i < es[u].size(); i++) {
      int v = es[u][i].first;
      if (status[v] == 2) {
        continue;
      }
      if (d[u] + es[u][i].second < d[v]) {
        d[v] = d[u] + es[u][i].second;
        // priority_queueはデフォルトで大きい順を優先するため-1を掛ける
        pq.push(make_pair(d[v] * (-1), v));
        status[v] = 1;
      }
    }
    
  }
  for (int i = 0; i < n; i++) {
    if (d[i] == INF) {
      cout << i << " -1" << endl;
    } else {
      cout << i << " " << d[i] << endl;
    } 
  }
}

int main() {
  
  cin >> n;

  INF = 20000000;

  // 入力を受け取る
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v, c;
      cin >> v >> c;
      es[u].push_back(make_pair(v,c));
    }
  }

  // ダイクストラ法開始
  dijkstra();

  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
static const int NMAX = 10000;
#define INF (1 << 30)

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> adj[NMAX];

  for (int i = 0; i < n; i++) {
    int id, nn;
    cin >> id >> nn;
    for (int j = 0; j < nn; j++) {
      int to, weight;
      cin >> to >> weight;
      // use negative weights for priority_queue
      adj[id].push_back(make_pair(-weight, to));
    }
  }

  bool visited[n];
  int dist[n];
  for (int i = 0; i < n; i++) {
    visited[i] = false;
    dist[i] = -INF;
  }

  priority_queue<pair<int, int>> distQ;

  // add node 0
  for (int i = 0; i < adj[0].size(); i++) {
    distQ.push(adj[0][i]);
    int weight = adj[0][i].first;
    int id = adj[0][i].second;
    dist[id] = weight;
  }
  visited[0] = true;

  while (!distQ.empty()) {
    // find nearest node
    int minnode = distQ.top().second;
    distQ.pop();

    // add node
    visited[minnode] = true;

    // update dist
    for (int i = 0; i < adj[minnode].size(); i++) {
      int weight = adj[minnode][i].first;
      int id = adj[minnode][i].second;

      if (visited[id] == false) {
        int newWeight = dist[minnode] + weight;
        if (-newWeight < -dist[id]) {
          distQ.push(make_pair(newWeight, id));
          dist[id] = newWeight;
        }
      }
    }
  }

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