Go: package main

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

const BIG_COST = 200000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

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

	nNodes := scanInt(sc)

	costs := make([][]int, nNodes)
	graph := make([][]int, nNodes)

	states := make([]int, nNodes)
	costSums := make([]int, nNodes)

	for i := 0; i < nNodes; i++ {
		costs[i] = make([]int, nNodes)
		costSums[i] = BIG_COST
	}

	for i := 0; i < nNodes; i++ {
		u := scanInt(sc)
		k := scanInt(sc)
		graph[u] = make([]int, k)
		for d := 0; d < k; d++ {
			v := scanInt(sc)
			graph[u][d] = v
			costs[u][v] = scanInt(sc)
		}
	}

	costSums[0] = 0
	for {
		minCost := BIG_COST
		var u int

		for i, cost := range costSums {
			if states[i] != ST_JOINED && cost < minCost {
				minCost = cost
				u = i
			}
		}

		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for _, v := range graph[u] {
			cost := minCost + costs[u][v]
			if cost < costSums[v] {
				costSums[v] = cost
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i, cost := range costSums {
		fmt.Fprintf(w, "%d %d\n", i, cost)
	}
	w.Flush()
}

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

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

int x[1000][1000];
int y[1000];

int main() {
	memset(x, 127, sizeof(x));
	memset(y, 127, sizeof(y));
	int n, a, b, c, d;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a >> b;
		for (int i = 0; i < b; i++) {
			cin >> c >> d;
			x[a][c] = d;
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < n; k++) {
				y[k] = min(y[k], y[j] + x[j][k]);
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << i << ' ' << y[i] << endl;
	}
	return 0;
}

C++: #define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

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

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 1000000010

int n;
int M[100][100];
int color[100],d[100],p[100];

void dik(int s){
  
  rep(i,n){
    color[i]=WHITE;
    d[i]=INF;
    p[i]=-1;
  }
  d[s]=0;
  
  int minc,u;
  while(1){
    minc=INF; 
    rep(i,n){
      if(color[i] != BLACK && d[i]<minc){
        minc=d[i];
        u=i;
      }
    }
    
    if(minc==INF) break;
    color[u]=BLACK;
    
    rep(v,n){
      if(color[v] != BLACK && M[u][v] != -1){
        if(d[u]+M[u][v]<d[v]){
          d[v]=d[u]+M[u][v];
          p[v]=u;
          color[v]=GRAY;
        }
      }
    }    
  }
  
}

int main(){
  cin>>n;
  
  rep(i,n){
    rep(j,n) M[i][j]=INF;
  }
  
  int x,y,z;
  rep(i,n){
    cin>>x>>y;
    rep(j,y){
      cin>>z;
      cin>>M[x][z];
    }
  }
  
  dik(0);
  rep(i,n) cout<<i<<" "<<d[i]<<endl;
  
  return 0;
}

C++: //Dijkstra法(蟻本p96)
//負の辺がない時のグラフの最短路
//値の代入と最小値の取り出し→priority_queueを利用
#include<bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repr(i,a,b) for(int i=(a); i>=(b); i--)
#define all(x) (x).begin(),(x),end()
typedef long long ll;

struct edge{int to,cost;};//辺の状態(toはどのノードに伸びているか、costは重み)
typedef pair<int,int> P;//firstは最短距離、secondは頂点の番号

const int INF=1<<29;
int N;//ノードの数
vector<edge> G[10010];//グラフの隣接リスト表示
int d[10010];//各頂点の最短距離

void dijkstra(int s){
    //greater<P>を指定しておくことでfirstが小さい順に取り出せるようにする
    priority_queue<P,vector<P>,greater<P> > que;
    fill(d,d+N,INF);//始めに全てのノードへの最短距離をINFとする
    d[s]=0;//探索を開始する地点の最短距離を0に設定
    que.push(P(0,s));//最短距離0で頂点sへのびる辺をqueに入れてスタート
    
    while(!que.empty()){//queueが空==全ての辺の探索が終わったので終了
        P p=que.top();//queueの先頭を取得←最も重みの小さい辺を取得
        que.pop();
        int v=p.second;//もっとも重みの小さい辺が向かうノード
        if(d[v]<p.first)continue;//ノードvへの最短距離が既に求まっている場合はスルー
        //ノードvから伸びている辺を全てqueに入れる(ダイクストラの最短路の候補)
        for(int i=0; i<G[v].size(); i++){
            edge e=G[v][i];
            //辺eの向かうノードが既に探索し終わっていて、かつそれが更新されないとき
            if(d[e.to]>d[v]+e.cost){
                d[e.to]=d[v]+e.cost;
                //queに辺の情報を入れる
                //d[e.to](暫定で)始点からの距離d[e.to]
                //ノードe.toへ向かう辺
                que.push(P(d[e.to],e.to));
            }
        }
    }
}

int main(){
    
    //入力
    cin>>N;
    rep(i,0,N){
        int a,b;
        cin>>a>>b;
        rep(j,0,b){
            int c,d;
            cin>>c>>d;
            G[a].push_back({c,d});
        }
    }
    
    dijkstra(0);
    
    rep(i,0,N){
        cout<<i<<" "<<d[i]<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<stdlib.h>
#include<limits>

using namespace std;

static const int MAX=100;
static const int INFTY=numeric_limits<int>::max();

typedef enum {
    WHITE,
    GRAY,
    BLACK,
}Color;

int n,M[MAX][MAX];

void Dijkstra(){
    int minv;
    int d[MAX];
    Color color[MAX];

    for(int i=0;i<n;i++){
        d[i]=INFTY;
        color[i]=WHITE;
    }
    
    d[0]=0;
    color[0]=GRAY;
    while(true){
        minv=INFTY;
        int u=-1;
        for(int 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(int v=0;v<n;v++){
            if(color[v]!=BLACK&&M[u][v]!=INFTY){
                if(d[v]>d[u]+M[u][v]){
                    d[v]=d[u]+M[u][v];
                    color[v]=GRAY;
                }
            }
        }
    }

    for(int i=0;i<n;i++){
        cout<<i<<' '<<(d[i]==INFTY ? -1 : d[i])<<endl;
    }
}

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            M[i][j]=INFTY;
        }
    }

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

    Dijkstra();

    return 0;
}

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

#define INFTY INT_MAX

int main()
{
    int n, m, i, j, a, w;
    int* adj[100];
    int* dist[100];
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        scanf("%d %d", &i, &m);
        adj[i] = new int [m + 1];   // adjacensy list.
        dist[i] = new int [m + 1];
        for(j = 0; j < m; j++){
            scanf("%d %d", &a, &m);
            adj[i][j] = a;
            dist[i][j] = w;
        }
        adj[i][m] = -1;
        dist[i][m] = -1;
    }

    int *d, *prev, *Q;
    d = new int [n];
    prev = new int [n];
    Q = new int [n];
    for(i = 0; i < n; i++){ d[i] = 200000; prev[i] = -1; Q[i] = i; }
    int rest = n;

    int u, v, min;
    d[0] = 0;  // 0 is start.
    while(rest > 0){
        min = 200000;
        for(i = 0; i < n; i++){
            if(min > d[i] && Q[i] >= 0){ min = d[i]; u = i; }
        }
        Q[u] = -1;
        rest--;
        for(j = 0; adj[u][j] >= 0; j++){
            v = adj[u][j];
            if(d[v] > d[u] + dist[u][j]){
                d[v] = d[u] + dist[u][j];
                prev[v] = u;
            }
        }
    };
    for(i = 0; i < n; i++){ printf("%d %d\n", i, d[i]); }
    for(i = 0; i < n; i++){
        delete adj[i]; delete dist[i]; }
    delete d;
    delete prev;
    delete Q;

    return 0;
}