Go: package main

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

const (
	MAX_N      = 10000
	MAX_WEIGHT = 100000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix, err := createAdjacentMatrix(n, sc)
	if err != nil {
		panic(err)
	}

	sssp([]int{0}, shortestList, adjacentMatrix)
	for _, v := range shortestList {
		fmt.Printf("%d %d\n", v.getEnd(), v.shortest)
	}
}

func (si ShortestInfo) getEnd() int {
	if len(si.edges) == 0 {
		return 0
	} else {
		return si.edges[len(si.edges)-1].end
	}
}

func createAdjacentMatrix(n int, sc *bufio.Scanner) ([][]int, error) {
	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			return nil, err
		}
		adjacentMatrix[id] = make([]int, n)
		for j := 0; j < n; j++ {
			adjacentMatrix[id][j] = -1
		}
		vertexNum, err := nextInt(sc)
		if err != nil {
			return nil, err
		}
		for j := 0; j < vertexNum; j++ {
			v, err := nextInt(sc)
			if err != nil {
				return nil, err
			}
			c, err := nextInt(sc)
			if err != nil {
				return nil, err
			}
			adjacentMatrix[id][v] = c
		}
	}
	return adjacentMatrix, nil
}

func sssp(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			if currentShortest.shortest+adj.weight < getShortest(adj.end, shortestList).shortest {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode]
	edges := []Edge{}
	for i, v := range adjacents {
		if v >= 0 {
			edges = append(edges, Edge{start: currentNode, end: i, weight: v})
		}
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define MAX 10000
#define INFTY (1<<20)
#define WHITE 0//未発見
#define GRAY 1//発見済み未確定
#define BLACK 2//確定済み

int n;//num of vertexes
vector<pair<int,int>> v_list[MAX+1];//頂点の隣接リスト(vector)を要素として持つ配列(添え字が頂点番号)

//http://homepage2.nifty.com/well/Operator.html#PAREN
class greaterInt{
public:
	bool operator() (pair<int,int>& a,pair<int,int>& b){
		return a.second > b.second;
	}
};

void dijkstra(){
	priority_queue<pair<int,int>,vector<pair<int,int>>,greaterInt> PQ;//<頂点番号,挿入時点での最短距離>
	int color[MAX];//各頂点の状態を格納する配列
	int d[MAX];//各頂点までの現在の最短距離を格納する変数

	for(int i=0;i<n;i++){
		d[i] = INFTY;
		color[i] = WHITE;
	}

	d[0] = 0;
	PQ.push(make_pair(0,0));//make_pairを使うと勝手にpairで宣言した型に直してくれる
	color[0] = GRAY;

	//PQ.empty() == trueのときこれ以上更新はなくこれまであった更新に関してはすべて処理
	//が終わっている状態
	while( !PQ.empty() ){
		pair<int,int> f = PQ.top();PQ.pop();
		int u = f.first;//今回確定する頂点番号
		
		if( color[u] == BLACK ) continue;//continue break http://kotazi.com/archives/1383
		//以下ではf.second == d[u]が保障される

		color[u] = BLACK;

		for(int j = 0;j < v_list[u].size();j++ ){
			int v = v_list[u][j].first;//はじめの[]は配列要素番号，二つ目の[]はベクトルの要素番号
			if(color[v] == BLACK) continue;
			if( d[v] > d[u] + v_list[u][j].second){
				d[v] = d[u] + v_list[u][j].second;
				PQ.push(make_pair(v,d[v]));
				color[v] = GRAY;
			}
		}
	}

	for(int i = 0;i < n;i++){
		cout <<i <<" " <<(d[i] == INFTY ? -1 : d[i]) <<endl;//http://msmori.blogspot.jp/2011/01/3.html
		//cout <<i <<" " <<d[i] <<endl;
	}
}

int main(){
	int k, u, v, c;

	cin >>n;
	for(int i = 0;i < n;i++){
		cin >>u >>k;
		for(int j = 0;j < k;j++){
			cin >>v >>c;
			v_list[u].push_back(make_pair(v,c));
		}
	}
	dijkstra();


	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;

#define MAX 10001
#define INF 5000001
#define NIL -1

int a[MAX][MAX];
int n;
int d[MAX];
int pi[MAX];

void Relax(int u, int v){
  if(d[v]> d[u]+a[u][v]){
    d[v]=d[u]+a[u][v];
    pi[v]=u;
  }
}

void ISS(int s){
  int v;
  for(v=0; v<n; v++){
    d[v]=INF;
    pi[v]=NIL;
  }
  d[s]=0;
}

void Dijkstra(int s){
  int i, v1, v, minv, u; 
  int flag[MAX];
  ISS(s);
  
  for(i=0; i<n; i++) flag[i]=0;

  for(v1=0; v1<n; v1++){
    
    minv = INF;
    // decide u
    for ( i = 0; i < n; i++ ){
      if(flag[i]==1) continue; 
      if(d[i]<=minv){
    u=i;
    minv=d[i];
      }
    }
    
    for(v=0; v<n; v++){
      if(a[u][v]==-1) continue;
      if(flag[v]==1) continue;
      /* if(a[u][v]<d[v]){
    pi[v]=u;
    d[v]=a[u][v];
    }*/
      Relax(u, v);
    }
    flag[u]=1;
  }
}

int main(){

  int i, j ,u ,k,v,c;
  // int a[MAX][MAX];
  cin>>n;

  for(i=0; i<n; i++){
    for(j=0; j<n; j++){
      a[i][j]=-1;
    }
  }

  for(i=0; i<n; i++){
      cin>>u>>k;
    for(j=0; j<k; j++){
        cin>>v>>c;
        a[i][v]=c;
    }
  }

  /*for(i=0; i<n; i++){
    for(j=0; j<n; j++){
    cout<<a[i][j]<<endl;
    }
    cout<<endl;
    }*/
  Dijkstra(0);
  for(i=0; i<n; i++){
      cout<<i<<" "<<d[i]<<endl;
  }

  return 0;
}