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 <cstdio>
#include <set>
#include <algorithm>
#include <cstring>

#define INF 1000000
using namespace std;

struct edge
{
  int to, nxt, w;
};

edge E[500005];
int head[10005], cnt = 0, dist[10005];
void add_edge(int u, int v, int w)
{
  E[cnt].to = v;
  E[cnt].w = w;
  E[cnt].nxt = head[u];
  head[u] = cnt++;
}

void dijkstra(int src)
{
  set <pair <int, int> > setds;
  for (int i = 0; i < 10005; i++)
  {
    dist[i] = INF;
  }
  setds.insert(make_pair(0, src));
  dist[src] = 0;
  while (!setds.empty())
  {
    pair<int, int> tmp = *(setds.begin());
    setds.erase(setds.begin());
    int u = tmp.second;
    for (int e = head[u]; ~e; e = E[e].nxt)
    {
      int v = E[e].to;
      int w = E[e].w;
      if (dist[v] > dist[u] + w)
      {
        if (dist[v] != INF)
        {
          setds.erase(setds.find(make_pair(dist[v], v)));
        }
        dist[v] = dist[u] + w;
        setds.insert(make_pair(dist[v], v));
      }
    }
  }
}

int main()
{
  memset(head, -1, sizeof(head));

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
  {
    int v, deg;
    scanf("%d%d", &v, &deg);
    for (int j = 0; j < deg; j++)
    {
      int to, cost;
      scanf("%d%d", &to, &cost);
      add_edge(v, to, cost);
    }
  }
  dijkstra(0);
  for (int i = 0; i < n; i++)
  {
    printf("%d %d\n", i, dist[i]);
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> node;
typedef vector<node> Adj_list;
typedef vector<Adj_list> Adj_lists;

class less_cost{
public:
  bool operator()(const pair<int, int> lhs, const pair<int, int> rhs) const{
    return lhs.second > rhs.second;
  }
};

int main(){

  int n;
  scanf("%d", &n);

  Adj_lists adj_lists(n);

  // read weigts
  for(int i=0; i<n; i++){
    int u, k;
    scanf("%d %d", &u, &k);
    for(int j=0; j<k; j++){
      int v,c;
      scanf("%d %d", &v, &c);
      adj_lists.at(u).push_back(node(v, c));
    }
  }

  // initialize cost
  vector<int> d(n, INT_MAX);
  d.at(0) = 0;
  priority_queue<node, vector<node>, less_cost> pq;
  pq.push(node(0, d.at(0)));
  vector<bool> is_belong_to_sssp(n, false);

  //dijkstra's algorithm
  while(!pq.empty()){
    // extract node u in V-S that has minimum cost
    const node u = pq.top();
    pq.pop();

    // if u's cost is larger than d[u],
    // than ignore this one
    if(u.second > d.at(u.first)) continue;


    is_belong_to_sssp.at(u.first) = true;

    // update node in V-S and adjacent to u
    for(const node& v : adj_lists.at(u.first)){
      if(!is_belong_to_sssp.at(v.first) && d.at(u.first) + v.second < d.at(v.first)){
        d.at(v.first) = d.at(u.first) + v.second;
        pq.push(node(v.first, d.at(v.first)));
      }
    }
  }

  // output
  for(int i=0; i<n; i++){
    printf("%d %d\n", i, d.at(i));
  }

  return 0;
}

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 WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

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

int main()
{
	int k,u,c,v;
	cin>>n;
	REP(i,n)
	{
		cin>>u>>k;
		REP(j,k)
		{
			cin>>v>>c;
			adj[u].PB(MP(v,c));
		}
	}
	
	dijkstra();
	
	return 0;
}