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;
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;
}

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, k, n) for(auto i = k; i != n; i++)
#define rrep(i, n) RREP(i, n, 0)
#define RREP(i, n, k) for(auto i = n; i != k; i--)
#define ALL(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " " << x << endl
#define in(x, data) (data).find(x) != (data).end()

typedef long long ll;
typedef pair<int, int> PRii;

class Node {
public:
    int dist, number;
    Node(int d, int n) {
        dist = d;
        number = n;
    }
};

bool operator < (const Node &l, const Node &r) {
    return l.dist < r.dist
    || (!(r.dist < l.dist) && l.number < r.number);
};

bool operator > (const Node &l, const Node &r) {
    return r < l;
};

class Edge {
public:
    int to, cost;
    Edge(int t, int c) {
        to = t;
        cost = c;
    }
};

int node_n;
vector<vector<Edge>> edge;
vector<int> d;

void cost_initializer(int n) {
    edge = vector<vector<Edge>>(n, vector<Edge>());
}

void dijkstra(int start_p) {
    d = vector<int>(node_n, INT_MAX); d[start_p] = 0;
    vector<int> prev = vector<int>(node_n);

    priority_queue<Node,vector<Node>,greater<Node>> Q;

    for(int i=0; i<node_n; i++) {
        Q.push(Node(d[i],i));
    }

    while(!Q.empty()) {
        Node u = Q.top(); Q.pop();
        if(u.dist > d[u.number]) continue;
        for(int i=0; i<edge[u.number].size(); i++) {
            int alt = d[u.number] + edge[u.number][i].cost;
            int v = edge[u.number][i].to;
            if(d[v] > alt) {
                d[v] = alt;
                prev[v] = u.number;
                Q.push(Node(d[v],v));
            }
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n; cin >> n;
    node_n = n;
    cost_initializer(node_n);

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

    dijkstra(0);
    rep(i,n) cout << i << " " << d[i] << endl;
    return 0;
}