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()

	list := make([][]int, n)
	weight := make([]int, n)
	for i := range list {
		list[i] = make([]int, n)
		weight[i] = NIL
		for j := range list[i] {
			list[i][j] = NIL
		}
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		for j := 0; j < k; j++ {
			v := scanInt()
			w := scanInt()
			list[u][v] = w
		}
	}

	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 i, w := range list[node.id] {
			if w == NIL {
				continue
			}
			heap.Push(h, Node{id: i, weight: w + node.weight})
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		fmt.Fprint(w, i)
		fmt.Fprint(w, " ")
		fmt.Fprint(w, weight[i])
		fmt.Fprintln(w)
	}
	w.Flush()
}

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;
}