Go: package main

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

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	route := make([][]Node, n)
	weight := make([]int, n)
	for i := 0; i < n; i++ {
		weight[i] = NIL
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		route[i] = make([]Node, k)
		for j := 0; j < k; j++ {
			route[u][j] = Node{id: scanInt(), weight: scanInt()}
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for _, nextNode := range route[node.id] {
			if weight[nextNode.id] != NIL {
				continue
			}
			nextNode.weight += node.weight
			heap.Push(h, nextNode)
		}
	}

	for i, w := range weight {
		fmt.Println(i, w)
	}
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
static const int MAX = 10000;
static const int INFTY = (1 << 20);
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] = WHITE;
    }
 
    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;
 
        if (d[u] < f.first*(-1)) continue;
 
        for (unsigned 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;
                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();
 
    return 0;
}

C++: #include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
static const int kMaxVertices = 10000;
static const int kInfinity = 100010;
using namespace std;

void Dijkstra(int n, vector<pair<int, int> > Adjacency_list[], int first_vertex){
	bool has_not_been_added[kMaxVertices];
	int distance[kMaxVertices];
	for (int i = 0; i < n; ++i)	{
		has_not_been_added[i] = true;
		distance[i] = kInfinity;
	}
	distance[first_vertex] = 0;

	priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > PQ;
	PQ.push(make_pair(0, first_vertex));

	while(!PQ.empty()){
		pair<int, int> f = PQ.top(); PQ.pop();
		int u = f.second;
		has_not_been_added[u] = false;
		if (distance[u] < f.first) continue;
		for(vector<pair<int, int> >::iterator it = Adjacency_list[u].begin(); it != Adjacency_list[u].end(); it++){
			int v = (*it).second;
			if(has_not_been_added[v] && distance[u] + (*it).first < distance[v]){
				distance[v] = distance[u] + (*it).first;
				PQ.push(make_pair(distance[v], v));
			}
		}
	}
	for (int i = 0;i < n;++i)
		printf("%d %d\n", i, distance[i]);
}

int main(){
	int number_of_vertices;
	scanf("%d", &number_of_vertices);
	vector<pair<int, int> > Adjacency_list[number_of_vertices];
	for (int vertex_from_i = 0; vertex_from_i < number_of_vertices; ++vertex_from_i)	{
		int vertex_from_id, degree_out;
		scanf("%d %d", &vertex_from_id, &degree_out);
		for (int vertex_to_i = 0; vertex_to_i < degree_out; ++vertex_to_i){
			int vertex_to_id, cost_of_route;
			scanf("%d %d", &vertex_to_id, &cost_of_route);
			Adjacency_list[vertex_from_id].push_back(make_pair(cost_of_route, vertex_to_id));
		}
	}
	Dijkstra(number_of_vertices, Adjacency_list, 0);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

const int INF = 1e8;

int n, m; // 頂点の数、辺の数

struct edge {int from, to, cost;}; // 辺の始点・終点・重み

vector<vector<int>> adj_matrix; // 隣接行列
vector<vector<edge>> adj_list; // 隣接リスト

vector<int> dist;
vector<bool> visited;

void dijkstra(int start) {
    dist.resize(n);
    visited.resize(n);
    rep(i, n) {
        if (i == start) dist[i] = 0;
        else dist[i] = INF;
        visited[i] = false;
    }
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
    pq.push({dist[start], start});
    while (not pq.empty()) {
        int cur = pq.top().second;
        pq.pop();
        visited[cur] = true;
        for (edge e : adj_list[cur]) {
            // int adj = e.to;
            if (visited[e.to] == false) {
                if (dist[e.to] > dist[e.from] + e.cost) {
                    dist[e.to] = dist[e.from] + e.cost;
                    pq.push({dist[e.to], e.to});
                }
            }
        }
    }
}

void solve_ALDS1_12_C() {
    cin >> n;
    adj_list.resize(n);
    rep(i, n) {
        int u, k;
        cin >> u;
        cin >> k;
        rep(j, k) {
            edge e;
            e.from = u;
            cin >> e.to;
            cin >> e.cost;
            adj_list[e.from].push_back(e);
        }
    }
    dijkstra(0);
    rep(i, n) {
        cout << i << " " << dist[i] << endl;
    }
}

int main() {
    // cin >> n;
    // cin >> m;
    // adj_matrix.resize(n, vector<int>(n, INF));
    // rep(i, n) {
    //     adj_matrix[i][i] = 0;
    // }
    // adj_list.resize(n);
    // rep(i, m) {
    //     edge e;
    //     cin >> e.from;
    //     cin >> e.to;
    //     cin >> e.cost;
    //     adj_matrix[e.from][e.to] = e.cost;
    //     adj_list[e.from].push_back(e);
    // }
    solve_ALDS1_12_C();
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define INF INT_MAX/3
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

#define MAX_N 10000
int G[MAX_N][MAX_N];
int MIN_COSTS[MAX_N];

int main() {
  int n;
  cin >> n;
  REP(i, n) {
    REP(j, n) {
      G[i][j] = INT_MAX;
    }
    MIN_COSTS[i] = INT_MAX;
  }
  int u, k;
  int v, c;
  REP(i, n) {
    cin >> u >> k;
    REP(j, k) {
      cin >> v >> c;
      G[u][v] = c;
    }
  }

  priority_queue<P> q;
  int position;
  int new_cost;
  q.push(P(0, 0));
  while (q.size() != 0){
    position = q.top().first;
    MIN_COSTS[position] = q.top().second;
    q.pop();
    REP(i, n) {
      if (G[position][i] != INT_MAX) {
        new_cost = MIN_COSTS[position] + G[position][i];
        if (new_cost < MIN_COSTS[i]) {
          MIN_COSTS[i] = new_cost;
          q.push(P(i, new_cost));
        }
      }
    }
  }

  REP(i, n) {
    cout << i << ' ' << MIN_COSTS[i] << endl;
  }

  return 0;
}