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++: #include<bits/stdc++.h>
using namespace std;
using Weight=long long;

struct Edge{
    int from,to;
    Weight weight;
};
using Edges=vector<Edge>;
using Graph=vector<Edges>;

Weight INF=1e9+7;
vector<Weight> Dijkstra(int s,const Graph& g){
    int n=g.size();
    vector<Weight> res(n,INF);
    vector<int> used(n,0);
    res[s]=0;
    priority_queue<pair<Weight,int>,vector<pair<Weight,int>>,greater<>> que;
    que.push({Weight(0),s});
    while(!que.empty()){
        auto p=que.top(); que.pop();
        Weight dist=p.first;
        int v=p.second;
        if(used[v]) continue;
        used[v]=true;
        for(int i=0;i<g[v].size();i++){
            int tdist=dist+g[v][i].weight;
            if(tdist<res[g[v][i].to]){
                res[g[v][i].to]=tdist;
                que.push({tdist,g[v][i].to});
            }
        }
    }
    return res;
}

int main(){
    int n;
    cin>>n;
    Graph g(n);
    for(int i=0;i<n;i++){
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++){
            Edge e;
            e.from=u;
            cin>>e.to>>e.weight;
            g[u].push_back(e);
        }
    }
    auto d=Dijkstra(0,g);
    for(int i=0;i<n;i++){
        cout<<i<<" "<<d[i]<<endl;
    }

    return 0;
}

C++: #include<iostream>
int main(){
  int u,k,C,V,A[1000][1000],D[1000][1000],n;
  std::cin >> n;
  for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j] = 200001;
  for(int i=0;i<n;i++){ 
    std::cin >> u >> k;
    for(int j=0;j<k;j++){
      std::cin >> V >> C;
      D[u][V] = C;
    } 
  }
  for(int i=0;i<n;i++)D[i][i] = 0;
  for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j]= std::min(D[i][j],D[i][k] + D[k][j]);
  for(int i=0;i<n;i++)std::cout << i << " " << D[0][i] << std::endl;
  return 0;
}

C++: #include<map>
#include<vector>
#include<cstdio>
using namespace std;
int n,N,i,j,k,c,m;
int main(){
	scanf("%d",&N);
	vector<map<int,int>>dist(N);
	vector<bool>used(N);
	vector<int>cost(N);
	for(;n<N;n++){
		cost[n]=1e9;
		for(scanf("%d%d",&i,&k);k--;dist[i][j]=c)scanf("%d%d",&j,&c);
	}
	for(cost[0]=0;;){
		m=1e9;
		for(i=0;i<N;i++)if(!used[i]&&m>cost[i])m=cost[i];
		if(m==1e9)break;
		for(j=0;j<N;j++)if(m==cost[j]){
			map<int,int>::iterator it=dist[j].begin();
			for(used[j]=1;it!=dist[j].end();it++)if(cost[it->first]>it->second+cost[j])cost[it->first]=it->second+cost[j];
		}
	}
	for(i=0;i<n;i++)printf("%d %d\n",i,cost[i]);
}

C++: #include <bits/stdc++.h>

using namespace std;
using ll =long long;

static const int inf = (1<<21);
static const int white = 0;
static const int gray =  1;
static const int black = 2;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
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; }
//Write From this Line

int n;
vector<pair<int,int>> adj[10010]; // 重み付き有向グラフのリスト表現


void dijkstra(){
	priority_queue<pair<int, int> > p_que;
	int color[10010];
	int d[10010];
	rep(i, n){
		d[i] = inf;
		color[i] = white;
	}

	d[0] = 0;
	p_que.push(make_pair(0,0));
	color[0] = gray;

	while( !p_que.empty() ){
		pair<int, int> f = p_que.top(); p_que.pop();
		int u = f.second;
		color[u] = black;

		// 最小値を取り出し、それが最短でなければ無視
		if(d[u] < f.first * (-1)) continue;

		for (int j = 0; j < adj[u].size(); j++) {
			int v = adj[u][j].first;
			if ( color[v] == black ) continue;
			if (d[v] > d[u] + adj[u][j].second ){
				d[v] = d[u] + adj[u][j].second;
				p_que.push(make_pair(d[v] * (-1), v));
				color[v] = gray;
			}
		}
	}
	rep(i, n){
		cout << i << " " << (d[i] == inf ? -1 : d[i]) << endl;
	}
}

int main()
{
	cin >> n;
	rep(i,n){
		int u, k;
		cin >> u >> k;
		rep(j,k){
			int a, b;
			cin >> a >> b;
			adj[u].push_back(make_pair(a,b));
		}
	}
	dijkstra();
	return 0;
}