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++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever while (true)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
using namespace std;
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int &s = 0, const int &d = 0, const Weight &w = 0) : src(s), dst(d), weight(w) {}
};

using Edges = std::vector<Edge>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

class Graph {
  std::vector<Edges> g;

public:
  Graph(const int &size = 0) : g(size) {}

  void addArc(const int &src, const int &dst, const Weight &w = 1) { g[src].emplace_back(src, dst, w); }

  void addEdge(const int &node1, const int &node2, const Weight &w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }

  const Edges &operator[](const int &i) const { return g[i]; }

  Graph toRootedTree(int r);
  std::pair<std::vector<Weight>, std::vector<int>> dijkstra(const int &src, const int &inf);
  Matrix warshallFloyd(const int &inf);
  std::pair<Weight, Edges> prim(const int &r);
  template<typename UnionFind> std::pair<Weight, Edges> kruskal();
  Weight solveTSP(const int &inf);
};

std::pair<std::vector<Weight>, std::vector<int>> Graph::dijkstra(const int &src, const int &inf = std::numeric_limits<Weight>::max() / 8) {
  using state = std::pair<Weight, int>;
  std::priority_queue<state, std::vector<state>, std::greater<state>> q;
  std::vector<Weight> dist(g.size(), inf);
  std::vector<int> via(g.size());
  dist[src] = 0;
  q.emplace(0, src);
  while (q.size()) {
    Weight d;
    int v;
    std::tie(d, v) = q.top();
    q.pop();
    if (dist[v] < d) continue;
    for (auto &e : g[v]) {
      if (cmin(dist[e.dst], dist[v] + e.weight)) {
        via[e.dst] = v;
        q.emplace(dist[e.dst], e.dst);
      }
    }
  }
  return {dist, via};
}

main {
  int n;
  cin >> n;
  Graph g(n);
  rep(_, n) {
    int u, k;
    cin >> u >> k;
    while (k--) {
      int v, c;
      cin >> v >> c;
      g.addArc(u, v, c);
    }
  }
  vi d = g.dijkstra(0).first;
  rep(i, n) cout << i << ' ' << d[i] << endl;
}

C++: #include<bits/stdc++.h>

using namespace std;
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007
#define INF (1<<28)
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define floot10 cout<<fixed<<setprecision(10);
#define int long long


signed main(){
	int n;
	cin>>n;
	vector<pair<int,int> > v[10000];
	lp(i,n){
		int u,k;
		cin>>u>>k;
		lp(j,k){
			int a,b;
			cin>>a>>b;
			v[u].push_back(make_pair(a,b));
		}
	}
	queue<pair<int,int> > q;
	q.push(make_pair(0,0));
	int memo[10000];
	lp(i,10000) memo[i]=INF;
	memo[0]=0;
	while(!q.empty()){
		pair<int,int> hoge=q.front();
		q.pop();
		int now=hoge.first;
		int cos=hoge.second;
		if(memo[now]<cos) continue;
		lp(i,v[now].size()){
			int next=v[now][i].first;
			int cost=v[now][i].second;
			if(memo[next]>cos+cost){
				 q.push(make_pair(next,cos+cost));
				 memo[next]=cos+cost;
			}
		}
	}
	lp(i,n){
		cout<<i<<" "<<memo[i]<<endl;
	}
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<algorithm>
#include<functional>
#define ll long long
#define inf  999999999
#define pa pair<int,int>

using namespace std;

int n;
vector<pa> a[10200];
void dij(){
	int d[10200]={0};
	int ans[10200]={0};
	priority_queue<pa>  PQ; 
	pair<int,pa> q;
	pa r;
	int wei=0;
	int u=0,z=0;
	d[0]=1;
	while(z!=n-1){
		for(int j=0;j<a[u].size();j++){
			r=make_pair(ans[u]+a[u][j].second,a[u][j].first);
			if(d[a[u][j].first]==0 ) PQ.push(r);
		}
	while(1){
		if(PQ.size()==0) {
			cout<<"dv"<<endl;
		break;
		}
		r=PQ.top();
		PQ.pop();
		
		if(d[r.second]==0) break;
	
		}
	u=r.second;
	d[u]=1;
	ans[u]=r.first;
	
	z++;
	}
//	cout<<-wei<<endl;
	for(int i=0;i<n;i++) cout<<i<<" "<<-ans[i]<<endl;
}



int main(){
	// your code goes here
	int m,k,z,l;
	cin>>n;
	for(int i=0;i<n;i++){
	cin>>z>>k;
	
		for(int j=0;j<k;j++){
		cin>>m>>l;
		a[i].push_back(make_pair(m,-l));
		}
	}
	dij();

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
inline ll read(){
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
const int maxn=1e4+5;
int n,k,u,v,c;
vector<pair<int,int> >adj[maxn];
void dijkstra(){
	priority_queue<pair<int,int> >PQ;
	int color[maxn],d[maxn];
	for(int i=0;i<n;i++){
		color[i]=0;
		d[i]=INF;
	}
	d[0]=0;
	PQ.push(make_pair(0,0));
	while(!PQ.empty()){
		pair<int,int> f=PQ.top();
		PQ.pop();
		int u=f.second;
		color[u]=1;
		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]==1) 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));
			}
		}
	}
	for(int i=0;i<n;i++){
		printf("%d ",i);
		if(d[i]==INF) printf("-1\n");
		else printf("%d\n",d[i]);	
	}
}
int main(){
	n=read();
	for(int i=0;i<n;i++){
		u=read(),k=read();
		for(int j=0;j<k;j++){
			v=read(),c=read();
			adj[u].push_back(make_pair(v,c));
		}
	}
	dijkstra();
	return 0;
}