Go: package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
	"container/heap"
)

type Node struct {
	Id int
	Dis int
}

type PriorityQueue []*Node

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].Dis < pq[j].Dis
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PriorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.(*Node))
}

func (pq *PriorityQueue) Pop() interface{} {
	n := len(*pq)
	pop := (*pq)[n-1]
	*pq = (*pq)[0:n-1]
	return pop
}

func main() {
	var n int
	fmt.Scan(&n)

	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	g := make([][]int, n)
	c := make([][]int, n)
	pq := make(PriorityQueue, 0, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		g[u] = make([]int, k)
		c[u] = make([]int, k)
		for j := 0; j < k; j++ {
			sc.Scan()
			g[u][j], _ = strconv.Atoi(sc.Text())
			sc.Scan()
			c[u][j], _ = strconv.Atoi(sc.Text())
		}

		if u == 0 {
			d[u] = 0
		} else {
			d[u] = 1 << 30
		}
		pq.Push(&Node{u, d[u]})
	}
	heap.Init(&pq)

	for pq.Len() > 0 {
		node := heap.Pop(&pq).(*Node)
		for i := 0; i < len(g[node.Id]); i++ {
			nextId := g[node.Id][i]
			nextDis := node.Dis + c[node.Id][i]
			if nextDis < d[nextId] {
				d[nextId] = nextDis
				heap.Push(&pq, &Node{nextId, nextDis})
			}
		}
	}

	wr := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		wr.WriteString(strconv.Itoa(i))
		wr.WriteString(" ")
		wr.WriteString(strconv.Itoa(d[i]))
		wr.WriteString("\n")
	}
	wr.Flush()
}

C++: //
//  main.cpp
//
//
//  Created by ???????????? on 2017/10/23.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <queue>
#include <algorithm>


#define n_max 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1<<20

using namespace std;

int n, M[n_max][n_max], color[n_max], d[n_max], p[n_max];

void init(){
    int i, j, id, num, node;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            M[i][j] = INFTY;
        }
    }
    
    
    for(i = 0; i < n; i++){
        scanf("%d %d", &id, &num);
        for(j = 0; j < num; j++){
            scanf("%d", &node);
            scanf("%d", &M[id][node]);
        }
        color[i] = WHITE;
        d[i] = INFTY;
    }
}

