Go: package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := make(map[int]int)
	q[0] = 0
	visited := make(map[int]struct{})
	out := make([]int, n)

	for len(q) > 0 {
		u := top(q)
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			if d, ok := q[uu]; !ok || d > q[u]+c {
				q[uu] = q[u] + c
			}
		}

		out[u] = q[u]
		delete(q, u)
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, d)
	}
}

func top(q map[int]int) int {
	minu := -1
	min := -1
	for u, d := range q {
		if min < 0 || d < min {
			minu = u
			min = d
		}
	}

	return minu
}

C++: #include<iostream>
#include<cstdio>
#define INF 200001 
using namespace std;
int n;
int A[1000][1000];
int D[1000][1000];
  
int main(){
  scanf("%d",&n);
  int u,k,C,V;
   for(int i=0;i<n;i++)for(int j=0;j<n;j++)D[i][j] = INF;
   
  for(int i=0;i<n;i++){
      scanf("%d %d",&u,&k);    
      for(int j=0;j<k;j++){
    scanf("%d %d",&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]= min(D[i][j],D[i][k] + D[k][j]);
 
  for(int i=0;i<n;i++)printf("%d %d\n",i,D[0][i]);
  
  
  return 0;
}

C++: //compiledef
#include <bits/stdc++.h>
#define rep(i, n) for(i=0;i<(n);i++)
#define per(i, n) for(i=(n);i>0;i--)
#define repx(i, n, x) for(i=(x);i<(n);i++)
#define xper(i, n, x) for(i=(n);i>(x);i--)
#define pback push_back

//c++def
using namespace std;
typedef long long int ll;
const ll MOD = 1e9+7;
const ll MOD2 = 998244353;
int n;
int w[10003][10003];
void daikustra(){
	int color[10003];
	int d[10003];
	int i;
	int v;
	int u= -1;
	rep(i, n){
		d[i] = 2e9;
		color[i] = 0;
	}
	d[0] = 0;
	color[0] = 1;
	while(999){
		int mincost = 2e9;
		rep(i, n){
			if(mincost>d[i] && color[i] !=2){
				u = i;
				mincost =d[i];
			}
		}
		if(mincost==2e9) break;
		color[u] = 2;
		for(v=0;v<n;v++){
			if(color[v] !=2&&w[u][v] != 2e9){
				if((d[u] + w[u][v])<d[v]){
					d[v] = d[u] + w[u][v];
					color[v] =1;
				}
			}
		}
	}
	rep(i, n){
		cout << i << " ";
		if(d[i] == 2e9)
			cout << -1;
		else
			cout<< d[i];
		cout <<endl;
	}
}

int main(){
	int i, j, k, l, u, v;
	cin >> n;
	rep(i, n)rep(j, n)w[i][j] = 2e9;
	rep(i, n){
		cin >> k >> l;
		rep(j, l){
			cin >> u >> v;
			w[k][u] = v;
		}
	}
	daikustra();
}

C++: #include<bits/stdc++.h>
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 color[MAX];
  int d[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,u,v,c;
  
  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 <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
#define MAX 10000000

int b[110];

int solve(int a[110][110], int n){
	b[0] = 0;
	for(int i = 0;i < n;i++){
		for(int j = 1;j < n;j++){
			//if(j == 4)cout << b[4] << i << " " << endl;
			if(a[i][j] != -1)b[j] = min(b[j], a[i][j]+b[i]);
			//if(j == 4)cout << b[4] << i << " " << endl;
		}
	}
	//cout << endl;
	return 0;
}

int main(){
	int a[110][110], n;
	fill((int*)a, (int*)(a+110), -1);
	fill(b, b+110, MAX);
	cin >> n;
	for(int i = 0;i < n;i++){
		int u, k, c, d;
		cin >> u >> k;
		for(int j = 0;j < k;j++){
			cin >> c >> d;
			a[u][c] = a[c][u] = d;
		}
	}
	for(int i = 0;i < 2*n;i++)solve(a, n);
	for(int i = 0;i < n;i++){
		cout << i << " " << b[i] << endl;
	}
	
	return 0;
}
/*
5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3
*/

C++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define USE_LLONG_AS_INT
#ifdef USE_LLONG_AS_INT
#define int long long
#define inf (1ll<<60)
#else
#define inf (1<<30)
#endif
#define rep(i,n) for(int i=0;i<(n);i++)
#define Rep(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a,b) for(int i=(a);i<=(b);i++)
#define rev(i,n) for(int i=(n)-1;i>=0;i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define eb emplace_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod=1e9+7;
const string sp=" ";

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

int n;
vector<vp> g;

void solve(){
    vi d(n,inf);
    priority_queue<pi,vp,greater<pi>> q;
    d[0]=0;
    q.push({0,0});
    while(!q.empty()){
        pi p=q.top();
        q.pop();
        int v=p.se;
        if(d[v]<p.fi)continue;
        for(pi e:g[v]){
            if(d[e.se]>d[v]+e.fi){
                d[e.se]=d[v]+e.fi;
                q.push({d[e.se],e.se});
            }
        }
    }
    rep(i,n){
        cout<<i<<sp<<d[i]<<endl;
    }
}

void run(){
    cin>>n;
    g.resize(n);
    rep(i,n){
        int u,k;
        cin>>u>>k;
        rep(j,k){
            int v,c;
            cin>>v>>c;
            g[u].eb(pi(c,v));
        }
    }
    solve();
}

C++: #include<iostream>
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, N[MAX][MAX];

void dijkstra(){
  int minv;
  int d[MAX],color[MAX];
  int i=0;

  for(i=0;i<n;i++)
    {
      d[i]=INFTY;
      color[i]=WHITE;
    }

  d[0] = 0;
  color[0]=GRAY;
  while(1)
    {
      minv = INFTY;
      int u =-1;
      int v=0;
      
      for(int i = 0; i<n ; i++)
	{
	  if(minv>d[i]&&color[i]!=BLACK)
	    {
	      u=i;
	      minv = d[i];
	    }
	}
      if(u==-1)
	{
	  break;
	}
      color[u]=BLACK;
      for(v=0;v<n;v++)
	{
	  if(color[v]!=BLACK && N[u][v]!=INFTY)
	    {
	    if(d[v]>d[u] +N[u][v])
	      {
		d[v]=d[u]+N[u][v];
		color[v]= GRAY;
	      }
	    }
	}
    }
  for(i=0;i<n;i++)
    {
      cout << i << " "<<(d[i] == INFTY ? -1 : d[i])<<endl;
    }
}

int main()
{
  cin>> n;
  int i=0;
  int j=0;
  for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
	{
	  N[i][j]=INFTY;
	}
    }

  int k,c,u,v;
  for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
	{
	  N[i][j] =INFTY;
	}
    }

  for(i=0;i<n;i++)
    {
      cin>>u>>k;
      for(j=0;j<k;j++)
	{
	  cin>>v>>c;
	  N[u][v]=c;
	}
    }

  dijkstra();

  return 0;
}