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>
#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