Go: package main

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

type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}
func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}
func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

type Q struct {
	pq *PriorityQueue
	ds map[int]int // It has always same value as pq. For random access.
}

func newQ() *Q {
	pq := new(PriorityQueue)
	heap.Init(pq)
	return &Q{
		pq: pq,
		ds: make(map[int]int),
	}
}
func (q *Q) pop() (u, d int) {
	item := heap.Pop(q.pq).(*Item)
	delete(q.ds, item.value)
	return item.value, item.priority
}
func (q *Q) get(u int) (int, bool) { // returns d
	d, ok := q.ds[u]
	return d, ok
}
func (q *Q) set(u, d int) {
	q.ds[u] = d
	for _, item := range *q.pq {
		if item.value != u {
			continue
		}

		item.priority = d
		heap.Fix(q.pq, item.index)
		return
	}
	heap.Push(q.pq, &Item{
		priority: d,
		value:    u,
	})
}
func (q *Q) len() int {
	return len(q.ds)
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := newQ()
	q.set(0, 0)
	visited := make(map[int]struct{})
	out := make([]int, n)

	for q.len() > 0 {
		u, d := q.pop()
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			dd := d + c
			if d, ok := q.get(uu); !ok || d > dd {
				q.set(uu, dd)
			}
		}

		out[u] = d
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, d)
	}
}

C++: #include <iostream>
#include <cstring>
#define MAX 120000
using namespace std;

int matrix[105][105],n,closest[105],close[105];

void djst(){
    int i,j,k;
    for(i=1;i<n;i++){
        if(matrix[0][i]<MAX)close[i]=matrix[0][i];
    }
    while(1){
        int mi=MAX,rec=-1;
        for(i=1;i<n;i++){
            if(close[i]<mi&&closest[i]==-1){
                mi=close[i];
                rec=i;
            }
        }
        if(rec==-1)break;
        closest[rec]=mi;
        close[rec]=MAX;
        i=2;
        while(i<n){
            for(j=0;j<n;j++){
                if(closest[j]==-1&&mi+matrix[rec][j]<close[j])close[j]=mi+matrix[rec][j];
            }
        }
    }
}

int main()
{
    memset(close,MAX,sizeof(close));
    memset(closest,-1,sizeof(closest));
    memset(matrix,MAX,sizeof(matrix));
    int i,j;
    cin>>n;
    for(i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        for(j=0;j<b;j++){
            int c,d;
            cin>>c>>d;
            matrix[a][c]=d;
        }
    }
    closest[0]=0;
    djst();
    for(i=0;i<n;i++){
        cout<<i<<" "<<closest[i]<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cstring>
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(true)
	{
		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(color[v]!=BLACK&&M[u][v]!=INFTY)
			{
				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()
{
	scanf("%d",&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++)
	{
		scanf("%d%d",&u,&k);
		for(int j=0;j<k;j++)
		{
			scanf("%d%d",&v,&c);
			M[u][v]=c;
		}
	}
	dijkstra();
}

C++: #include <iostream>
#include <cstdio>
#pragma warning(disable:4996)
using namespace std;
const int maxn = 100 + 10;
const int maxv = 10000 + 10;
const int INF = 1 << 21;
int n;
int E;
typedef struct{
	int from, to, cost;
}Edge;

Edge e[maxv];
int d[maxn];
void bellman_ford(int s){
	fill(d, d + n, INF);
	d[s] = 0;
	while (true)	{
		bool bUpdate = false;
		for (int i = 0; i<E; i++)	{
			Edge es = e[i];
			if (d[es.from] != INF&&d[es.to]>d[es.from] + es.cost)		{
				d[es.to] = d[es.from] + es.cost;
				bUpdate = true;
			}
		}
		if (!bUpdate) break;
	}
	for (int i = 0; i<n; i++) printf("%d %d\n", i, d[i]);
}

int main(){
	scanf("%d", &n);
	int id, c;
	E = 0;
	for (int i = 0; i<n; i++){
		scanf("%d%d", &id, &c);
		while (c--){
			scanf("%d%d", &e[E].to, &e[E].cost);
			e[E].from = id;
			E++;
		}
	}
	bellman_ford(0);
	return 0;
}

C++: /*宣言*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<string.h>
#include<string>
#include<vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
int main()
{
	int n, u, k, w[101][101], v, c;
	int d[101], kas[101] = { 0 }, K;
	cin >> n;
	memset(d, 0x7f, sizeof(d));
	memset(w, 0x7f, sizeof(w));
	rep(i, n)
	{
		cin >> u >> k;
		rep(j, k)
		{
			cin >> v >> c;
			w[u][v] = c;
		}
	}
	d[0]=0;
	int p[101];
	int Min,Min1;
	Min = 0;
	Min1 = 0x7f;
	kas[0] = 1;
	rep(j, n)
	{
		rep(i, n)
		{
			if (d[i] > d[Min] + w[Min][i])
			{
				d[i] = d[Min] + w[Min][i];
				p[i] = Min;
			}
			if (Min1 > d[i] && kas[i] == 0)
			{
				Min1 = d[i];
				K = i;
			}
		}
		kas[K] = 1;
		Min = K;
		Min1 = 0x7f;
	}
	rep(i, n)
	{
		cout << i <<' '<< d[i] << endl;
	}
	return 0;
}

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

static const int N = 100;
static const int inf = (1 << 30);
static const int white = 0;
static const int gray = 1;
static const int black = 2;

int n, M[N][N];

void dijkstra(int s) {
	int color[N], d[N];
	for (int i = 0; i < n; i++) {
		color[i] = white;
		d[i] = inf;
		//p[n] = inf;
	}
	d[s] = 0;
	color[s] = gray;
	int u = -1;
	while (true) {
		int mincost = inf;
		for (int i = 0; i < n; i++) {
			if (color[i] != black && d[i] < mincost) {
				mincost = d[i];
				u = i;
			}
		}
		if (mincost == inf) {
			break;
		}
		color[u] = black;
		for (int v = 0; v < n; v++) {
			if (color[v] != black && M[u][v] != inf) {
				if (d[v] > d[u] + M[u][v]) {
					d[v] = d[u] + M[u][v];
					color[v] = gray;
					//p[v] = u;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << i << " " << (d[i] == inf ? -1 : d[i]) << '\n';
	}
	//return  0;
}

int main() {
	int u, k, v, c;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			M[i][j] = inf;
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v >> c;
			M[u][v] = c;
		}
	}
/*	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (M[i][j] == inf) {
				cout << -1  << " ";
			} else {
				cout << M[i][j] << " ";
			}
		}
		cout << '\n';
}
*/
	dijkstra(0);
	return 0;
}