Go: package main

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

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	route := make([][]Node, n)
	weight := make([]int, n)
	for i := 0; i < n; i++ {
		weight[i] = NIL
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		route[i] = make([]Node, k)
		for j := 0; j < k; j++ {
			route[u][j] = Node{id: scanInt(), weight: scanInt()}
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for _, nextNode := range route[node.id] {
			if weight[nextNode.id] != NIL {
				continue
			}
			nextNode.weight += node.weight
			heap.Push(h, nextNode)
		}
	}

	for i, w := range weight {
		fmt.Println(i, w)
	}
}

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