void dijkstra(){
    //initializing
    d[0] = 0;
    p[0] = -1;
    
    priority_queue<pair<int, int>> PQ;
    
    PQ.push(make_pair(0, 0));
    
    int u;
    while(!PQ.empty()){
        int mincost = INFTY;
        //first weight, second idx
        pair<int, int> f= PQ.top(); PQ.pop();
        u = f.second;
        color[u] = BLACK;
        
        if(d[u] < f.first*(-1)) continue;
        
        for(int v = 0; v < n; v++){
            if((color[v] != BLACK) && (M[u][v] != INFTY)){
                if((d[u] + M[u][v]) < d[v]){
                    d[v] = M[u][v] + d[u];
//                    p[v] = u;
                    PQ.push(make_pair(d[v]*(-1), v));
                    color[v] = GRAY;
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        cout<<i<<" "<<d[i]<<endl;
    }
}

int main(int argc, const char * argv[]) {
    init();
    dijkstra();
    //    std::cout << "Hello, World!\n";
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int INFTY=(1<<20);
int n;
vector<pair<int,int> >adj[10000];
void di()
{
    priority_queue<pair<int,int> >PQ;
    int c[10000],d[10000];
    for(int i=0;i<n;i++)
        d[i]=INFTY;
    d[0]=0;
    PQ.push(make_pair(0,0));
    while(!PQ.empty())
    {
        pair<int,int>f=PQ.top();PQ.pop();
        int u=f.second;
        c[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(c[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));
                c[v]=GRAY;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%d %d\n",i,(d[i]==INFTY?-1:d[i]));
    }
}
int main()
{
    scanf("%d",&n);
    int a,b,c,d;
    for(int i=0;i<n;i++)
    {
        scanf("%d %d",&a,&b);
        for(int j=0;j<b;j++)
        {
            scanf("%d %d",&c,&d);
            adj[a].push_back(make_pair(c,d));
        }
    }
    di();
    return 0;
}

C++: #include<iostream>
#include<climits>
#include<cstdio>
using namespace std;

typedef struct{
  int A[10001];
  int ans;
}node;

int head=0;
int tail=1;
int n;


int main(){
  int num,son,z,w;
  cin>>n;
  node *T=new node[n];
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      T[i].A[j]=-1;
    }
  }

  for(int i=0;i<n;i++){
    scanf("%d%d",&num,&son);
    T[num].ans=INT_MAX;
    for(int j=0;j<son;j++){
      scanf("%d%d",&z,&w);
      T[num].A[z]=w;
      }
  }

  T[0].ans=0;

  int *Q=new int[1000000];
  Q[0]=0;
  while(true){
    if(head==tail)break;
    num=Q[head++];
    //if(head==n*5-1)head=1;
    for(int i=0;i<n;i++){
      if(T[num].A[i]!=-1){
	if(T[i].ans>T[num].ans+T[num].A[i]){
	  T[i].ans=T[num].ans+T[num].A[i];
	  Q[tail++]=i;
	  //if(tail==n*5-1)tail=1;
	}
      }
    }
  }
  delete [] Q;
 
  for(int i=0;i<n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }
  delete [] T;
  return 0;
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#include<functional>
#define MAX 10000
#define INFTY (1<<20)
#define MPII(a,b) make_pair((a),(b))
#define PII pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;

class Node
{
public:
	Node() {};
	int d;
	bool done;
	priority_queue<PII, vector<PII>, greater<PII>> E;
};

int main()
{
	priority_queue<PII, vector<PII>, greater<PII>> order;
	Node nodes[MAX];
	REP(i, MAX)
	{
		nodes[i].done = false;
		nodes[i].d = INFTY;
	}
	int n;
	cin >> n;
	FOR(i, 0, n)
	{
		int ID, m;
		cin >> ID >> m;
		REP(l, m)
		{
			int v, w;
			cin >> v >> w;
			PII p = MPII(w, v);
			nodes[ID].E.push(p);
		}
	}
	PII now = MPII(0, 0);
	order.push(now);
	nodes[now.second].d = 0;
	while (1)
	{
		if (!nodes[now.second].done)
		{
			while (!nodes[now.second].E.empty())
			{
				PII temp = nodes[now.second].E.top();
				nodes[now.second].E.pop();
				int nextv = temp.second;
				int w = temp.first;
				if (nodes[nextv].d > nodes[now.second].d + w)
				{
					nodes[nextv].d = nodes[now.second].d + w;
				}
				order.push(MPII(nodes[nextv].d,nextv ));
			}
			nodes[now.second].done = true;
		}
		order.pop();
		if (order.empty())break;
		now = order.top();
	}
	REP(i, n)
	{
		cout << i << " " << nodes[i].d << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <functional>
#include <string.h>

using namespace std;
static const int INF = 2e9;
static const int SIZE = 10001;
typedef pair<int, int> P;
struct edge
{
	int to;
	int cost;
};

int d[SIZE];
vector<edge> G[SIZE];

void solve()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; ++j)
		{
			int to, cost;
			scanf("%d %d", &to, &cost);
//			cin >> to >> cost;
			edge e1;
			e1.to = to;
			e1.cost = cost;
			G[u].push_back(e1);
		}
	}
	for (int i = 0; i < SIZE; ++i)
	{
		d[i] = INF;
	}
	d[0] = 0;
	priority_queue<P, vector<P>, greater<P> > pque;
	pque.push(P(0, 0));
	while (!pque.empty())
	{
		P p = pque.top();
		pque.pop();
		int v = p.second;
		for (int i = 0; i < G[v].size(); ++i)
		{
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost)
			{
				d[e.to] = d[v] + e.cost;
				pque.push(P(d[e.to], e.to));
			}
		}
	}
	for (int i = 0; i < n; ++i)
	{
		printf("%d %d\n", i, d[i]);
//		cout << i << " " << d[i] << endl;
	}
}

int main()
{
	solve();
	return(0);
}