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<stdio.h>
#include<stdlib.h>

#define rep( i ,end ) for( int i = 0 ; i < end ; ++i )
#define Nmax 100
#define INF ( INT_MAX / 10 )

int Node[ Nmax ][ Nmax ] ;

int min( int a ,int b )
{
	return a > b ? b : a ;
}

void Init( int ( *NODE )[ Nmax ] ,int N )
{
	rep( i ,N )
	{
		rep( j ,N )
		{
			NODE[ i ][ j ] = ( i == j ) ? 0 : INF ;
		}
	}
}



int main( void )
{
	int N ,V ,K ;
	int v ,cost ;
	scanf( "%d" ,&N ) ;

	Init( Node ,N ) ;

	rep( i ,N )
	{
		scanf( "%d %d" ,&V ,&K ) ;

		rep( j ,K )
		{
			scanf( "%d %d" ,&v ,&cost ) ;

			Node[ V ][ v ] = cost ;
		}
	}

	rep( u ,N )
	{
		rep( i ,N )
		{
			rep( j ,N )
			{
				Node[ i ][ j ] = min( Node[ i ][ j ] ,Node[ i ][ u ] + Node[ u ][ j ] ) ;
			}
		}
	}

	rep( u ,N )
	{
		printf( "%d %d\n" ,u ,Node[ 0 ][ u ] ) ;
	}

	return 0 ;
}

C++: #include <iostream>
using namespace std;
#define MAX 101
#define INFTY (1<<21)
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];
void dijkstra()
{
    int minv;
    int d[MAX], color[MAX], p[MAX];
    for(int i=0; i<MAX; i++)
    {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    d[0] = 0;
    color[0] = GRAY;
    while(1)
    {
        minv = INFTY;
        int u = -1;
        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(int v=0; v<n; v++)
        {
            if(color[v] != BLACK && M[u][v] != -1)
            {
                if(d[u]+M[u][v] < d[v])
                {
                    d[v] = d[u] + M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    for(int i=0; i<n; i++)
    {
        cout << i << " " << d[i] << endl;
    }
}
int main()
{
    cin >> n;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
            M[i][j] = INFTY;
    }
    int k, c, u, v;
    for(int i=0; i<n; i++)
    {
        cin >> u >> k;
        for(int j=0; j<k; j++)
        {
            cin >> v >> c;
            M[u][v] = c;
        }
    }
    dijkstra();
    return 0;
}

C++: #include <stdio.h>
#define INF (1<<21)

int main(void)
{
 int i,j,n,m,u,k,l,v,c,a[10000],b[10000],d[10000][10000];

 scand("%d",&n);
 
 for(i=0;i<n;i++){
 	scanf("%d %d",&u,&k);
 	a[i]=INF;
 	b[i]=0;
 	for(j=0;j<k;j++){
 		scanf("%d %d",&v,&c);
 		d[u][v]=c;
 	}
 }
 a[0]=0;
 
 while(1){
 	
 	l=-1;
 	m=INF;
 	
 	for(i=0;i<n;i++){
 		if(a[i]<m && b[i]=0){
 			m=a[i];
 			l=i;
 		}
 	}
 	
 	if(l==-1) break;
 	
 	b[l]=1;
 	
 	for(i=0;i<n;i++){
 		if(a[i]>=d[l][i]+a[l]) a[i]=d[l][i]+a[l];
 	}
 }
 
 for(i=0;i<n;i++){
 	if(a[i]==INF) printf("%d -1\n",i);
 	else printf("%d %d\n",i,a[i]);
 }
 
 return 0;
}

 return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

struct Edge {
    int from, to, cost;
    Edge(int f, int t, int c) : from(f), to(t), cost(c) { }
    bool operator <(const Edge& e) const {
        return cost < e.cost;
    }
};

const int INF = (int)1e9;
typedef pair<int, int> P;

vector<int> dijkstra(const vector<vector<Edge> >& g, int s) {
    vector<int> res(g.size(), INF);
    priority_queue<P, vector<P>, greater<P> > Q;
    Q.push(P(0, s));
    res[s] = 0;
    while(!Q.empty()) {
        P p = Q.top(); Q.pop();
        if(p.first > res[p.second]) continue;
        for(int i = 0; i < (int)g[p.second].size(); i++) {
            Edge e = g[p.second][i];
            if(res[e.to] > p.first + e.cost) {
                res[e.to] = p.first + e.cost;
                Q.push(P(res[e.to], e.to));
            }
        }
    }
    return res;
}

int main() {
    int N; cin >> N;
    vector<vector<Edge> > es(N);
    for(int i = 0; i < N; i++) {
        int u, k; cin >> u >> k;
        while(k--) {
            int v, c; cin >> v >> c;
            es[u].push_back(Edge(u, v, c));
        }
    }
    vector<int> res = dijkstra(es, 0);
    for(int i = 0; i < N; i++) {
        cout << i << ' ' << res[i] << endl;
    }
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define INF 1<<20
using namespace std;
int n;
int A[1000][1000];
int d[1000];
bool used[1000];
int D[1000][1000];

void Init(int f){
  for(int i=0;i<f;i++){
    for(int j=0;j<f;j++)D[i][j] = INF;
  }

}

int main(){
  scanf("%d",&n);
  int u,k,C,V;
  Init(n);
 
  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;
	D[V][u] = 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++: #include <iostream>
#include <stack>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#include <string.h>
#include <cstdio>
#include <numeric>
#include <string.h>
#include <set>
#include <map>
#define ll long long
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
#define mec(b,a) memcpy(b,a,sizeof(a))

const ll inf = 0x3f3f3f3f;
const int maxn = 1000;

int data[maxn][maxn];
int n;

void dijkstra(){
	int i,j,min,v;
	int d[maxn];
	bool vis[maxn];
 
	for(i=0;i<n;i++){
		vis[i]=0;
		d[i]=data[0][i];
	}
 
	for(i=0;i<n;i++){
		min=inf;
		for(j=0;j<n;j++)
			if(!vis[j] && d[j]<min){
				v=j;
				min=d[j];
			}
		vis[v]=1;
 
		for(j=0;j<n;j++)
			if(!vis[j] && d[j]>data[v][j]+d[v])
				d[j]=data[v][j]+d[v];
	}
	for(int i=0; i<n; i++){
		cout<<i<<" "<<d[i]<<endl;
	}
}

int main(){
	cin>>n;
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			if(i==j){
				data[i][j] = 0;
			}else{
				data[i][j] = data[j][i] = inf;
			}
		}
	}
	for(int i=0; i<n; i++){
		int start,m;
		cin>>start>>m;
		for(int j=0; j<m; j++){
			int end,weight;
			cin>>end>>weight;
			data[start][end] = data[end][start] = min(data[start][end],weight);
		}
	}
	
	dijkstra();
	 
	
    return 0;
}