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<iostream>
#include<algorithm>
#include<queue>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
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 d[MAX], color[MAX];
	
	for(int i = 0; i < n; i++) {
		d[i] = INFTY;
		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 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;
				PQ.push(make_pair(d[v] * (-1), v));
				color[v] = GRAY;
			}
		}
    }
    
    for(int i = 0; i < n; i++) {
    	cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
    }
}

int main() {
	int k, c, u, v;
	
	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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100000
#define EMAX 500000
#define INFTY 2000000000
typedef struct {
  int pre,next,x;
}Cost;
int n,d[MAX];
Cost E[EMAX];
void jr(int);

void jr(int cou){
  int i,tmp;
  
  for(i=0;i<n;i++){
    d[i]=INFTY;
  }
  d[0]=0;

  tmp=1;
  
  while(tmp!=0){
    tmp = 0;
    for(i = 0;i < cou; i++){
      if(E[i].x + d[E[i].pre] < d[E[i].next]){
	d[E[i].next] = E[i].x + d[E[i].pre];
	tmp = 1;
      }
    }
  }
}

int main()
{
  int i,j,u,k,cou=0;

  scanf("%d",&n);

  for(i=0;i<n;i++){
    scanf("%d%d",&u,&k);
    for(j=0;j<k;j++){
      E[cou].pre = u;
      scanf("%d%d", &E[cou].next, &E[cou].x);
      cou++;
    }
  }

  jr(cou);

  for(i=0;i<n;i++){
    printf("%d %d\n",i,d[i]);
  }

  return 0;
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define str string
#define ld long double
#define vec vector
#define vll vec<ll>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
#define ALL(a) a.begin(),a.end()
#define rALL(a) a.rbegin(),a.rend()
#define kai "\n"
#define yuque priority_queue
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mie min_element
#define mae max_element
#define tos to_string
#define sep setprecision
#define lob lower_bound
#define upb upper_bound
#define nep next_permutation
#define MOD 1000000007ll
#define MIN (1e-10)
#define equal(a,b) (abs((a)-(b))<MIN)
#define v2 ten
#define chokusen senbun
#define kakkei vec<ten>
using namespace std;
void solve();
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed;
	solve();
}
struct pea{
	ll f,s;
	bool operator<(const pea &p)const{
		return f!=p.f?f<p.f:s<p.s;
	};
};
struct tri{
	ll f,s,t;
	bool operator<(const tri &p)const{return f<p.f;};
};
class ten{
public:
	ld x,y;
	ten operator+(ten p){return{x+p.x,y+p.y};}
	ten operator-(ten p){return{x-p.x,y-p.y};}
	ten operator*(ld k){return{k*x,k*y};}
	ten operator/(ld k){return{x/k,y/k};}
	ld abs(){return sqrt(abs2());}
	ld abs2(){return x*x+y*y;};
	bool operator<(const ten &p)const{return x!=p.x?x<p.x:y<p.y;};
	bool operator==(const ten &p)const{
		return (-MIN<x-p.x)&&(x-p.x<MIN)&&(-MIN<y-p.y)&&(y-p.y<MIN);
	};
	ld naiseki(v2 b){return x*b.x+y*b.y;}
	ld gaiseki(v2 b){return x*b.y-y*b.x;}
};
struct senbun{ten p1,p2;};
struct en{ten o;ld r;};
void dijkstra(const vec<vec<pea>> &c,ll v,vll &d){
	d[v]=0;
	ll n=c.size();
	ll count=1;
	vec<bool> seen(n,false);
	seen[v]=true;
	yuque<pea> hen;
	rep(i,0,c[v].size())hen.push({-c[v][i].f,c[v][i].s});
	while(count!=n){
		pea k=hen.top();hen.pop();
		while(seen[k.s]){
			k=hen.top();
			hen.pop();
		}
		d[k.s]=-k.f;
		count++;
		seen[k.s]=true;
		rep(i,0,c[k.s].size())hen.push({k.f-c[k.s][i].f,c[k.s][i].s});
	}
}
void solve(){
	ll n;cin>>n;
	vec<vec<pea>> c(n);
	rep(i,0,n){
		ll u,k;
		cin>>u>>k;
		rep(j,0,k){
			ll v,w;
			cin>>v>>w;
			c[u].pub({w,v});
		}
	}
	vll ans(n);
	dijkstra(c,0,ans);
	rep(i,0,n)cout<<i<<' '<<ans[i]<<kai;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

constexpr int LEN = 10005;
vector<P> a[LEN];

void dikstra(const int n){
	int d[n];
	bool check[n] = {0};
	memset(d, 1, sizeof(d));
	priority_queue<P, vector<P>, greater<P> > h;

	d[0] = 0;
	h.push(make_pair(d[0], 0));
	check[0] = 1;

	while(h.size()){
		P f = h.top(); h.pop();
		int u = f.second;
		int uc = f.first;
		check[u] = 1;
		for(P vf : a[u]){
			int v = vf.second;
			int vc = vf.first;
			if(!check[v]){
				if((uc + vc) < d[v]){
					d[v] = uc + vc;
					h.push(make_pair(d[v], v));
				}
			}
		}
	}

	rep(i, n) cout << i << ' ' << d[i] << endl;
	return;
}

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

	dikstra(n);
	return 0;
}