Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const (
	white int = iota
	gray
	black
)

type node struct {
	id     int
	parent int
	edge   []int
	weight []int
	color  int
}

type graph struct {
	nodes    []node
	distance []int
}

var infty = 1 << 32
var null = -1

func constructgraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n := nextInt(sc)

	g := &graph{make([]node, n), make([]int, n)}

	for i := 0; i < n; i++ {
		id := nextInt(sc)
		edges := nextInt(sc)
		n := node{}
		n.id = id
		n.edge = make([]int, edges)
		n.weight = make([]int, edges)
		n.color = white
		for j := 0; j < edges; j++ {
			to := nextInt(sc)
			w := nextInt(sc)
			n.edge[j] = to
			n.weight[j] = w
		}
		g.nodes[id] = n
	}
	return g
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func (g *graph) preparedijkstra(s int) {
	for i := 0; i < len(g.nodes); i++ {
		g.nodes[i].color = white
		g.nodes[i].parent = null
		g.distance[i] = infty
	}
	g.distance[s] = 0
}

func (g *graph) dijkstra(s int) []int {
	g.preparedijkstra(s)

	for {
		var u int
		mincost := infty
		for i, d := range g.distance {
			if g.nodes[i].color != black && mincost > d {
				mincost = d
				u = i
			}
		}

		if mincost == infty {
			break
		}

		g.nodes[u].color = black

		for i, e := range g.nodes[u].edge {
			if g.nodes[e].color != black {
				if g.distance[e] > g.distance[u]+g.nodes[u].weight[i] {
					g.distance[e] = g.distance[u] + g.nodes[u].weight[i]
					g.nodes[e].parent = u
					g.nodes[e].color = gray
				}
			}
		}
	}
	return g.distance
}

func printShortestPath(d []int) {
	for i, v := range d {
		fmt.Printf("%d %d\n", i, v)
	}
}

