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++: ///
// File:  alds1_12_c.cpp
// Author: ymiyamoto
//
// Created on Wed Oct 18 22:36:29 2017
//

#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define SIZE 10000

struct route_t {
  uint32_t node;
  int64_t weight;

  bool operator<(const route_t &r) const
  {
    return weight > r.weight;
  }
};

static vector<route_t> graph[SIZE];
static int64_t costs[SIZE];
static bool visited[SIZE];
static priority_queue<route_t> q;

static void dikstra()
{
  q.push({0, 0});
  costs[0] = 0;

  while (!q.empty()) {
    route_t r = q.top();
    q.pop();

    if (!visited[r.node]) {
      visited[r.node] = true;
      for (uint32_t i = 0; i < graph[r.node].size(); i++) {
        int64_t cost = graph[r.node][i].weight;
        uint32_t node = graph[r.node][i].node;
        if (cost != -1) {
          if (costs[r.node] + cost < costs[node]) {
            costs[node] = costs[r.node] + cost;
          }
          q.push({node, cost});
        }
      }
    }
  }
}

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    costs[i] = INT32_MAX;
  }

  for (uint32_t i = 0; i < n; i++) {
    uint32_t id, k;
    cin >> id >> k;
    for (uint32_t j = 0; j < k; j++) {
      route_t r;
      cin >> r.node >> r.weight;
      graph[id].push_back(r);
    }
  }

  dikstra();

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

  return 0;
}

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

const int NMAX = 10000;
const int AMAX = 9999999;

const int BLACK = 2;
const int GRAY = 1;
const int WHITE = 0;

vector<pair<int, int> > A[NMAX];
int c[NMAX];
int d[NMAX];
int n;

void dijkstra(int s) {
	for (int i = 0; i < n; i++) {
		c[i] = WHITE;
		d[i] = AMAX;
	}

	d[s] = 0;
	c[s] = GRAY;
	priority_queue<pair<int, int> > PQ;
	PQ.push(make_pair(d[s], s));

	while (!PQ.empty()) {
		pair<int, int> f = PQ.top(); PQ.pop();
		int minw, u;
		minw = f.first;
		u = f.second;

		c[u] = BLACK;

		if (d[u] < minw * (-1)) continue;

		for (int i = 0; i < A[u].size(); i++) {
			int v = A[u][i].first;
			int w = A[u][i].second;
			if (c[v] == BLACK) continue;

			if (d[v] > d[u] + w) {
				d[v] = d[u] + w;
				PQ.push(make_pair(d[v] * (-1), v));
				c[v] = GRAY;
			}
		}
	}
}

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;
			scanf("%d %d", &v, &c);
			A[u].push_back(make_pair(v, c));
		}
	}

	dijkstra(0);

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

	return 0;
}

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

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

typedef struct edge_def {
  int from;
  int to;
  int cost;

  edge_def(int _from, int _to, int _cost) {
    from = _from;
    to   = _to;
    cost = _cost;
  }
} edge;

const int INF = INT_MAX;
const int V = 10000;
int n, t;
vector<edge> es[V];
int d[V];

void dijkstra(int s) {
  int i;
  priority_queue<PII, vector<PII>, greater<PII > > q;
  for (i=0; i<n; i++) d[i] = INF;
  d[s] = 0;
  q.push(PII(0, s));

  while (q.size()) {
    PII p = q.top();
    q.pop();
    int v = p.second;

    if (d[v] < p.first) continue;

    for (i=0; i<(int)es[v].size(); i++) {
      edge e = es[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost; 
        q.push(PII(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int i, j, k;
  int u, v, c;
  for (i=0; i<V; i++) es[i].clear();

  scanf("%d ", &n);
  for (i=0; i<n; i++) {
    scanf("%d%d ", &u, &k);
    for (j=0; j<k; j++) {
      scanf("%d%d ", &v, &c);
      es[u].psb(edge(u, v, c));
    }
  }

  dijkstra(0);

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

	return 0;
}

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

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


int n, M[MAX][MAX];

void dijkstra() {
    int minv;
    int d[MAX], color[MAX];

    for( int i = 0; i < n; i++ ) {
        d[i] = INFTY;
        color[i] = WHITE;
    }

    d[0] = 0;
    color[0] = GRAY;

    while(1) {
        minv = INFTY;
        int u = -1;
        for (int i = 0; i < n; i++) {
            if (minv  > d[i] && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1 ) {
            break;
        }
        color[u] = BLACK;
        for (int v = 0; v < n; v++) {
            if (d[v] > d[u] +M[u][v]) {
                d[v] = d[u] + M[u][v];
                color[v] = GRAY;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout << i << " " << ( d[i] == INFTY ? -1 : d[i] ) << endl;
    }
}

int main(int argc, char const* argv[])
{
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            M[i][j] = INFTY;
        }
    }

    int k, c, u, v;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            cin >> v >> c;
            M[u][v] = c;
        }
    }

    dijkstra();
    return 0;
}