Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [][]int

func (g *Graph) dikstra(n int) string {
	var (
		u, minv = 0, 0
	)

	d := make([]int, n)
	color := make([]int, n)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		color[i] = WHITE
	}
	d[0] = 0
	color[0] = GRAY

	for {
		minv = INFTY
		u = -1
		for 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 v := 0; v < n; v++ {
			if color[v] != BLACK && (*g)[u][v] != INFTY {
				if d[v] > d[u]+(*g)[u][v] {
					d[v] = d[u] + (*g)[u][v]
					color[v] = GRAY
				}
			}
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		out.WriteString(fmt.Sprintf("%d ", i))
		if d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d\n", -1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d\n", d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make(Graph, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			g[i][j] = INFTY
		}
	}

	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())

		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())

			g[u][v] = c
		}
	}

	return g.dikstra(n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int main() {

	int INF = 100001;

	int n;
	scanf("%d", &n);

	vector<pair<int, int>> G[n];
	for (int i = 0; i < n; ++i) {
		int from;
		scanf("%d", &from);

		int v;
		scanf("%d", &v);

		for (int j = 0; j < v; ++j) {
			int to;
			int w;
			scanf("%d %d", &to, &w);
			G[from].push_back(make_pair(to, w));
		}
	}

	int C[n];
	for (int i = 0; i < n; ++i)
		C[i] = INF;

	int color[n];
	int WHITE = 0;
	int BLACK = 1;
	for (int i = 0; i < n; ++i)
		color[i] = WHITE;

	priority_queue<pair<int, int>> PQ;
	PQ.push(make_pair(0, 0));
	C[0] = 0;
	while (!PQ.empty()) {
		int u = PQ.top().second;
		PQ.pop();
		color[u] = BLACK;
		for (int i = 0; i < (int) G[u].size(); ++i) {
			auto c = G[u][i];
			if (color[c.first] != BLACK && c.second + C[u] < C[c.first]) {
				C[c.first] = c.second + C[u];
				PQ.push(make_pair(C[c.first] * -1, c.first));
			}
		}
	}

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

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <list>
#include <stack>
#include <cctype>
#include <cmath>

using namespace std;

/* typedef */

typedef long long ll;

/* constant */

const int INF = 1 << 30;
const int MAX = 100;
const int mod = 1000000007;
const double pi = 3.141592653589;

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

/* global variables */
vector< vector<pair<int, int> > > Adj(MAX, vector<pair<int, int> >());
vector<int> color(MAX, WHITE);
vector<int> d(MAX, INF);
vector<int> p(MAX, -1);
int n;

/* function */
void dijkstra();

/* main */
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));
        }
    }

    dijkstra();

    for (int i = 0; i < n; i++ ) {
        cout << i << ' ' << d[i] << '\n';
    }
   

}

void dijkstra() {

    pair<int, int> p1;
    d[0] = 0;
    p[0] = -1;

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

    while (!PQ.empty()) {

        p1 = PQ.top(); PQ.pop();
        int u = p1.second;
        color[u] = BLACK;

        if ( d[u] < p1.first * (-1) ) continue;

        for (auto p2 : Adj[u]) {

            int v = p2.first;
            int cost = p2.second;

            if (color[v] != BLACK) {
                if (d[u] + cost < d[v]) {
                    p[v] = u;
                    d[v] = d[u] + cost;
                    color[v] = GRAY;
                    PQ.push(make_pair(-d[v], v));
                }
            }
        }
    }

}

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

using namespace std;
int d[10002];
struct Edge{
    int from,to,w;
};
const int inf=1<<30;
vector<Edge> E;
vector<int> G[10002];
struct node{
    int to,w;
    friend bool operator < (const node& a, const node& b){
        return a.w>b.w;
    }
};
bool vis[10002];
int V;
void dijkstar(int start=0){
    priority_queue<node> que;
    que.push(node {start,0});
    d[start]=0;
    while(!que.empty()){
        node s=que.top();
        que.pop();
        int nowV=s.to,w=s.w;
        if(vis[nowV]) continue;
        vis[nowV]=true;
        for(int i=0;i<G[nowV].size();i++){
            Edge ee=E[G[nowV][i]];
            if(d[ee.to]>d[nowV]+ee.w){
                d[ee.to]=d[nowV]+ee.w;
                que.push(node {ee.to,d[ee.to]});
            }
        }
    }
}

