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++: #include<algorithm>
#include<iostream>
//#include<cstdlib>
//#include<cstring>
//#include<cstdio>
//#include<vector>
#include<queue>
//#include<stack>
//#include<cmath>
//#include<list>
//#include<map>
//#include<set>
using namespace std;

//typedef vector<int> VI;
//typedef vector<VI> VVI;
//typedef vector<string> VS;
//typedef stack<int> SI;
//typedef queue<int> QI;
//typedef pair<int,int> PII;
//typedef long long LL;

//#define d(x)  cout<<#x<<" = "<<(x)<<endl;
//#define ALL(a)  (a).begin(),(a).end()
//#define RALL(a) (a).rbegin(), (a).rend()
//#define PB push_back
//#define MP make_pair
//#define EMP empty()
//#define SZ(a) int((a).size())
//#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
//#define REACH(i,c) for(typeof((c).rbegin()) i=(c).rbegin(); i!=(c).rend(); ++i)
//#define EXIST(s,e) ((s).find(e)!=(s).end())
//#define SORT(c,n) sort(c,c+n)
//#define VSORT(c) sort((c).begin(),(c).end())
//#define FOR(i,a,b) for(int i=(a);i<(b);++i)
//#define REP(i,n)  FOR(i,0,n)
//#define DFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
//#define DREP(i,n) DFOR(i,n,0)
//#define INF 1000000000

//const double EPS = 1e-10;
//const double PI  = acos(-1.0);
////////////////////////////////////////////////
static const int MAX=10000;
static const int INF=(1<<20);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n;
vector<pair<int,int>> adj[MAX];
void dijkstra()
{
	priority_queue<pair<int,int>> PQ;
	int color[MAX];
	int d[MAX];
	for(int i=0;i<n;i++)
	{
		d[i]=INF;
		color[i]=WHITE;
	}
	
	d[0]=0;
	PQ.push(make_pair(0,0));
	color[0]=GRAY;
	
	while(!PQ.empty())
	{
		PII f=PQ.top();
		PQ.pop();
		int u=f.second;
		color[u]=BLACK;
		
		if(d[u]<f.first*(-1))continue;
		
		for(int j=0;j<SZ(adj[u]);j++)
		{
			int v=adj[u][j].first;
			if(color[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));
			color[v]=GRAY;
		}
	}
	for(int i=0;i<n;i++)
	{
		cout<<i<<" "<<(d[i]==INF?-1:d[i])<<endl;
	}
}

int main()
{
	int k,u,c,v;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>u>>k;
		for(int j=0;j<k;j++)
		{
			cin>>v>>c;
			adj[u].push_back(make_pair(v,c));
		}
	}
	
	dijkstra();
	
	return 0;
}

C++: #include <iostream>
#include <utility>
#include <queue>
#include <vector>
using namespace std;
#define M 10010
long long int Min(long long int x,long long int y){
        return x>y?x:y;
}
int main(){
        int N;
        cin >> N;
        vector <pair<int,int> >adjancent[M];
        for(int i=0 ; i<N ; i++ ){
                int node,degree,to,cost;
                cin >> node >> degree;
                for(int l=0;l<degree;l++){
                        cin >> to >> cost;
                        adjancent[node].push_back(make_pair(to,cost));
                }
        }
        priority_queue<pair<long long int,long long int> > descending;
        descending.push(make_pair(0,0));
        vector<long long int> dp(N,2400000000);
        dp[0]=0;
        while(descending.size()!=0){
                pair<long long int,long long int> now=descending.top();descending.pop();
                for(int i=0;i<adjancent[now.first].size();i++){
                        pair<long long int,long long int>next=adjancent[now.first][i];
                        if(dp[next.first]>dp[now.first]+next.second){
                                dp[next.first]=dp[now.first]+next.second;
                                descending.push(make_pair(next.first,dp[next.first]));
                        }
                }
        }
        for(int i=0 ; i<N ; i++ )
                cout<<i<<" "<<dp[i]<<endl;
        return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=1000000;
const int infly=(1<<21);
const int white=0;
const int grey=1;
const int black=-1;

struct node{
    int k;
    int to;
    int next;
};

struct node m[2*maxn+5];
int head[maxn+5],n=0;
int color[maxn+5],d[maxn+5],p[maxn+5];
int tu,k,v,c,nn;
int cnt=0;

void add(int u,int v,int tot)
{
    m[++n].k=v;
    m[n].next=head[u];
    m[n].to=tot;
    head[u]=n;
}

void ds()
{
    priority_queue<pair<int, int> > p;
    for(int i=0;i<=maxn;i++)
    {
        color[i]=white;
        d[i]=infly;
    }

    d[0]=0;
    p.push(make_pair(0,0));
    color[0]=grey;

    while(!p.empty())
    {
        pair<int,int> f=p.top();
        p.pop();
        int u=f.second;

        color[u]=black;

        for(int j=head[u];j;j=m[j].next)
        {
            int v=m[j].k;
            if(color[v]==black) continue;
            if(d[v]>d[u]+m[j].to)
            {
                d[v]=d[u]+m[j].to;
                p.push(make_pair(d[v]*(-1),v));
                color[v]=grey;
            }
        }
    }
}

int main()
{
    scanf("%d",&nn);
    for(int i=1;i<=nn;i++)
    {
        scanf("%d%d",&tu,&k);
        for(int j=1;j<=k;j++)
        {
            scanf("%d%d",&v,&c);
            add(tu,v,c);
        }
    }


    ds();
    for(int i=0;i<nn;i++)
    {
        printf("%d ",i);
        if(d[i]!=infly) printf("%d\n",d[i]);
        else printf("-1\n");
    }

    return 0;
}

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

typedef struct{
  int A[10010];
  long long ans;
}node;
node T[10010];
int Q[100300003];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    num=Q[head++];
        if(head==100300002)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==100300002)tail=1;
	}
      }
    }
  }
}

int main(){
  int num,son,z,w;
  cin>>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++){
    cin>>num>>son;
    T[num].ans=LONG_LONG_MAX;
    for(int j=0;j<son;j++){
      cin>>z>>w;
      T[num].A[z]=w;
      }
  }
  Q[0]=0;
  T[0].ans=0;
  function(0);
 
  for(int i=0;i<n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }
  return 0;
}