Go: package main

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

var sc = bufio.NewScanner(os.Stdin)

const T = 10000000

var n int
var A [101][101]int

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func Shortestsearch() {
	var Minimum int
	var d, color [100]int
	var u int

	for i := 0; i < n; i++ {
		d[i] = T
		color[i] = 0
	}

	d[0] = 0
	color[0] = 1

	for {
		Minimum = T
		u = -1
		for i := 0; i < n; i++ {
			if Minimum > d[i] && color[i] != 2 {
				u = i
				Minimum = d[i]
			}
		}
		if u == -1 {
			break
		}
		color[u] = 2
		for j := 0; j < n; j++ {
			if color[j] != 2 && A[u][j] != T {
				if d[j] > d[u]+A[u][j] {
					d[j] = d[u] + A[u][j]
					color[j] = 1
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}
func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			A[i][j] = T
		}
	}
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			v := nextInt()
			c := nextInt()
			A[u][v] = c
		}
	}
	Shortestsearch()
}

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