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<cstdio>
#include<queue>
#include<vector>
#define MA 10005
#define INF (1<<30)
using namespace std;
struct Node{
    short id;//根据题意，id最大在short有效范围内
    int weight;
};

short n;
vector<pair<int, int> >adj[MA];//加权有向图的邻接表
priority_queue<Node> PQ;//优先级队列
bool state[MA];//节点状态，false为未访问
int dis[MA];//节点到0点的权值总和

/**重载'<'以自定义优先级队列的优先级，这里定义为权值小优先，与默认的大优先相反*/
operator < (const Node node1, const Node node2){
    if(node1.weight==node2.weight)
        return node1.id > node2.id;//权值相同比id，意义不大
    return node1.weight > node2.weight;//返回与正常比较相反的结果
}

/**初始化*/
void init(){
    for(short i=0;i<n;i++)
        dis[i] = INF;//初始权值置为无穷
    dis[0] = 0;//0点权值为0
    Node ini;//0点
    ini.id=0;ini.weight = 0;//0点初始值
    PQ.push(ini);//将0点推入队列
}

/**狄克斯特拉算法*/
void dijkstra(){
    while(!PQ.empty()){//执行至队列为空
        Node nowNode = PQ.top();//从队列中取出元素
        PQ.pop();//清除取出的元素
        short nowId = nowNode.id;//当前元素的ID
        state[nowId] = true;//该节点已从队列中清除，设为访问完毕
        if(dis[nowId]>nowNode.weight)continue;//取出最小值，如果不是最短路径则忽略
        int nowSize = adj[nowId].size();//空间换时间，减少部分运算量
        for(int i=0; i<nowSize; i++){//遍历当前元素的邻接节点
            short nextId = adj[nowId][i].first;//下一个节点的ID
            if(state[nextId]) continue;//如果该节点已被读取则跳过
            int nextWeight = dis[nowId] + adj[nowId][i].second;//0到下一个节点的权值
            if(dis[nextId] > nextWeight){//发现了更短的路径
                dis[nextId] = nextWeight;//更新权值数组对应的数据
                Node nextNode;//即将推入队列的节点
                nextNode.id=nextId; nextNode.weight = nextWeight;//将数据存入节点
                PQ.push(nextNode);//将存有数据的节点推入队列
            }
        }
    }
}

int main(){
    int id, flag, nextId, weight;
    freopen("D:/works/C++200/try136_AOJ_ALDS1_12_BC/test.txt", "r", stdin);
    scanf("%d", &n);
    for(int i=0;i<n;i++){
        scanf("%d%d",&id, &flag);
        while(flag--){
            scanf("%d%d", &nextId, &weight);//读取邻接表
            adj[id].push_back(make_pair(nextId, weight));
        }
    }
    init();//执行初始化
    dijkstra();//执行狄克斯特拉算法
    for(short i=0;i<n;i++){//输出全部节点数据
        printf("%d %d\n", i, ((dis[i]==INF)?(-1):(dis[i])));
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#define INF 1<<30
#define MAX 1000000
using namespace std;


struct edge {
  int from,to,cost,k;
};

edge es[MAX];



int main(){
  int n,u,k,E,c,v;
  E = 0;
  scanf("%d",&n);

  for(int i=0;i<n;i++){
    scanf("%d %d",&u,&k);
   
    for(int j=0;j<k;j++){
      scanf("%d %d",&v,&c);
      es[E].from = u;
      es[E].to = v;
      es[E++].cost = c;
    }
  }

  
  int d[MAX];
  for(int i=0;i<n;i++)d[i] = INF;
  d[0] = 0;
  while(true){
    bool update = false;
    for(int i=0;i<E;i++){
      edge e = es[i];
      if(d[e.from] != INF && d[e.to] > d[e.from] + e.cost){
	d[e.to] = d[e.from] + e.cost;
	update = true;
      }
    }
    if(!update)break;


  }

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

  return 0;
}

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

#define MAX 10000
#define INF (1<<30)
#define BLACK 0
#define GRAY 1
#define WHITE 2

int n;
vector< pair<int,int> > graph[MAX];

// プロトタイプ宣言
void dijkstra();
// ここまで

int main(){
	int now,k,to,w;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>now>>k;
		for(int j=0;j<k;j++){
			cin>>to>>w;
			graph[now].push_back( make_pair(to,w) );
		}
	}

	dijkstra();


	return 0;
}

void dijkstra(){
	priority_queue< pair<int,int> > PQ;
	int color[MAX];
	int d[MAX];
	for(int i=0;i<n;i++){
		d[i]=INF;
		color[i]=WHITE;
	}

	d[0]=0;
	PQ.push(make_pair(0,0));		// （重み、ノード番号）
	color[0]=GRAY;


	while(PQ.empty()==false){
		pair<int,int> f=PQ.top();	PQ.pop();
		int from = f.second;
		color[from]=BLACK;

		if(d[from] < f.first*(-1) )continue;

		for(pair<int,int> next:graph[from]){
			int to=next.first;
			int w=next.second;
			if(color[to]==BLACK) continue;

			if(d[to]>d[from]+w){
				d[to]=d[from]+w;

				PQ.push( make_pair(d[to]*(-1), to) );
				color[to]=GRAY;
			}
		}
	}
	for(int i=0;i<n;i++){
		cout<<i<<" "<<(d[i]==INF? -1:d[i])<<endl;
	}
}

C++: #include<iostream>
using namespace std;
#include<cstdio>
#include<vector>
#include<utility>
#include<queue>

typedef vector<pair<int, int> > adj;

struct current_cost
{
	int index, cost;
	bool operator < (const current_cost &cur) const
	{
		return cost > cur.cost;
	}
};

current_cost init(int i, int c)
{
	current_cost cur;
	cur.index = i, cur.cost = c;
	return cur;
}

int main()
{
	int i, j, k, n, m, next, cost;
	scanf("%d", &n);
	adj *adlist;
	adlist = new adj [n];

	for(k = 0; k < n; k++){
		scanf("%d %d", &i, &m);
		for(j = 0; j < m; j++){
			scanf("%d %d", &next, &cost);
			adlist[i].push_back(make_pair(next, cost));
		}
	}
	
	int *d;
	d = new int [n];
	d[0] = 0;
	for(i = 1; i < n; i++){
		d[i] = 200000;
	}

	priority_queue<current_cost> C;
	current_cost cur = init(0, 0);
	C.push(cur);

	int size;
	bool *check;
	check = new bool [n];
	for(i = 0; i < n; i++){ check[i] = false; }

	while(!C.empty())
	{
		cur = C.top(); C.pop();
		if(check[cur.index]) continue;
		check[cur.index] = true;

		i = cur.index;
		size = adlist[i].size();
		for(j = 0; j < size; j++){
			k = adlist[i][j].first;
			if(d[i] + adlist[i][j].second < d[k]){
				d[k] = d[i] + adlist[i][j].second;
				C.push(init(k, d[k]));
			}
		}
	};
	for(i = 0; i < n; i++){ printf("%d %d\n", i, d[i]); }

	return 0;
}