Go: package main

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

type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }
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 = i
	pq[j].index = j
}
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
}

type Q struct {
	pq *PriorityQueue
	ds map[int]int // It has always same value as pq. For random access.
}

func newQ() *Q {
	pq := new(PriorityQueue)
	heap.Init(pq)
	return &Q{
		pq: pq,
		ds: make(map[int]int),
	}
}
func (q *Q) pop() (u, d int) {
	item := heap.Pop(q.pq).(*Item)
	delete(q.ds, item.value)
	return item.value, item.priority
}
func (q *Q) get(u int) (int, bool) { // returns d
	d, ok := q.ds[u]
	return d, ok
}
func (q *Q) set(u, d int) {
	q.ds[u] = d
	for _, item := range *q.pq {
		if item.value != u {
			continue
		}

		item.priority = d
		heap.Fix(q.pq, item.index)
		return
	}
	heap.Push(q.pq, &Item{
		priority: d,
		value:    u,
	})
}
func (q *Q) len() int {
	return len(q.ds)
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscan(r, &n)
	cs := make([]map[int]int, n) // []v_i -> c_i
	for h := 0; h < n; h++ {
		var u, k int
		fmt.Fscan(r, &u, &k)
		m := make(map[int]int)
		for i := 0; i < k; i++ {
			var v, c int
			fmt.Fscan(r, &v, &c)
			m[v] = c
		}

		cs[u] = m
	}

	q := newQ()
	q.set(0, 0)
	visited := make(map[int]struct{})
	out := make([]int, n)

	for q.len() > 0 {
		u, d := q.pop()
		for uu, c := range cs[u] {
			// Consider the connection between u and uu.
			if _, ok := visited[uu]; ok {
				continue
			}
			dd := d + c
			if d, ok := q.get(uu); !ok || d > dd {
				q.set(uu, dd)
			}
		}

		out[u] = d
		visited[u] = struct{}{}
	}

	for u, d := range out {
		fmt.Println(u, d)
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxx = 1010;
const int INF = 1 << 30;
int n, m[maxx][maxx];
int u, v ,k, c;
int flag[maxx];

void dijkstra() {
	int d[maxx];
	for (int i = 0; i < n; i++) {
		d[i] = INF;
		flag[i] = 0;
	}

	d[0] = 0;
	while (1) {
		int minv = INF;
		u = -1;
		// ???????????????????°???????
		for (int i = 0; i < n; i++) {
			if (minv > d[i] && flag[i] == 0) {
				u = i;
				minv = d[i];
			}
		}

		if (u == -1)
			break;

		// ?????°???????????\S
		flag[u] = 1;

		// ??´??°?????±u?????????V-S???v?????????????°??????¬
		for (int v = 0; v < n; v++) {
			if (flag[v] == 0 && m[u][v] != INF && d[v] > d[u] + m[u][v]) {
				d[v] = d[u] + m[u][v];
			}
		}
	}

	for (int i = 0; i < n; i++)
		printf("%d %d\n", i, d[i] == INF ? -1 : d[i]);
}
int main()
{
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			m[i][j] = INF;
		}
	}
	for (int i = 0; i < n; i++) {
		scanf("%d %d", &u, &k);
		for (int j = 0; j < k; j++) {
			scanf("%d %d", &v, &c);
			m[u][v] = c;
		}
	}
	dijkstra();
	return 0;
}

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

const int INF=10000002ll;

int n,m[100][100];

void dijkstra();

int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      m[i][j]=INF;
    }
  }
  
  int k,c,u,v;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
      cin>>v>>c;
      m[u][v]=c;
    }
  }

  dijkstra();

  return(0);
}

void dijkstra(){
  int mn,d[100],sn[100];
  
  for(int i=0;i<n;i++){
    d[i]=INF;
    sn[i]=0;
  }

  d[0]=0;
  while(1){
    mn=INF;
    int u=-1;
    for(int i=0;i<n;i++){
      if(mn>d[i] && sn[i]==0){
	u=i;
	mn=d[i];
      }
    }
    if(u==-1)break;
    sn[u]=1;
    for(int i=0;i<n;i++){
      if(sn[i]==0 && m[u][i]!=INF){
	if(d[i]>d[u]+m[u][i]){
	  d[i]=d[u]+m[u][i];
	}
      }
    }
  }

  int ans;
  for(int i=0;i<n;i++){
    cout<<i<<" ";
    if(d[i]==INF)cout<<"-1"<<endl;
    else cout<<d[i]<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <climits>

using namespace std;

int n;
vector<int> g[100];
vector<int> w[100];
int d[100];
char color[100];

bool bellman() {
    for (int i = 0; i < n; ++i) {
        d[i] = INT_MAX;
    }
    d[0] = 0;

    for (int j = 0; j < n ; ++j) {
        for (int i = 0; i < n; ++i) {
            for (int k = 0; k < g[i].size(); ++k) {
                int v = g[i][k];
                if(d[i] != INT_MAX && d[v] > d[i] + w[i][k]) {
                    d[v] = d[i] + w[i][k];
                    if(j == n - 1) return true;
                }
            }
        }
    }
    return false;
}

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

    bool neg = bellman();

    for (int j = 0; j < n; ++j) {
        if (d[j] == INT_MAX) cout << "INF" << endl;
        else
            cout << j << " " << d[j] << endl;

    }

    return 0;
}

C++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_12_B: Single Source Shortest Path I

#include <algorithm>
#include <cstdio>

using uint = unsigned int;

class Graph {
  const uint n_;
  uint* const g_;
  const uint INFINIT;
  
public:

  Graph(uint n) : n_(n), g_(new uint[(n_*n_)]), INFINIT(100000*(n_-1)+1) {
    std::fill_n(g_, (n_*n_), INFINIT);
  }

  ~Graph() { delete[] g_; }
  
  uint* operator [](uint i) const { return g_+(n_*i); }

  void dijkstra(uint* dist)
  {
    std::fill_n(dist, n_, INFINIT);
    bool visited[n_];
    std::fill_n(visited, n_, false);
    auto i = 0u; // 0-start
    visited[i] = true;
    dist[i] = 0u;
    for (auto n = 0u; n < n_-1; ++n) {
      auto cur_min = INFINIT;
      auto es = (*this)[i];
      auto base_dist = dist[i];
      for (auto j = 0u; j < n_; ++j) {
        if (visited[j]) continue;
        dist[j] = std::min(dist[j], base_dist+es[j]);
        if (cur_min > dist[j]) {
          cur_min = dist[j];
          i = j;
        }
      }
      visited[i] = true;
    }
  }
};

int main()
{
  uint n;
  std::scanf("%u", &n);

  Graph g(n);

  uint u, k, c, v;
  for (auto i = 0u; i < n; ++i) {
    std::scanf("%u %u", &u, &k);
    for (auto j = 0u; j < k; ++j) {
      std::scanf("%u %u", &c, &v);
      g[u][c] = v;
    }
  }

  uint dist[n];
  g.dijkstra(dist);
  for (auto i = 0u; i < n; ++i) {
    std::printf("%u %u\n", i, dist[i]);
  }
}

// eof