Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

//make priority queue
type Item struct {
	id       int
	priority int // this state is priority and distans from start
	index    int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

// we want the ascending-priority-queue.
// So we use less than here.
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = j
	pq[j].index = i
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(dist int, id int) {
	for _, i := range *pq {
		if i.id == id {
			if i.priority > dist {
				i.priority = dist
			}
		}
	}
}

// the main code
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	return n
}

func dikstra(dist [][]int, n int) []int {
	// preparate priority queue
	inf := 1 << 30
	pq := make(PriorityQueue, n)
	shortests := make([]int, n)
	for i := 0; i < n; i++ {
		pq[i] = &Item{
			id:       i,
			priority: inf,
			index:    i,
		}

		shortests[i] = inf
	}
	pq[0].priority = 0
	shortests[0] = 0

	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		shortests[item.id] = item.priority
		for id, d := range dist[item.id] {
			if d != -1 {
				pq.update(item.priority+d, id)
			}
		}
		heap.Init(&pq)
	}
	return shortests
}

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

	// init
	n := nextInt(sc)
	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dist[i][j] = -1
		}
	}

	// std Input
	for i := 0; i < n; i++ {
		u := nextInt(sc)
		k := nextInt(sc)
		for j := 0; j < k; j++ {
			v, c := nextInt(sc), nextInt(sc)
			dist[u][v] = c
		}
	}

	shortests := dikstra(dist, n)
	for i, d := range shortests {
		fmt.Printf("%d %d\n", i, d)
	}
}

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

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }


void Dijkstra(int s, const vector<vector<P>> &g, vector<int> &dis) {
  priority_queue<P, vector<P>, greater<P>> que; // pair = (cost, v);
  dis[s] = 0;
  que.emplace(0,s);
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int now = p.second;
    if(dis[now] < p.first) continue;
    for(P np : g[now]) {
      int next = np.second;
      if(dis[next] > dis[now] + np.first) {
        dis[next] = dis[now] + np.first;
        que.emplace(dis[next], next);
      }
    }
  }
}


int main(){
    
    int n;
    cin >> n;
    V<V<P>> g(n); // pair = (cost, to);
    rep(i,n) {
        int a, k;
        cin >> a >> k;
        rep(j,k) {
            int b, v;
            cin >> b >> v;
            g[a].emplace_back(v,b);
        }
    }

    V<int> d(n, inf);
    Dijkstra(0,g,d);
    rep(i,n) {
        printf("%d %d\n", i, d[i]);
    }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <climits>
#include <queue>

#define INF INT_MAX

using namespace std;

int main(){
  int n; cin >> n;
  vector<pair<int,int> > G[n];
  for(int i=0;i<n;i++){
    int u,k; cin >> u >> k;
    for(int j=0;j<k;j++){
      int v,c; cin >> v >> c;
      G[u].push_back(make_pair(v,c));
    }
  }
  //dijkstra from 0
  priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > PQ;
  int visited[n],d[n];
  for(int i=0;i<n;i++){
    visited[i]=0;
    d[i]=INF;
  }
  d[0]=0;
  PQ.push(make_pair(d[0],0));
  while(PQ.empty()==false){
    int u=PQ.top().second;
    int u_first=PQ.top().first;
    PQ.pop();
    //cout << "u:" << u << endl;
    visited[u]=1;
    if(d[u]<u_first) continue;
    for(int i=0;i<G[u].size();i++){
      int v=G[u][i].first;
      //cout << "v:" << v << endl;
      if(visited[v]==0){
        if(d[u]+G[u][i].second<d[v]){
          d[v]=d[u]+G[u][i].second;
          PQ.push(make_pair(d[v],v));
        }
      }
    }
  }
  for(int i=0;i<n;i++) cout << i << " " << d[i] << endl;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define MAX 10000
#define INF (1 << 21)
#define WHITE  1
#define GRAY  2
#define BLACK  3
using namespace std;

int n;
vector<pair<int, int> > adj[MAX];

void dijkstra(){
  priority_queue<pair<int, int> > pq;

  vector<int> color(MAX);
  vector<int> d(MAX);
  
  for(int i = 0; i < n; i++){
    d[i] = INF;
    color[i] = WHITE;
  }

  d[0] = 0;

  pq.push(make_pair(0, 0));

  color[0] = GRAY;

  while(!pq.empty()){
    pair<int, int> f = pq.top();
    pq.pop();

    int u = f.second;

    color[u] = BLACK;

    if(d[u] < f.first * (-1)) continue;

    for(int i = 0; i < adj[u].size(); i++){
      int v = adj[u][i].first;

      if(color[v] == BLACK) continue;
      if(d[v] > d[u] + adj[u][i].second){
	d[v] = d[u] + adj[u][i].second;
	pq.push(make_pair(d[v] * (-1), v));
	color[v] = GRAY;
      }
    }
  }

  for(int i = 0; i < n; i++){
    cout << i << " " << (d[i] == INF ? -1: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;
      adj[u].push_back(make_pair(v, c));
    }
  }

  dijkstra();

  return 0;

}