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 <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

void dijkstra(vector<vector<pair<int, int>>>& edges, vector<int>& ds, int start) {
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
    q.push(make_pair(0, start));
    while (!q.empty()) {
        int d = q.top().first;
        int u = q.top().second;
        q.pop();
        if (ds[u] >= 0) {
            continue;
        }
        ds[u] = d;
        for (auto p : edges[u]) {
            q.push(make_pair(d + p.second, p.first));
        }
    }
}

int main() {
    int n;
    cin >> n;
    vector<vector<pair<int, int>>> edges(n);
    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;
            edges[u].push_back(make_pair(v, c));
        }
    }

    vector<int> ds(n, -1);
    dijkstra(edges, ds, 0);

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

C++: #define ll long long
#define MAX 10000

#include "bits/stdc++.h"

using namespace std;
static const int INF = (1 << 21);

int n;
priority_queue<pair<int,int>> PQ;
vector<pair<int, int>> adj[MAX];
int color[MAX];
int d[MAX];

void dj(int s){
	for (int i = 0;i < n;i++) {
		d[i]=INF;
		color[i] = 0;
	}
	
	d[s] = 0;
	PQ.push(make_pair(0, 0));
	color[0] = 1;

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

		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] == 2) 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] = 1;
			}
		}
	}

	for (int i = 0;i < n;i++) {
		cout << i << " " << (d[i] == INF ? -1 : d[i]) << endl;
	}

}

int main() {
	cin >> n;
	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;
			adj[u].push_back(make_pair(v,c));
		}
	}

	dj(0);

}

C++: #include <cstdio>           // printf(), getc(), scanf()
#include <cctype>           // isdigit()
#include <vector>
#include <queue>            // priority_queue
#include <algorithm>        // fill()
#include <utility>          // pair

using namespace std;

static const int MAX_V = 10000;
static const int INF = 100000000;

typedef pair<int, int> P;
struct edge
{
	int to;
	int cost;
};

vector<edge> G[MAX_V];
int V;

void
dijkstra()
{
	priority_queue<P, vector<P>, greater<P> > que;
	int d[MAX_V];
	int u;
	unsigned int v; 

	fill(d, d + V, INF);
	d[0] = 0;
	que.push(P(0, 0));

	while (!que.empty())
	{
		P p = que.top(); que.pop();
		int u = p.second;
		if (d[u] < p.first)
			continue;

		for (v = 0; v < G[u].size(); ++v)
		{
			edge e = G[u][v];
			if (d[u] + e.cost < d[e.to])
			{
				d[e.to] = d[u] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}

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

int
main(int argc, char** argv)
{
	edge e;
	int u, v;
	int i, j, k;
	int n;
	int c;

	c = getc(stdin);
	while (c == ' ')
		c = getc(stdin);
	
	n = 0;
	while (isdigit(c))
	{
		n = n * 10 + c - '0';
		c = getc(stdin);
	}

	V = n;
	for (i = 0; i < V; ++i)
	{
		while (c == ' ' || c == '\n')
			c = getc(stdin);

		n = 0;
		while (isdigit(c))
		{
			n = n * 10 + c - '0';
			c = getc(stdin);
		}

		u = n;
		while (c == ' ')
			c = getc(stdin);

		n = 0;
		while (isdigit(c))
		{
			n = n * 10 + c - '0';
			c = getc(stdin);
		}

		k = n;
		for (j = 0; j < k; ++j)
		{
			while (c == ' ')
				c = getc(stdin);

			n = 0;
			while (isdigit(c))
			{
				n = n * 10 + c - '0';
				c = getc(stdin);
			}

			v = n;
			while (c == ' ')
				c = getc(stdin);

			n = 0;
			while (isdigit(c))
			{
				n = n * 10 + c - '0';
				c = getc(stdin);
			}

			e.to = v;
			e.cost = n;
			G[u].push_back(e);
		}
	}

	dijkstra();
	return 0;
}

C++: #include <utility>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int n;					//えぬ
int a[100][100] = {0};	//辺の重さ
int f[100] = {0};		//その頂点が調査済みかどうか

int main(){
	cin >> n;
	//まず辺の重さをaに入れる
	for ( int i = 0; i < n; i++ ) {
		for ( int j = 0; j < n; j++ ) {
			a[i][j] = -1;
		}
	}
	a[0][0] = 0;
	for ( int i = 0; i < n; i++ ) {
		int u;
		cin >> u;
		int k;
		cin >> k;
		for ( int j = 0; j < k; j++ ) {
			int v;
			cin >> v;
			cin >> a[u][v];
		}
	}
	for ( int i = 0; i < n - 1; i++ ) {
		int k = -1;
		int v = 0;
		for ( int j = 1; j < n; j++ ) {
			if ( a[0][j] == -1 || f[j] == 1 ) continue;
			if ( k == -1 || a[0][j] < k ) {
				v = j;
				k = a[0][j];
			}
		}
		f[v] = 1;
		for ( int j = 1; j < n; j++ ) {
			if ( v == j ) continue;
			if ( a[v][j] == -1 || f[j] == 1 ) continue;
			if ( a[0][j] == -1 || a[0][j] > a[0][v] + a[v][j] ) {
				a[0][j] = a[0][v] + a[v][j];
			}
		}
	}
	for ( int i = 0; i < n; i++ ) {
		cout << i << " " << a[0][i] << endl;
	}
	return 0;
}