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++: //
// Created by 李青坪 on 2018/11/8.
//
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#define INFINITY 1<<21
#define NIL -1
#define MAXN 10000

using namespace std;
struct Node{
    int id;
    int weight;
    friend bool operator < (Node x1, Node x2) {
        return x1.weight < x2.weight;
    }
};

struct cmp{
    bool operator ()(Node &a, Node &b){
        return a.weight > b.weight;
    }
};

vector<Node> graph[MAXN];
priority_queue<Node, vector<Node>, cmp> V_S;
int n;
int d[MAXN];
bool visited[MAXN];

void init(){
    fill(d, d+MAXN, INFINITY);
    d[0] = 0;
    fill(visited, visited+MAXN, false);
    Node node;
    node.id = 0;
    node.weight = 0;
    V_S.push(node);
}

void updateMinWeight(Node u){
    vector<Node>::iterator it;
    for (it = graph[u.id].begin(); it != graph[u.id].end(); ++it){ //遍历与u相连的所有结点，看有没有结点到S的距离减小了
        if (visited[(*it).id]) continue;
        if (d[(*it).id] > d[u.id] + (*it).weight){
            d[(*it).id] = d[u.id] + (*it).weight;
            Node node;
            node.id = (*it).id;
            node.weight = d[(*it).id];
            V_S.push(node);
        }
    }
}

void dijkstra(){
    while (!V_S.empty()){
        Node u = V_S.top();
        V_S.pop();
        visited[u.id] = true;
        updateMinWeight(u);
    }
}

int main(){
    scanf("%d", &n);
    int u, k;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &u, &k);
        int v, w;
        Node node;
        for (int j = 0; j < k; ++j) {
            scanf("%d %d", &v, &w);
            node.id = v;
            node.weight = w;
            graph[u].push_back(node);
        }
    }
    init();
    dijkstra();
    for (int l = 0; l < n; ++l) {
        printf("%d %d\n", l, d[l]);
    }
//    Node l[6];
//    l[0].id = 0; l[0].weight = 3;
//    l[1].id = 1; l[1].weight = 2;
//    l[2].id = 2; l[2].weight = 5;
//    l[3].id = 3; l[3].weight = 3;
//    l[4].id = 4; l[4].weight = 4;
//    l[5].id = 5; l[5].weight = 2;
//    for (int i = 0; i < 6; ++i) {
//        V_S.push(l[i]);
//    }
//    for (int j = 0; j < 6; ++j) {
//        printf("id: %d weight: %d\n", V_S.top().id, V_S.top().weight);
//        V_S.pop();
//    }
}
/*
5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3
 */

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 = costs[r.node];
        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, costs[node]});
          }
        }
      }
    }
  }
}

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<bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

struct edge{int to,cost;};
using Graph=vector<vector<edge>>;
using pint=pair<int,int>;
const int INF=1e9;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N; cin>>N;
    Graph G(N);
    for(int i=0;i<N;i++){
        int u,k; cin>>u>>k;
        while(k--){
            int v,c; cin>>v>>c;
            G[u].push_back({v,c});
        }
    }

    vector<int> dist(N,INF),finished(N,false);
    priority_queue<pint,vector<pint>,greater<pint>> Q;
    dist[0]=0;
    Q.push({0,0});
    while(!Q.empty()){
        int v=Q.top().second; Q.pop();
        if(finished[v]) continue;
        finished[v]=true;
        for(edge e:G[v]) if(chmin(dist[e.to],dist[v]+e.cost)){
            Q.push({dist[e.to],e.to});
        }
    }
    for(int i=0;i<N;i++){
        cout<<i<<' '<<dist[i]<<endl;
    }
    return 0;
}