func main() {
	g := constructgraph()
	d := g.dijkstra(0)
	printShortestPath(d)
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define D_MAX 1 << 21
#define N_MAX 100
using namespace std;

int dis[N_MAX];
int n;

struct node {
  int to;
  int cost;
  node(int to,int cost) {
    this->to = to;
    this->cost = cost;
  }
};

void dijkstra(int st,vector< vector<node*> > &nodes) {
  for(int i=0;i<n;i++) dis[i] = D_MAX;
  dis[st] = 0;
  bool visited[n]; for(int i=0;i<n;i++) visited[i] = false;
  vector<int> vertexes;
  visited[st] = true;
  vertexes.push_back(st);
  int pre_cost = 0;
  while(vertexes.size() < n) {
    // update distance list
    int v = vertexes[vertexes.size()-1];
    for(int j=0;j<nodes[v].size();j++) {
      int u = nodes[v][j]->to;
      if(visited[u]) continue;
      int cost = nodes[v][j]->cost + pre_cost;
      dis[u] = min(dis[u],cost);
    }
    // find vertex which has smallest distance.
    int next_v;
    int minimum = D_MAX;
    for(int i=0;i<n;i++) {
      if(visited[i]) continue;
      if(dis[i] < minimum) {
        minimum = dis[i];
        next_v = i;
      }
    }
    visited[next_v] = true;
    vertexes.push_back(next_v);
    pre_cost = dis[next_v];
  }
}

int main() {
  scanf("%d",&n);
  int u,k,v,c;
  vector<vector< node*> > nodes;
  nodes.resize(n);
  for(int i=0;i<n;i++) {
    scanf("%d %d",&u,&k);
    for(int j=0;j<k;j++) {
      scanf("%d %d",&v,&c);
      nodes[u].push_back(new node(v,c));
    }
  }

  // check
  /*
  for(int i=0;i<nodes.size();i++) {
    for(int j=0;j<nodes[i].size();j++) {
      printf("%2d :%2d %2d\n",i,nodes[i][j]->to,nodes[i][j]->cost);
    }
  }
  */

  dijkstra(0,nodes);
  for(int i=0;i<n;i++) {
    printf("%d %d\n",i,dis[i]);
  }
}

C++: #include <stdio.h>

using namespace std;


int main(){
	int n,count,t,min,min_index,tmp,left_node,right_node,dist;
	scanf("%d",&n);
	int distance[n][n],P[n];

	for(int i=0;i<n;i++){
		for(int k=0;k<n;k++)distance[i][k]=100001;
		P[i]=0;
	}
	distance[0][0] = 0;
	P[0]=1;

	for(int i=0;i<n;i++){
		scanf("%d %d",&left_node,&tmp);
		for(int k=0;k<tmp;k++){
			scanf("%d %d",&right_node,&dist);
			distance[left_node][right_node] = dist;
		}
	}

	while(count<n-1){

		min=100001;
		for(int i=1;i<n;i++){
			if(P[i]==0&&distance[0][i]<min){
				min_index = i;
				min = distance[0][i];
			}
		}
		t = min_index;
		P[t]=1;
		count++;

		for(int i=0;i<n;i++){
			if(distance[0][i] > distance[0][t]+distance[t][i]){
				distance[0][i]=distance[0][t]+distance[t][i];
			}
		}
	}

	for(int i=0;i<n;i++){
		printf("%d %d\n",i,distance[0][i]);
	}

    return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1e9
using namespace std;
int M[100][100],d[100],p[100];
bool vist[100]={0};
void prim(int n){
	for(int i=0;i<n;i++) d[i] = INF;
	d[0] = 0;
	p[0] = -1;
	int mincost, u;
	while(1){
		mincost = INF;
		for(int i=0;i<n;i++)
			if(!vist[i] && d[i]<mincost)
				mincost = d[i], u = i;
		if(mincost == INF) break;
		vist[u] = 1;
		for(int i=0; i<n; i++)
			if(!vist[i] && M[u][i]!=-1 && d[u]+M[u][i]<d[i])
				d[i] = M[u][i] + d[u],
				p[i] = u;
	}
}
int main()
{	
	memset(M,-1,sizeof(M));
	int n,du,v,u; cin>>n;
	for(int i = 0;i < n;i++){
		scanf("%d%d",&v,&du);
		while(du--)
			scanf("%d",&u),
			scanf("%d",&M[v][u]);
	}
	prim(n);
	for(int i=0; i<n; i++)
		printf("%d %d\n",i,d[i]);
	return 0;
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP0(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP1(i,n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define RREP0(i,n) for(int i=n-1; i>=0; --i)
#define RREP1(i,n) for(int i=n; i>=1; --i)

#define SORT(c) sort((c).begin(),(c).end())
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define CLR(a) memset((a), 0 ,sizeof(a))

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

const int INTINF = 2147483647;
const LL LLINF = 9223372036854775807;
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
const int MAX = 100;
int n, M[MAX][MAX];
int d[MAX], color[MAX];

struct init{
    init(){
        cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);
    };
} init;


void dijkstra(){
  int minv;

  REP0(i,n){
    d[i] = INTINF;
    color[i] = WHITE;
  }

  d[0] = 0;
  color[0] = GRAY;

  while (1) {
    minv = INTINF;
    int u = -1;
    // 未確定頂点のうち最も近いもの := u を選んで確定
    REP0(i,n){
      if ( minv > d[i] && color[i] != BLACK ){
        u = i;
        minv = d[i];
      }
    }
    if ( u == -1 ) break;
    color[u] = BLACK;
    // uから到達可能な未確定頂点を選んで最短距離を更新していく
    REP0(v,n){
      if ( color[v] != BLACK && M[u][v] != INTINF) {
        if ( d[v] > d[u] + M[u][v] ) {
          d[v] = d[u] + M[u][v];
          color[v] = GRAY;
        }
      }
    }
  }
}



void input() {
  std::cin >> n;
  REP0(i,n) REP0(j,n) M[i][j] = INTINF;

  int u, k, v, c;
  REP0(i,n){
    std::cin >> u >> k;
    REP0(j,k){
      std::cin >> v >> c;
      M[u][v] = c;
    }
  }
}


void output(){
  REP0(i,n){
    std::cout << i << " " << ( d[i] == INTINF ? -1 : d[i] ) << '\n';
  }
}


int main(int argc, char const *argv[]) {
  input();
  dijkstra();
  output();
  return 0;
}