Go: package main

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

const (
	white int = iota
	gray
	black
)

type node struct {
	id     int
	parent int
	edge   []int
	weight []int
	color  int
}

type graph struct {
	nodes    []node
	distance []int
}

var infty = 1 << 32
var null = -1

func constructgraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n := nextInt(sc)

	g := &graph{make([]node, n), make([]int, n)}

	for i := 0; i < n; i++ {
		id := nextInt(sc)
		edges := nextInt(sc)
		n := node{}
		n.id = id
		n.edge = make([]int, edges)
		n.weight = make([]int, edges)
		n.color = white
		for j := 0; j < edges; j++ {
			to := nextInt(sc)
			w := nextInt(sc)
			n.edge[j] = to
			n.weight[j] = w
		}
		g.nodes[id] = n
	}
	return g
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func (g *graph) preparedijkstra(s int) {
	for i := 0; i < len(g.nodes); i++ {
		g.nodes[i].color = white
		g.nodes[i].parent = null
		g.distance[i] = infty
	}
	g.distance[s] = 0
}

func (g *graph) dijkstra(s int) []int {
	g.preparedijkstra(s)

	for {
		var u int
		mincost := infty
		for i, d := range g.distance {
			if g.nodes[i].color != black && mincost > d {
				mincost = d
				u = i
			}
		}

		if mincost == infty {
			break
		}

		g.nodes[u].color = black

		for i, e := range g.nodes[u].edge {
			if g.nodes[e].color != black {
				if g.distance[e] > g.distance[u]+g.nodes[u].weight[i] {
					g.distance[e] = g.distance[u] + g.nodes[u].weight[i]
					g.nodes[e].parent = u
					g.nodes[e].color = gray
				}
			}
		}
	}
	return g.distance
}

func printShortestPath(d []int) {
	for i, v := range d {
		fmt.Printf("%d %d\n", i, v)
	}
}

func main() {
	g := constructgraph()
	d := g.dijkstra(0)
	printShortestPath(d)
}

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