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++: // C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int ll
#define loop(__x, __start, __end) for(Int __x = __start; __x < __end; __x++)
#define loopdown(__x, __start, __end) for(Int __x = __start; __x > __end; __x--)
#define rep(__n) for(Int __x = 0; __x < __n; __x++)
#define span(__a,__x,__y) __a.begin()+__x,__a.begin()+__y
#define span_all(__a) __a.begin(),__a.end()
#define len(__x) (__x.size())
#define last(__x) (__x[__x.size()-1])

using namespace std;

struct Point {
  Int x, y;
  Point(Int x, Int y): x(x), y(y) {}
  Point(): x(0), y(0) {}

  bool operator< (const Point &p1) {
    return x < p1.x && y < p1.y;
  }
};
ostream & operator << (ostream & out, Point const & v) {
  out << "Point(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Point & v) {
  Int x, y;
  in >> x; in >> y; v.x = x; v.y = y;
  return in;
}

// Geometry

#define EPS 0.0000000001

class Vector2 {
public:
  double x, y;
  
  Vector2(double x = 0, double y = 0): x(x), y(y) {}
  
  Vector2 operator + (Vector2 v) { return Vector2(x + v.x, y + v.y); }
  Vector2 operator - (Vector2 v) { return Vector2(x - v.x, y - v.y); }
  Vector2 operator * (double k) { return Vector2(x * k, y * k); }
  Vector2 operator / (double k) { return Vector2(x / k, y / k); }
  
  double length() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  double dot (Vector2 v) { return x * v.x + y * v.y; }
  double cross (Vector2 v) { return x * v.y - y * v.x; }
  
  bool operator < (const Vector2 &v) const {
    return x != v.x ? x < v.x : y < v.y;
  }
  
  bool operator == (const Vector2 &v) const {
    return fabs(x - v.x) < EPS && fabs(y - v.y) < EPS;
  }
};

ostream & operator << (ostream & out, Vector2 const & v) { 
  out<< "Vector2(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Vector2 & v) { 
  double x, y;
  in >> x;
  in >> y;
  v.x = x;
  v.y = y;
  return in;
}


class Dijkstra {
public:
  struct Edge {
    long long from, to, w;
    Edge(long long from, long long to, long long w): from(from), to(to), w(w) {}
    Edge() {}
  };

  struct Node {
    long long id, dist;

    Node(long long id, long long dist): id(id), dist(dist) {}
    bool operator< (const Node &other) const {
      return dist > other.dist;
    }
  };

  const long long INFTY = (1L << 21);
  long long V;

  Dijkstra(long long V): V(V), G(V, vector<Edge>(0)), dist(V, INFTY) {}

  long long distance(long long t) {
    return dist.at(t);
  }

  void addEdge(long long s, long long t, long long w, bool directed = true) {
    if (directed) {
      G[s].push_back(Edge(s, t, w));
      return;
    }

    G[s].push_back(Edge(s, t, w));
    G[t].push_back(Edge(t, s, w));
  }

  void sssp(long long src) {
    resetDist(src);
    priority_queue<Node> PQ;
    vector<bool> done(V, false);
    PQ.push(Node(src, 0));
    dist.at(src) = 0;

    while (PQ.size()) {
      Node node = PQ.top(); PQ.pop();
      if (done.at(node.id)) continue;
      done.at(node.id) = true;
      for (auto edge: G[node.id]) {
        if (done.at(edge.to)) continue;
        if (dist.at(node.id) + edge.w < dist.at(edge.to)) {
          dist.at(edge.to) = dist.at(node.id) + edge.w;
          PQ.push(Node(edge.to, dist.at(edge.to)));
        }
      }
    }
  }
private:
  vector<vector<Edge> > G;
  vector<long long> dist;

  void resetDist(long long src) {
    loop(x,0,dist.size()) {
      dist.at(x) = INFTY;
    }
    dist.at(src) = 0;
  }
};


#define MAX_N 10001

Int N;
vector<Int> S, T, W;


void input() {
  cin >> N;
  Int from, c, to, w;
  rep(N) {
    cin >> from >> c;
    rep(c) {
      cin >> to >> w;
      S.push_back(from);
      T.push_back(to);
      W.push_back(w);
    }
  }
}

void solve() {
  Dijkstra di(N);
  loop(i,0,S.size()) {
    di.addEdge(S.at(i), T.at(i), W.at(i));
  }
  di.sssp(0);
  loop(u,0,N) {
    cout << u << " " << di.distance(u) << endl;
  }
}

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

C++: #include <map>
#include <vector>
#include <cstdio>
using namespace std;
int main(){
	int n=0,N,i,j,k,c;
	scanf("%d",&N);
	vector<map<int,int> >dist(N);
	vector<bool>used(N);
	vector<int>cost(N);
	for(;n<N;n++){
		cost[n]=999999;
		for(scanf("%d%d",&i,&k);k--;dist[i][j]=c)scanf("%d%d",&j,&c);
	}
	for(cost[0]=0;;){ //dijkstra
		int m=999999;
		for(i=0;i<N;i++)if(!used[i]&&m>cost[i])m=cost[i];
		if(m==999999)break;
		for(j=0;j<N;j++)if(m==cost[j]){
			map<int,int>::iterator it=dist[j].begin();
			for(used[j]=1;it!=dist[j].end();it++)if(cost[it->first]>it->second+cost[j])cost[it->first]=it->second+cost[j];
		}
	}
	for(i=0;i<n;i++)printf("%d %d\n",i,cost[i]);
}

C++: #include <iostream>
#include <queue>	
#include <utility>	
#include <list>		
#define N 10000	
#define INF (1<<31-1);
using namespace std;

typedef pair<int,int> pii;	
vector<pii> adj[N];	
bool color[N];	
int dist[N];	
int n;	
priority_queue<pii> pq; 

int dijkstra(int k){	
	for(int i=0;i<n;i++){
		color[i]=false; 
		dist[i]=INF;	
	}
	color[k]=true; 
	dist[k]=0;	
	for(int i=0;i<adj[k].size();i++){
		pq.push(adj[k][i]); 
	}
	while(!pq.empty()){	
		pii temp=pq.top();pq.pop();	
		temp.first=-temp.first;	
		if(color[temp.second])continue;	
		color[temp.second]=true; 
		dist[temp.second]=temp.first;	
		for(int i=0;i<adj[temp.second].size();i++){
			if(!color[adj[temp.second][i].second])pq.push(make_pair(-dist[temp.second]+adj[temp.second][i].first,adj[temp.second][i].second));
		}
	}

	for(int i=0;i<n;i++) cout<<i<<" "<<dist[i]<<endl;
	return 0;
}
int main(void){
	int v,c;	
	int u,k;	
	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(-c,v));	
		}
	}
	dijkstra(0);	
	return 0;
}