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>

#define NIL -2
#define INF 1000000001

using namespace std;
static const int BLK = 2;
static const int GRY = 1;
static const int WHT = 0;

void dijkstra(const vector<vector<pair<int,int> > > &adj_list, 
                    vector<int> &color, 
                    vector<int> &parent, 
                    vector<int> &minpath,
                    int start_node)
{
    priority_queue<pair<int,int>,vector<pair<int,int> >, greater<pair<int,int> > >  PQ;
    PQ.push(make_pair(start_node,0));

    while(!PQ.empty()){
        pair<int,int> t = PQ.top(); PQ.pop();
        int u = t.second;
        color[u] = BLK;
        if(minpath[u] < t.first) continue;
        for(int i = 0;i < adj_list[u].size();i++){
            int v = adj_list[u][i].first;
            if(color[v] != BLK){
                if(minpath[u] + adj_list[u][i].second < minpath[v]){
                    minpath[v] = minpath[u] + adj_list[u][i].second;
                    parent[v] = u;
                    color[v] = GRY;
                    PQ.push(make_pair(minpath[v],v));
                }  
            }
        }
        
    }

}


int main(){
    int mat_size;
    int node, deg;
    int to,weight;
    int temp;
    int cost = 0;

    scanf("%d",&mat_size);
    vector<vector<pair<int,int> > > adj_list(mat_size);
    vector<int> color(mat_size,WHT);
    vector<int> minpath(mat_size,INF);
    vector<int> parent(mat_size,-1);
    
    for(int i = 0;i < mat_size;i++){
        scanf("%d %d",&node,&deg);
        for(int j = 0;j < deg;j++){
            scanf("%d %d", &to, &weight);
            adj_list[node].push_back(make_pair(to,weight));
        }
    }
    
    minpath[0] = 0;
    dijkstra(adj_list,color,parent,minpath,0);

    for(int i = 0; i < mat_size;i++){
        printf("%d %d\n",i,minpath[i]);
    }

    return 0;   
}

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[100050];
int head[100050];
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[100050]={0};
int dis[10005];
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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define REPR(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
const int INF = 1001001001;
typedef pair<int,int> pii;

template<class t> inline bool chmax(t& a, t b) { if (a < b) { a = b; return 1; } return 0; }
template<class t> inline bool chmin(t& a, t b) { if (a > b) { a = b; return 1; } return 0; }

using Graph = vector<vector<pii> >;

int n;
vector<bool> seen;
vector<int> dist;
priority_queue<pair<int, int> > pq;

void dijkstra(const Graph &G, int v){
  seen[v] = true;

  for(pii p : G[v]){
    int u = p.first;
    int c = p.second;
    if(seen[u] == true) continue;
    if(chmin(dist[u], dist[v]+c)) pq.push(make_pair((-1)*dist[u], u));
  }

  while(!pq.empty()){
    pair<int, int> tmp;
    tmp = pq.top();
    pq.pop();
    if(seen[tmp.second]) continue;
    int nv = tmp.second;
    dijkstra(G, nv);
  }

  return;
}

int main(){
  cin >> n;

  Graph G(n);
  rep(i, n){
    int v, k; cin >> v >> k;
    rep(j, k){
      int u, c; cin >> u >> c;
      G[v].push_back(make_pair(u, c));
    }
  }

  seen.assign(n, false);
  dist.assign(n, INF);
  dist[0] = 0;
  dijkstra(G, 0);

  rep(i, n) cout << i << " " << dist[i] << endl;

  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i) 
#define PI acos(-1)

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

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

    int n;
    cin>>n;
    vector<vector<P>> adj(n);
    rep(i, n) {
        int u, k, v, c;
        cin>>u>>k;
        rep(j, k) {
            cin>>v>>c;
            adj[u].push_back(P(c, v));
        }
    }

    priority_queue<P, vector<P>, greater<P>> q;
    vector<int> wmap(n, -1);
    q.push(P(0, 0));
    while (!q.empty()) {
        P p = q.top(); q.pop();
        int w = p.first;
        int v = p.second;
        if (wmap[v] != -1) continue;

        wmap[v] = w;
        for (auto e : adj[v]) {
            q.push(P(e.first + w, e.second));
        }
    }

    rep(i, n) cout<<i<<" "<<wmap[i]<<endl;
}