Go: package main

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

type Node struct {
	Id int
	Dis int
}

type PriorityQueue []*Node

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

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].Dis < pq[j].Dis
}

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

func (pq *PriorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.(*Node))
}

func (pq *PriorityQueue) Pop() interface{} {
	n := len(*pq)
	pop := (*pq)[n-1]
	*pq = (*pq)[0:n-1]
	return pop
}

func main() {
	var n int
	fmt.Scan(&n)

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

	g := make([][]int, n)
	c := make([][]int, n)
	pq := make(PriorityQueue, 0, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		g[u] = make([]int, k)
		c[u] = make([]int, k)
		for j := 0; j < k; j++ {
			sc.Scan()
			g[u][j], _ = strconv.Atoi(sc.Text())
			sc.Scan()
			c[u][j], _ = strconv.Atoi(sc.Text())
		}

		if u == 0 {
			d[u] = 0
		} else {
			d[u] = 1 << 30
		}
		pq.Push(&Node{u, d[u]})
	}
	heap.Init(&pq)

	for pq.Len() > 0 {
		node := heap.Pop(&pq).(*Node)
		for i := 0; i < len(g[node.Id]); i++ {
			nextId := g[node.Id][i]
			nextDis := node.Dis + c[node.Id][i]
			if nextDis < d[nextId] {
				d[nextId] = nextDis
				heap.Push(&pq, &Node{nextId, nextDis})
			}
		}
	}

	wr := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		wr.WriteString(strconv.Itoa(i))
		wr.WriteString(" ")
		wr.WriteString(strconv.Itoa(d[i]))
		wr.WriteString("\n")
	}
	wr.Flush()
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef pair<ll,P>  PP;
typedef vector<ll> Vector;
typedef vector<Vector> DVector;
typedef priority_queue<P, vector<P>, greater<P>> PQueue;

#define fi          first
#define se          second
#define sfi         se.fi
#define sse         se.se
#define pb          push_back
#define INF         INT_MAX
#define MOD         1000000007
#define bcnt        __builtin_popcount
#define all(x)      (x).begin(),(x).end()
#define uni(x)      x.erase(unique(all(x)),x.end())
#define ub(x,y)     (upper_bound(all(x),y)-x.begin())
#define lb(x,y)     (lower_bound(all(x),y)-x.begin())
#define rep(i,n)    repl(i,0,n-1)
#define usort(x)    sort(all(x))
#define dsort(x)    sort(all(x),greater<ll>())
#define mkp(x,y)    make_pair(x,y)
#define mmax(x,y)   (x>y?x:y)
#define mmin(x,y)   (x<y?x:y)
#define maxch(x,y)  x=mmax(x,y)
#define minch(x,y)  x=mmin(x,y)
#define exist(x,y)  (find(all(x),y)!=x.end())
#define each(itr,v) for(auto itr:v)
#define repl(i,a,b) for(ll i=(ll)(a);i<=(ll)(b);i++)
DVector costs;
ll n;
Vector mincost;
void djk(ll start){
  PQueue pq;
  mincost.resize(n,INF);
  mincost[start] = 0;
  pq.push({0,start});
  while(pq.size()){
    P state = pq.top();pq.pop();
    if(mincost[state.se] < state.fi)continue;
    rep(i,n)if(mincost[i] > mincost[state.se] + costs[state.se][i]){
      mincost[i] = mincost[state.se] + costs[state.se][i];
      pq.push({mincost[i],i});
    }
  }
}

int main(){
  cin.sync_with_stdio(false);
  cin >> n;
  costs.resize(n,Vector(n,INF));
  rep(i,n){
    ll u,k;
    cin >> u >> k;
    rep(j,k){
      ll v,c;
      cin >> v >> c;
      costs[u][v] = c;
    }
  }
  djk(0);
  rep(i,n)cout << i << " " << mincost[i] << endl;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)

static const int MAX = 10000;
static const int INFTY = (1 << 20);

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

void dijkstra(int s) {
	priority_queue<pair<int, int>> PQ;
	int color[MAX];
	
	REP(i, n) {
		color[i] = 0;
	}

	dist[s] = 0;
	PQ.push(make_pair(0, s));
	color[s] = 1;

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

		//????°???????????????????????????????????????§???????????°??????
		if (dist[u] < f.first*(-1)) continue;

		REP(j, adj[u].size()) {
			int v = adj[u][j].first;
			if (color[v] == 2) continue;
			if (dist[v] > dist[u] + adj[u][j].second) {
				dist[v] = dist[u] + adj[u][j].second;
				PQ.push(make_pair(dist[v] * (-1), v));
				color[v] = 1;
			}
		}
	}

}

int main() {
	int k, u, v, c;
	cin >> n;
	REP(i, n) {
		cin >> u >> k;
		REP(j, k) {
			cin >> v >> c;
			adj[u].push_back(make_pair(v, c));
		}
	}

	REP(i, n) dist[i] = INFTY;

	dijkstra(0);

	REP(i, n) {
		cout << i << " " << (dist[i] == INFTY ? -1 : dist[i]) << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<vector>
using namespace std;

static const int MAX=1000;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

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

void dijkstra(){
  priority_queue<pair<int,int> > PQ;
  int color[MAX];
  int d[MAX];

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

  d[0]=0;
  PQ.push(make_pair(0,0));
  color[0]=GRAY;

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

    color[u]=BLACK;

    for(int i=0;i<adj[u].size();i++){
      int v=adj[u][i].first;
      if(color[v]==BLACK) continue;
      if(d[u]+adj[u][i].second<d[v]){
	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]<<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;
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct Edge {
    int d, i, j;
    Edge(int d_, int i_, int j_) : d(d_), i(i_), j(j_) {}
    bool operator< (const Edge& right) const{
        return d > right.d;
    }
};
void calc(int t, vector<vector<pair<int, int > > >& G, vector<int>& distance){
    int new_black = t;
    priority_queue<Edge> temp;
    for (int i = 1; i < G.size(); i++) {
        for (int j = 0; j < G[new_black].size(); j++) {
            if (distance[G[new_black][j].first] != -1) continue;
            int dis = G[new_black][j].second + distance[new_black];
            temp.push(Edge(dis, new_black, G[new_black][j].first));
        }
        while (distance[temp.top().j] != -1) temp.pop();
        distance[temp.top().j] = temp.top().d;
        new_black = temp.top().j;
        temp.pop();
    }
}

int main(){
    int n;
    cin >> n;
    vector<vector<pair<int, int> > > G(n);
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        G[u].resize(k);
        for (int j = 0; j < k; j++) {
            int v, c;
            cin >> v >> c;
            G[u][j] = make_pair(v, c);
        }
    }

    vector<int> distance(n, -1);
    distance[0] = 0;
    calc(0, G, distance);
    for (int i = 0; i < n; i++) {
        cout << i << " " << distance[i] << endl;
    }
    return 0;
}