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

	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<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define INF 1 << 30
struct ss{
	int next;
	int to;
	int val;
}e[200050];
int head[200050];
int tot;
void add(int x,int y,int val){
	e[tot].next=head[x];
	e[tot].to=y;
	e[tot].val=val;
	head[x]=tot++;
}
int n;
int vis[200000];
int dis[200050];
void spfa(){
	for(int i=0;i<n;i++){
		dis[i]=INF;
	}
	vis[0]=1;
	dis[0]=0;
	queue<int> st;
	st.push(0);
	while(!st.empty()){
		int t=st.front();
		st.pop();
		vis[t]=0;
		for(int i=head[t];i!=-1;i=e[i].next){
			int to=e[i].to;
			if(dis[to]>dis[t]+e[i].val){
				dis[to]=dis[t]+e[i].val;
				if(!vis[to]){
					st.push(to);
					vis[to]=1;
				}
			}
		}
	}
}
int main(){
	scanf("%d",&n);
	tot=0;
	memset(head,-1,sizeof(head));
	int a,to,val,num;
	for(int i=0;i<n;i++){
		scanf("%d %d",&a,&num);
		for(int j=1;j<=num;j++){
			scanf("%d %d",&to,&val);
			add(a,to,val);
		}
	}
	spfa();
	printf("0 0\n");
	for(int i=1;i<n;i++){
		printf("%d %d\n",i,dis[i]);
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#define Lens 10005
#define Inf 1<<22

using namespace std;
vector<pair<int, int> > adj[Lens];
int N;
char c[Lens];
int d[Lens];

void dijkstra()
{
	d[0] = 0;
	priority_queue<pair<int, int> > PQ;
	PQ.push(make_pair(0, 0));
	while (!PQ.empty())
	{
		pair<int, int> t = PQ.top();
		PQ.pop();
		int u = t.second;
		c[u] = 'b';
		if (d[u] < -t.first) continue;
		for (int i = 0; i < adj[u].size(); i++)
		{
			int tmp = adj[u][i].first;
			if (c[tmp] == 'w'&&d[tmp] > d[u] + adj[u][i].second)
			{
				d[tmp] = d[u] + adj[u][i].second;
				PQ.push(make_pair(-d[tmp],tmp));
				cout << tmp << "  " << d[tmp] << endl;
			}
		}

	}
}

int main()
{

	cin >> N;
	for (int i = 0; i < N; i++)
	{
		int k, n;
		cin >> k >> n;
		c[k] = 'w';
		d[k] = Inf;
		for (int j = 0; j < n; j++)
		{
			int v, tw;
			cin >> v >> tw;
			adj[k].push_back(make_pair(v, tw));
		}

	}
	dijkstra();
	for (int i = 0; i < N; i++)
	{
		cout << i << ' ' << d[i] << endl;;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<utility>
#include<limits>
using namespace std;
typedef pair<int, int> Edge;
int main()
{
	int n; cin >> n;
	vector< vector< Edge > > lists;

	for (int i = 0; i < n; i++)
	{
		vector<Edge> list;
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			Edge edge;
			cin >> edge.second >> edge.first;
			list.push_back(edge);
		}
		lists.push_back(list);
	}

	vector<int> costs(n, -1);
	costs[0] = 0;
	priority_queue<Edge> queue;
	queue.push(Edge(0, 0));

	while (!queue.empty())
	{
		Edge edge = queue.top(); queue.pop();
		int dest = edge.second;
		int cost = edge.first;
		if (costs[dest]<cost)continue;
		for (Edge next : lists[dest])
		{
			next.first += cost;
			if (costs[next.second] > next.first||costs[next.second]==-1)
			{
				costs[next.second] = next.first;
				queue.push(next);
			}
		}
	}
	for (int i = 0; i < n; i++)
		cout << i << " " << costs[i] << endl;

	char c;
	cin >> c;

	return 0;
}