int main(){
    cin>>V;
    for(int i=0;i<V;i++){
        int id,k;
        cin>>id>>k;
        vis[i]=false;
        d[i]=inf;
        while(k--){
            int out,val;
            cin>>out>>val;
            E.push_back(Edge{id,out,val});
            G[id].push_back(E.size()-1);
        }
    }
    dijkstar();
    for(int i=0;i<V;i++){
        cout<<i<<" "<<d[i]<<endl;
    }
}

C++: #include<iostream>
#include<cstdio>
#include<climits>
#include<cstring>
#include<algorithm>
#define INF 1<<25
using namespace std;
typedef long ull;
 
int main(){
  int n;
  int A[1000][1000];
  ull D[1000][1000];
  scanf("%d",&n);
  int u,k,C,V;
  
  for(int i=0;i<=n;i++){
    for(int j=0;j<=n;j++)D[i][j] = INF;
  }
  
  for(int i=0;i<n;i++){
    scanf("%d %d",&u,&k);    
    for(int j=0;j<k;j++){
      scanf("%d %d",&V,&C);
      D[u][V] = C;
      D[V][u] = C;
    }    
  }
  
  for(int i=0;i<n;i++)D[i][i] = 0;
  for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j] = min(D[i][j],D[i][k] + D[k][j]);
  for(int i=0;i<n;i++){
    cout << i << " " << D[0][i] << endl;
  }
  
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#define inf 10000000
#define W 0
#define G 1
#define B 2
using namespace std;
int n,color[100];
int g[100][100],d[100],p[100];

void dikstr(int st){
int u,i,j;
	for(i=0;i<n;i++){
	color[i]=W;d[i]=inf;
	}
	d[st]=0;
	p[st]=-1;
	while(true){
	int mincost=inf;
		for(i=0;i<n;i++){
			if(color[i]!=B && d[i]<mincost){
			mincost=d[i];
				u=i;
			}
			
		}
		if(mincost==inf)
		break;
		color[u]=B;
		for(int v=0;v<n;v++){
			if(color[v]!=B && g[u][v]!=inf){
				if(d[u]+g[u][v]<d[v]){
				d[v]=d[u]+g[u][v];
					p[v]=u;
					color[v]=G;
				}
			}
		}
	}
}
main(){
	int i,j,u,k,c,v;
	cin>>n;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++)
		g[i][j]=inf;
	}
	for(i=0;i<n;i++){
	cin>>u>>k;
		for(j=0;j<k;j++){
		cin>>v>>c;
			g[u][v]=c;
		}
	}
	dikstr(0);
	
	for(i=0;i<n;i++)
	cout<<i<<" "<<d[i]<<endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
const int INF=1000000000;
int G[100][100];
int d[100],used[100];
int n;
void dijkstra(int s){
    fill(d,d+n,INF);
    fill(used,used+n,false);
    
    d[s]=0;
    while(true){
	int v=-1;
	for(int u=0;u<n;u++){
	    if(used[u]) continue;
	    if(v==-1||d[u]<d[v]) v=u;
	}
	if(v==-1) break;
	used[v]=true;

	for(int u=0;u<n;u++){
	    d[u]=min(d[u],d[v]+G[v][u]);
	}
    }
}
int main(){
    cin>>n;
    fill(G[0],G[n],INF);
    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;
	    G[u][v]=G[v][u]=c;
	}
    }
    dijkstra(0);
    for(int i=0;i<n;i++){
	cout<<i<<" "<<d[i]<<endl;
    }

    return 0;
}