Go: package main

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

//make priority queue
type Item struct {
	id       int
	priority int // this state is priority and distans from start
	index    int
}

type PriorityQueue []*Item

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

// we want the ascending-priority-queue.
// So we use less than here.
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

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

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(dist int, id int) {
	for _, i := range *pq {
		if i.id == id {
			if i.priority > dist {
				i.priority = dist
			}
		}
	}
}

// the main code
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	return n
}

func dikstra(dist *[][]int, n int) *[]int {
	// preparate priority queue
	inf := 1 << 30
	pq := make(PriorityQueue, n)
	shortests := make([]int, n)
	for i := 0; i < n; i++ {
		pq[i] = &Item{
			id:       i,
			priority: inf,
			index:    i,
		}

		shortests[i] = inf
	}
	pq[0].priority = 0
	shortests[0] = 0
	heap.Init(&pq)

	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		shortests[item.id] = item.priority
		for id, d := range (*dist)[item.id] {
			if d != -1 {
				pq.update(item.priority+d, id)
			}
		}
		heap.Init(&pq)
	}
	return &shortests
}

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

	// init
	n := nextInt(sc)
	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dist[i][j] = -1
		}
	}

	// std Input
	for i := 0; i < n; i++ {
		u := nextInt(sc)
		k := nextInt(sc)
		for j := 0; j < k; j++ {
			v, c := nextInt(sc), nextInt(sc)
			dist[u][v] = c
		}
	}

	shortests := dikstra(&dist, n)
	for i, d := range *shortests {
		fmt.Printf("%d %d\n", i, d)
	}
}

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

#define INF (1 << 30)

struct edge{

  int cost, to;

};

struct state{

  int node, cost;
  bool operator <(const state &b)const{
    return cost > b.cost;
  }
};

int n;
int d[10001];
vector<edge> glaph[10001];
priority_queue<state> qu;

void sss(){

  fill(d,d+10001,INF);
  qu.push((state){0,0});
  d[0] = 0;

  while(!qu.empty()){

    state info;
    info = qu.top();
    qu.pop();

    for(int i = 0; i < (double)glaph[info.node].size(); i++){
      if(d[glaph[info.node][i].to] > glaph[info.node][i].cost + d[info.node]){
      d[glaph[info.node][i].to] = glaph[info.node][i].cost + d[info.node];
      qu.push((state){glaph[info.node][i].to,d[info.node]}); 
      }
	}
  }

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



}

int main(){

  cin >> n;

  for(int i = 0; i < n; i++){
    int k, v, c, u;
    cin >> u >> k;
    for(int j = 0; j < k; j++){
      cin >> v >> c;
      glaph[u].push_back((edge){c,v});
    }
  }

  sss();


}

C++: #include <cstdio>
#include <queue>
#include <algorithm>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

const int inf = 1e9;

typedef pair<int, int> P;

struct E{
	E(int t, int c): t(t), c(c){} 

	int t, c;
};

int n;
vector<E> g[10000];
int d[10000];

int main(){
	scanf("%d", &n);
	rep(i, n){
		int u, k;
		scanf("%d%d", &u, &k);
		rep(j, k){
			int v, c;
			scanf("%d%d", &v, &c);
			g[u].push_back(E(v, c));
		}
	}

	fill_n(d, n, inf);

	priority_queue<P, vector<P>, greater<P> > q;
	d[0] = 0;
	q.push(P(0, 0));
	while(!q.empty()){
		P v = q.top();
		q.pop();
		if(v.first > d[v.second]){
			continue;
		}
		rep(i, g[v.second].size()){
			E e = g[v.second][i];
			if(d[e.t] > v.first + e.c){
				d[e.t] = v.first + e.c;
				q.push(P(d[e.t], e.t));
			}
		}
	}

	rep(i, n){
		printf("%d %d\n", i, d[i]);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <string>
#define SIZE 10005
#define INF 10000005

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int V;
ll K;

struct edge{int to,cost;};
vector<edge> G[SIZE];
int d[SIZE];

void dijkstra(int s){
	priority_queue<P, vector<P>, greater<P>> que;
	fill(d,d+V,INF);
	d[s] = 0;
	que.push(P(0,s));

	while(!que.empty()){
		P p = que.top();que.pop();
		int v = p.second;
		if(d[v] < p.first)continue;
		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;
				que.push(P(d[e.to],e.to));
			}
		}
	}
}

int main()
{
	scanf("%d",&V);
	int u,k,v,c;
	for(int i=0;i<V;i++){
		scanf("%d%d",&u,&k);
		for(int j=0;j<k;j++){
			scanf("%d%d",&v,&c);
			G[u].push_back((edge){v,c});
		}
	}
	dijkstra(0);
	for(int i=0;i<V;i++){
		printf("%d %d\n",i,d[i]);
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <stack>

using namespace std;

#define mod 1000000007

int main()
{
	int n;
	cin >> n;
	int ans[10001] = {};
	vector<vector<pair<int, int> > > graph(n + 1);
	for(int i = 0; i < n; i++){
		ans[i] = -1;
		int u, k;
		cin >> u >> k;
		for(int j = 0; j < k; j++){
			int v, c;
			cin >> v >> c;
			graph[u].push_back(make_pair(v, c));
		}
	}
	priority_queue<pair<int, int> > qu;
	qu.push(make_pair(0, 0));
	ans[0] = 0;
	while(!qu.empty()){
		int nowd = -(qu.top()).first;
		int nownode = (qu.top()).second;
		qu.pop();
		for(int i = 0; i < graph[nownode].size(); i++){
			int node = graph[nownode][i].first;
			int next = nowd + graph[nownode][i].second;
			if(ans[node] < 0 || ans[node] > next){
				ans[node] = next;
				qu.push(make_pair(-next, node));
			}
		}
	}
	for(int i = 0; i < n; i++){
		cout << i << " " << ans[i] << endl;
	}
}