Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Single Source Shortest Path

For a given weighted graph $G = (V, E)$, find the shortest path from a source to each vertex. For each vertex $u$, print the total weight of edges on the shortest path from vertex $0$ to $u$.

Input

In the first line, an integer $n$ denoting the number of vertices in $G$ is given. In the following $n$ lines, adjacency lists for each vertex $u$ are respectively given in the following format:

$u$ $k$ $v_1$ $c_1$ $v_2$ $c_2$ ... $v_k$ $c_k$

Vertices in $G$ are named with IDs $0, 1, ..., n-1$. $u$ is ID of the target vertex and $k$ denotes its degree. $v_i (i = 1, 2, ... k)$ denote IDs of vertices adjacent to $u$ and $c_i$ denotes the weight of a directed edge connecting $u$ and $v_i$ (from $u$ to $v_i$).

Output

For each vertex, print its ID and the distance separated by a space character in a line respectively. Print in order of vertex IDs.

Constraints

$1 \leq n \leq 100$

$0 \leq c_i \leq 100,000$

$|E| \leq 10,000$

All vertices are reachable from vertex $0$

Sample Input 1

5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3

Sample Output 1

0 0
1 2
2 2
3 1
4 3

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in Ruby (judge verdict: Accepted)


class Graph
  attr_accessor :n, :ver, :matrices
  def initialize(number_of_vertexes)
    @n = number_of_vertexes
    gets_adj_list
    create_vertexes
  end

  def dijkstra(n=0)
    init_all_vers_property
    set_root(n)
    loop do
      break unless u = next_mincost_vertex
      add_minimum_spanning_tree(u)
      update_mindists(parent: u)
    end
  end

  def puts_dists
    ver.each{|v| puts "#{v.id} #{v.weight}"}
  end

  private
    def init_all_vers_property
      ver.each {|v| v.init_color_and_dist }
    end

    def gets_adj_list
      v_c = []
      n.times do |i|
        _,_,*v_c[i] = gets.split.map(&:to_i)
      end
      @matrices = Array.new(n){Array.new(n,Float::INFINITY)}
      v_c.each_with_index do |vc,idx|
        vc.each_slice(2) do |v,c|
          matrices[idx][v] = c
        end
      end
    end

    def set_root(v)
      ver[v].weight = 0
      ver[v].parent = -1
    end

    def next_mincost_vertex
      mincost = Float::INFINITY
      u = nil
      ver.each do |v|
        if v.color != :black && v.weight < mincost
          mincost = v.weight
          u = v.id
        end
      end
      u
    end

    def add_minimum_spanning_tree(u)
      ver[u].color = :black
    end

    def update_mindists(parent:)
      matrices[parent].each_with_index do |cost,idx|
        if ver[idx].color != :black && cost + ver[parent].weight < ver[idx].weight
          ver[idx].weight = cost + ver[parent].weight
          ver[idx].parent = parent
          ver[idx].color = :gray
        end
      end
    end

    def create_vertexes
      @ver = []
      n.times {|i| ver << V.new(i)}
    end
end

class V
  attr_accessor :id, :color, :weight, :parent
  def initialize(id)
    @id = id
    init_color_and_dist
  end

  def init_color_and_dist
    @color = :white
    @weight = Float::INFINITY
  end
end

n = gets.to_i
graph = Graph.new(n)
graph.dijkstra
graph.puts_dists