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++: //
// Created by 李青坪 on 2018/11/8.
//
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#define INFINITY 1<<21
#define NIL -1
#define MAXN 10000

using namespace std;
struct Node{
    int id;
    int weight;
    friend bool operator < (Node x1, Node x2) {
        return x1.weight < x2.weight;
    }
};

struct cmp{
    bool operator ()(Node &a, Node &b){
        return a.weight > b.weight;
    }
};

vector<Node> graph[MAXN];
priority_queue<Node, vector<Node>, cmp> V_S;
int n;
int d[MAXN];
bool visited[MAXN];

void init(){
    fill(d, d+MAXN, INFINITY);
    d[0] = 0;
    fill(visited, visited+MAXN, false);
    Node node;
    node.id = 0;
    node.weight = 0;
    V_S.push(node);
}

void updateMinWeight(Node u){
    vector<Node>::iterator it;
    for (it = graph[u.id].begin(); it != graph[u.id].end(); ++it){ //遍历与u相连的所有结点，看有没有结点到S的距离减小了
        if (visited[(*it).id]) continue;
        if (d[(*it).id] > d[u.id] + (*it).weight){
            d[(*it).id] = d[u.id] + (*it).weight;
            Node node;
            node.id = (*it).id;
            node.weight = d[(*it).id];
            V_S.push(node);
        }
    }
}

void dijkstra(){
    while (!V_S.empty()){
        Node u = V_S.top();
        V_S.pop();
        visited[u.id] = true;
        updateMinWeight(u);
    }
}

int main(){
    scanf("%d", &n);
    int u, k;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &u, &k);
        int v, w;
        Node node;
        for (int j = 0; j < k; ++j) {
            scanf("%d %d", &v, &w);
            node.id = v;
            node.weight = w;
            graph[u].push_back(node);
        }
    }
    init();
    dijkstra();
    for (int l = 0; l < n; ++l) {
        printf("%d %d\n", l, d[l]);
    }
//    Node l[6];
//    l[0].id = 0; l[0].weight = 3;
//    l[1].id = 1; l[1].weight = 2;
//    l[2].id = 2; l[2].weight = 5;
//    l[3].id = 3; l[3].weight = 3;
//    l[4].id = 4; l[4].weight = 4;
//    l[5].id = 5; l[5].weight = 2;
//    for (int i = 0; i < 6; ++i) {
//        V_S.push(l[i]);
//    }
//    for (int j = 0; j < 6; ++j) {
//        printf("id: %d weight: %d\n", V_S.top().id, V_S.top().weight);
//        V_S.pop();
//    }
}
/*
5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3
 */

C++: ///
// File:  alds1_12_c.cpp
// Author: ymiyamoto
//
// Created on Wed Oct 18 22:36:29 2017
//

#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define SIZE 10000

struct route_t {
  uint32_t node;
  int64_t weight;

  bool operator<(const route_t &r) const
  {
    return weight > r.weight;
  }
};

static vector<route_t> graph[SIZE];
static int64_t costs[SIZE];
static bool visited[SIZE];
static priority_queue<route_t> q;

static void dikstra()
{
  q.push({0, 0});
  costs[0] = 0;

  while (!q.empty()) {
    route_t r = q.top();
    q.pop();

    if (!visited[r.node]) {
      visited[r.node] = true;
      for (uint32_t i = 0; i < graph[r.node].size(); i++) {
        int64_t cost = costs[r.node];
        uint32_t node = graph[r.node][i].node;
        if (cost != -1) {
          if (costs[r.node] + cost < costs[node]) {
            costs[node] = costs[r.node] + cost;
            q.push({node, costs[node]});
          }
        }
      }
    }
  }
}

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    costs[i] = INT32_MAX;
  }

  for (uint32_t i = 0; i < n; i++) {
    uint32_t id, k;
    cin >> id >> k;
    for (uint32_t j = 0; j < k; j++) {
      route_t r;
      cin >> r.node >> r.weight;
      graph[id].push_back(r);
    }
  }

  dikstra();

  for (uint32_t i = 0; i < n; i++) {
    cout << i << " " << costs[i] << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

struct edge{int to,cost;};
using Graph=vector<vector<edge>>;
using pint=pair<int,int>;
const int INF=1e9;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N; cin>>N;
    Graph G(N);
    for(int i=0;i<N;i++){
        int u,k; cin>>u>>k;
        while(k--){
            int v,c; cin>>v>>c;
            G[u].push_back({v,c});
        }
    }

    vector<int> dist(N,INF),finished(N,false);
    priority_queue<pint,vector<pint>,greater<pint>> Q;
    dist[0]=0;
    Q.push({0,0});
    while(!Q.empty()){
        int v=Q.top().second; Q.pop();
        if(finished[v]) continue;
        finished[v]=true;
        for(edge e:G[v]) if(chmin(dist[e.to],dist[v]+e.cost)){
            Q.push({dist[e.to],e.to});
        }
    }
    for(int i=0;i<N;i++){
        cout<<i<<' '<<dist[i]<<endl;
    }
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
using namespace std;
const int INF = 2000000000;
const int num = 10000;
int n, k, u, v, w;
int c[num];
int d[num];
vector <pair<int, int> > ve[num];
int main() {
	priority_queue <pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
	fill(d, d + num, INF);
	d[0] = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v >> w;
			ve[u].push_back(make_pair(v, w));
		}
	}
	fill(c, c + num, 0);
	q.push(make_pair(0, 0));
	while (!q.empty()) {
		int f = q.top().first;
		int s = q.top().second;
		q.pop();
		c[s] = 1;
		if (d[s] < f) continue;
		for (int i = 0; i < ve[s].size(); i++) {
			int fi = ve[s][i].first;
			int se = ve[s][i].second;
			if (c[fi]) continue;
			if (d[fi] > d[s] + se) {
				d[fi] = d[s] + se;
				q.push(make_pair(d[fi], fi));
			}
		}
	}
	for (int i = 0; i < n; i++) cout << i << " " << d[i] << endl;
	return 0;
}