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++: // ConanYu's https://vjudge.net/solution/13220749
// semon0x29a's https://vjudge.net/solution/13218148

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using pi = pair<int, int>;

const int INF = 0x3F3F3F3F;
const int maxN = 10010;

int n;
bool mark[maxN];
LL dis[maxN];
map<pi, LL> path;

void init() {
	memset(dis, 0x3F, sizeof(dis));
	for (int i=0; i<n; ++i) path[{i, i}] = 0;
}

void dijkstra() {
	dis[0] = 0;
	for (int i=0; i<n; ++i) {
		int marker = 0, amin = INF;
		for (int i=0; i<n; ++i) if (!mark[i]) {
			if (dis[i] < amin) {
				amin = dis[i];
				marker = i;
			}
		}
		mark[marker] = true;
		for (int i=0; i<n; ++i) if (!mark[i]) {
			if (path.count({marker, i}))
			if (dis[i] > dis[marker] + path[{marker, i}])	
				dis[i] = dis[marker] + path[{marker, i}];
		}
	}
}

int main()
{	
	scanf("%d", &n);
	init();
	
	for (int i=0; i<n; ++i) 
	{
		int u, k; scanf("%d%d", &u, &k);
		while (k--) {
			int v; LL c; scanf("%d%lld", &v, &c);
			if (path.count({u, v}))
				path[{u, v}] = min(path[{u, v}], c);
			else path[{u, v}] = c;
		}
	}
	
	dijkstra();
	for (int i=0; i<n; ++i) {
		printf("%d %lld\n", i, dis[i]);
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template<class T> ostream &operator << (ostream &os, const vector<T> &);
template<size_t n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, const tuple<T...> &){}
template<size_t n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream &os, const tuple<T...> &t){
    os << (n == 0 ? "" : " ") << get<n>(t); _ot<n+1>(os, t);
}
template<class...T> ostream &operator << (ostream &os, const tuple<T...> &t){
    _ot<0>(os, t); return os;
}
template<class T, class U> ostream & operator << (ostream &os, const pair<T,U> &p){
    return os << "(" << p.first << ", " << p.second << ") ";
}
template<class T> ostream &operator << (ostream &os, const vector<T> &v){
    for(size_t i = 0; i < v.size(); i++) os << v[i] << (i + 1 == v.size() ? "" : ", "); return os;
}
#ifdef DEBUG
#define dump(...) (cerr << #__VA_ARGS__ << " = " << make_tuple(__VA_ARGS__) \
                   << " (L : " << __LINE__ << ")" << endl)
#else
#define dump(...)
#endif
#define all(c) begin(c), end(c)
#define range(i,a,b) for(int i = a; i < (int)(b); i++)
#define rep(i,b) range(i,0,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
using ll = long long;
// #define int ll
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int,int>;
void fastios(){ ios_base::sync_with_stdio(0); cin.tie(0); }
int const mod = 1000000007;
auto const inf = numeric_limits<int>::max()/8;

using Weight = int;
using Capacity = int;
struct Edge {
    int src, dst; Weight weight;
    Edge(int s, int d, int w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

pair<vector<Weight>, bool> spfa(const Graph &g, int s){
    const Weight inf = numeric_limits<Weight>::max()/4;
    const int n = g.size(); queue<int> q;
    vector<Weight> dist(n, inf); vector<int> inQ(n);
    vector<int> count(n);
    dist[s] = 0; q.emplace(s); inQ[s] = true; ++count[s];
    bool negCycle = false;
    while(q.size()){
        int v = q.front(); q.pop(); inQ[v] = false;
        for(auto &e : g[v]){
            if(dist[v] != inf && dist[e.dst] > dist[v] + e.weight){
                dist[e.dst] = dist[v] + e.weight;
                if(!inQ[e.dst]){
                    q.emplace(e.dst); inQ[e.dst] = true; ++count[e.dst];
                    if(count[e.dst] >= n){ negCycle = true; goto END; }
                }
            }
        }
    }
 END:;
    return make_pair(dist, !negCycle);
}

int main(){
    fastios();
    int n;
    cin >> n;
    Graph g(n);
    rep(i,n){
        int u,k;
        cin >> u >> k;
        rep(j,k){
            int v,c;
            cin >> v >> c;
            g[u].eb(u,v,c);
        }
    }
    auto dist = spfa(g,0);
    rep(i,n){
        cout << i << ' ' << get<0>(dist)[i] << '\n';
    }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
static const int MAX = 10000;
static const int INFTY = (1<<20);

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

void dij(){
  priority_queue<pair<int,int> > PQ;
  int color[MAX], d[MAX], i, j, u, v;
  for(i = 0; i < n; i++){
    d[i] = INFTY;
    color[i] = 0;
  }
  d[0] = 0;
  PQ.push(make_pair(0,0));
  color[0] = 1;

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

    if(d[u] < f.first * (-1)) continue;
    for(j = 0; j < G[u].size(); j++){
      v = G[u][j].first;
      if(color[v] == 2) continue;
      if(d[v] > d[u] + G[u][j].second){
        d[v] =  d[u] + G[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = 1;
      }
    }
  }

  for(i = 0; i < n; i++){
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
  }
}
int main()
{
  int k, u, v, c, i, j;

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

  dij();
  return 0;
}