Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := makeGraph(n)
	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			c := getNextInt(scanner)
			g[u][v] = c
		}
	}

	ans := make([]int, n)
	im := make(IntMap, 1)
	im.increment(0)
	for len(im) < n {
		min := 1 << 30
		x := -1
		for u := range im {
			for v := 0; v < n; v++ {
				if im.get(v) != 0 {
					continue
				}
				if min > ans[u]+g[u][v] {
					min = ans[u] + g[u][v]
					x = v
				}
			}
		}
		im.increment(x)
		ans[x] = min
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i, ans[i]))
	}
	writer.Flush()
}

func makeGraph(n int) [][]int {
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = 1 << 30
		}
	}

	return g
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>

#define MAX 100
static const int INFTY =(1<<21);

int n,M[MAX][MAX];
int unvisited=0,decide=1,comp_decide=2;

void dijkstra(){
	int d[MAX];
	int state[MAX];
	for(int i=0;i<n;i++){
		d[i]=INFTY;
		state[i]=unvisited;
	}
	d[0]=0;
	state[0]=0;
	while(1){
		int mincost=INFTY,u=-1;
		for(int i=0;i<n;i++){
			if(d[i]<mincost&&state[i]!=comp_decide){
				u=i;
				mincost=d[i];
			}
		}
		if(u==-1) break;
		state[u]=comp_decide;
		for(int v=0;v<n;v++){
			if(state[v]!=comp_decide&&M[u][v]!=INFTY){
				if(d[v]>d[u]+M[u][v]){
					d[v]=d[u]+M[u][v];
					state[v]=decide;
				}
			}
		}
	}
	for(int i=0;i<n;i++){
			printf("%d %d\n",i,(d[i]==INFTY ? -1:d[i]));
	}
}

int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			M[i][j]=INFTY;
		}
	}
	int u,k,v,c;
	for(int i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		for(int j=0;j<k;j++){
			scanf("%d %d",&v,&c);
			M[u][v]=c;
		}
	}
	dijkstra();
}

C++: #include<iostream>
#define INF 1<<30
using namespace std;
int main() {
  int n; cin >> n;
  int graph[100][100];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) graph[i][j] = INF;
  for (int i = 0; i < n; ++i) {
    int u, k, v; cin >> u >> k;
    for (int j = 0; j < k; ++j) {
      cin >> v;
      cin >> graph[u][v];
    }
  }
  int mincost[100], flag[100] = {};
  mincost[0] = 0;
  for (int i = 1; i < n; ++i) mincost[i] = INF;
  while (1) {
    int x = -1;
    for (int i = 0; i < n; ++i)
      if (!flag[i] && (x == -1 || mincost[x] > mincost[i])) x = i;
    if (x == -1) break;
    flag[x] = 1;
    for (int i = 0; i < n; ++i)
      if (mincost[i] > mincost[x]+graph[x][i]) mincost[i] = mincost[x]+graph[x][i];
  }
  for (int i = 0; i < n; ++i) cout << i << " " << mincost[i] << endl;
  return 0;
}

C++: #include <iostream>
#include <string>
#include <unordered_map>
#include <stdio.h>
#include <list>
#include <vector>
#include <algorithm>
#include <stack>
#include <climits>
#include <queue>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; ++i)
typedef long long ll;

int M[100][100];

int main()
{
	int n;cin >> n;
	rep(i,n)
		rep(j,n)M[i][j]=INT_MAX;
		
	rep(i,n)
	{
		int hoge, hogege;
		cin >> hoge >> hogege;
		rep(j,hogege)
		{
			int foo,fooo;
			cin >> foo >> fooo;
			M[i][foo]=fooo;
		}
	}

	vector<int> color(n);
	rep(i,n)color[i]=0;
	vector<int> d(n);
	rep(i,n)d[i]=INT_MAX;
	vector<int> p(n);
	d[0]=0;
	p[0]=-1;

	while (1)
	{
		int u;
		int mincost = INT_MAX;
		rep(i,n)
		{
			if((color[i]!=2)&&(d[i]<mincost))
			{
				mincost=d[i];
				u=i;
			}
		}

		if(mincost==INT_MAX)break;

		color[u] = 2;

		rep(v,n)
		{
			if(color[v] != 2&&M[u][v]!=INT_MAX)
			{
				if(d[u] + M[u][v] < d[v])
				{
					d[v] = d[u] + M[u][v];
					p[v] = u;
					color[v] = 1;
				}
			}
		}
	}
	rep(i,n)
	{
		cout<<i<<" "<<d[i]<<endl;
	}


	

	return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long INF = 1LL << 60;    // 1.15x10^18
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using edge = pair<int, long long>;
using Graph = vector<vector<edge>>;

vector<long long> dijkstra(const Graph &g, int s) {
    int n = g.size();
    vector<long long> dist(n, INF);
    vector<bool> fixed(n);
    dist[s] = 0;
    while (true) {
        int u = -1;
        for (int i = 0; i < n; i++) if (!fixed[i]) {
            if (u == -1 || dist[i] < dist[u]) u = i;
        }
        if (u == -1) break;
        fixed[u] = true;
        for (auto &e: g[u]) chmin(dist[e.first], dist[u] + e.second);
    }
    return dist;
}

int main() {
    int V; cin >> V;
    Graph g(V);
    for (int i = 0; i < V; i++) {
        int u, k; cin >> u >> k;
        while (k--) {
            int v, c; cin >> v >> c;
            g[u].emplace_back(v, c);
        }
    }
    auto dist = dijkstra(g, 0);
    for (int u = 0; u < V; u++) cout << u << " " << dist[u] << endl;
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;

class UF{
public:
  vector<int> u;
  vector<int> r;

  void init(int n){
    u.resize(n, 0);
    r.resize(n, 0);
    for(int i=0; i<n; i++) u[i] = i;
  }
  int root(int x){
    // if( u[x] == x ) return x;
    // else return root(u[x]);
    return u[x] == x ? x : u[x] = root(u[x]);
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if( x == y ) return;

    if( r[x] < r[y] ){
      u[x] = y;
    }else{
      u[y] = x;
      if( r[x] == r[y] ) r[x]++;
    }
  }
};
class Graph{
public:
  int from, to, cost;
  bool operator<(const Graph &g1)const{
    return g1.cost < cost;
  };
};

int main(void) {

  int n;
  cin >> n;
  UF uf;
  uf.init(n);
  priority_queue<Graph> g;

  vector< vector<int> > d(n, vector<int> (n, inf));

  for(int i=0; i<n; i++){
    d[i][i] = 0;
    int u, k;
    cin >> u >> k;
    for(int j=0; j<k; j++){
      int c, v;
      cin >> v >> c;
      d[u][v] = min(c, d[u][v]);
      // Graph tg;
      // tg.to = v;
      // tg.from = u;
      // tg.cost = c;
      // g.push(tg);
    }
  }
  while( !g.empty() ){
    cerr << g.top().from << " " << g.top().to << " " << g.top().cost << endl;
    g.pop();
  }

  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
      for(int k=0; k<n; k++){
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
      }
    }
  }

  for(int i=0; i<n; i++){
    cout << i << " " << d[0][i] << endl;
  }

  return 0;
}

// EOF