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<bits/stdc++.h>
using namespace std;
#define INF 1000000000

int n;
int g[100][100];
int d[100],p[100];

void dijkstra(int start){
  d[start]=0;
  int mn,t,cnt=0;
  while(cnt<=n){
    mn=INF;
    for(int i=0;i<n;i++){
      if(p[i]==0 && mn>d[i]){
	mn=d[i];
	t=i;
      }
    }
    for(int i=0;i<n;i++){
      d[i]=min(g[t][i]+d[t],d[i]);
    }
    p[t]=1;
    cnt++;
  }
}


    
int main(){
  int u,k,v,c;
  for(int i=0;i<100;i++){
    p[i]=0;
    d[i]=INF;
    for(int j=0;j<100;j++){
      if(i==j) g[i][j]=0;
      else g[i][j]=INF;
    }
  }
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
      cin>>v>>c;
      g[u][v]=c;
    }
  }
  dijkstra(0);
  for(int i=0;i<n;i++){
    cout<<i<<" "<<d[i]<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

//????????¨???????????????????????°??????????????\?????????????????????

#define MAX 100

using Weight = int;
struct Edge {
	int s; //source
	int d; //destination
	Weight w; //weight
	Edge(int s = 0, int d = 0, int w = INF) : s(s), d(d), w(w) {};
};
using Edges = vector<Edge>;
//???????????\???
//g[u].push_back(Edge(u, v, c));
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

//?????¢?´¢, ??¢?´¢???, ??¢?´¢???
enum { WHITE, GRAY, BLACK };

//???????§????????????? (Dijkstra)
//dist: ?§????????????????????????§??????????????¢
//prev: ???????????¨????????????
void dijkstra(Graph g, int s, vector<Weight> &dist, vector<int> &prev) {
	typedef tuple<Weight, int, int> State; //?§?????????????????°?????????? ??? ???
	priority_queue<State, vector<State>, greater<State> >PQ;
	vector<int> color(g.size(), WHITE);
	dist.assign(g.size(), INF); //distance
	prev.assign(g.size(), -1);
	dist[s] = 0; PQ.push(State(0, s, -1)); color[s] = GRAY; //?§????
	while (!PQ.empty()) {
		Weight d; int v; int u;
		tie(d, v, u) = PQ.top(); PQ.pop(); //dist???????°??????¨??????????´????????????????
		if (dist[v] < d) continue; //?????§???????????§???????????°??????
		color[v] = BLACK;
		prev[v] = u;
		for (auto e : g[v]) { //??£??\????????????????????¨????????????		
			if (color[e.d] == BLACK)continue; //??¢?´¢????????????
			if (dist[e.d] > dist[v] + e.w) {
				dist[e.d] = dist[v] + e.w;
				PQ.push(State(dist[e.d], e.d, v));
				color[e.d] = GRAY;
			}
		}
	}
}

//?§?????????????????????§??????????????????
//???????????????????????°?????????????§??????????????????§?????????????????¢????????????
vector<int> getPath(int s, int g, vector<int> prev) {
	vector<int> path;
	for (int i = g; i != s; ) path.push_back(i = prev[i]);
	reverse(path.begin(), path.end());
	return path;
}

//???????????????
void printPath(vector<int> path) {
	cout << "Path: ";
	for (auto p : path) cout << p << " ";
	cout << endl;
}

int main() {
	int n; cin >> n;
	Graph g(n);
	for (int i = 0; i < n; i++) {
		int u, k; scanf("%d%d", &u, &k);
		for (int j = 0; j < k; j++) {
			int v, c; scanf("%d%d", &v, &c);
			g[u].push_back(Edge(u, v, c));
		}
	}
	vector<Weight> dist;
	vector<int> prev;
	dijkstra(g, 0, dist, prev);
	for (int i = 0; i < n; i++) {
		printf("%d %d\n", i, dist[i]);
		//printPath(getPath(0, i, prev));
	}
	return 0;
}

C++: #include<iostream>
#include<climits>
using namespace std;

typedef struct{
  int A[1000];
  int ans;
  bool c;
}node;
node T[1000];
int Q[10000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    num=Q[head++];
    T[num].c=true;
    for(int i=0;i<n;i++){
      if(T[num].A[i]!=0){
	//	cout<<T[num].ans<<endl;
	if(T[i].ans>T[num].ans+T[num].A[i]){
	  T[i].ans=T[num].ans+T[num].A[i];
	  T[num].A[i]=0;
	Q[tail++]=i;
	}
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    //  cout<<head<<" "<<tail<<endl;
  }

}

int main(){
  int num;
  int son;
  int z,w;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    T[num].ans=INT_MAX;
    for(int j=0;j<son;j++){
      cin>>z>>w;
      T[num].A[z]=w;
      }
  }
  /*  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout<<i<<" "<<j<<" "<<T[i].A[j]<<endl;
    }
    }*/
  Q[0]=0;
 T[0].ans=0;
  function(0);
  T[0].ans=0;

  /* 
for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
    }
*/

  for(int i=0;i<n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int N;
int G[100][100];
int d[100];
int used[100];
const int INF=1000000000;
void dijkstra(int s){
  for(int u=0;u<N;u++)d[u]=INF;
  d[s]=0;
  while(1){   
    int v=-1;
    for(int u=0;u<N;u++){
      if((v==-1 || d[v]>d[u]) && !used[u])v=u;
    }
    if(v==-1)break;
    used[v]=1;
    for(int u=0;u<N;u++){
      d[u]=min(d[u],d[v]+G[v][u]);
    }
  }
}
int main(){

  for(int i=0;i<100;i++){
    for(int j=0;j<100;j++){
      G[i][j]=INF;
      if(i==j)G[i][j]=0;
    }
  }
  scanf("%d",&N);
  for(int i=0;i<N;i++){
    int u,k,c,v;
    scanf("%d %d",&u,&k);
    for(int j=0;j<k;j++){
      scanf("%d %d",&v,&c);
      G[u][v]=c;
     
    }
  }
  dijkstra(0);
  for(int i=0;i<N;i++)printf("%d %d\n",i,d[i]);
  return 0;
}