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

Single Source Shortest Path II

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 10,000$

$0 \leq c_i \leq 100,000$

$|E| < 500,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: Time Limit Exceeded)



class PriorityQueue
	def initialize
		@node = []
		@n = 0
	end

	private

	def left_key key
		2*key+1
	end

	def right_key key
		2*key+2
	end

	def from_root_swap key, 
		right = 2*key+2
		left = 2*key+1
		if  key < 0 
			return nil
		end
		if !@node[left] && !@node[right]
			return nil
		elsif @node[left] && !@node[right]
			if @node[left].cost < @node[key].cost
				@node[key], @node[left] =  @node[left], @node[key]
				from_root_swap(left)
			end
		elsif @node[right] && !@node[left]
			if @node[right].cost < @node[key].cost
				@node[key], @node[right] =  @node[right], @node[key]
				from_root_swap(right)
			end
		elsif @node[key].cost >= @node[left].cost && @node[left].cost <= @node[right].cost
			@node[key], @node[left] =  @node[left], @node[key]
			from_root_swap(left)
		elsif @node[key].cost >= @node[right].cost && @node[right].cost <= @node[left].cost
			@node[key], @node[right] =  @node[right], @node[key]
			from_root_swap(right)
		end
	end

	def from_leaf_swap key
		if key == 0
			return nil
		end
		parent_key = (key-1)/2
		if @node[parent_key].cost > @node[key].cost
			@node[key], @node[parent_key] = @node[parent_key], @node[key]
			from_leaf_swap(parent_key)
		end
	end

	def extract!
		node = @node[0]
		if @node.size == 0
			railse "Queue is Empty"
		elsif @node.size == 1
			@n -= 1
			return @node.pop
		else
		@n -= 1
		node = @node[0]
		@node[0] = @node.pop
		from_root_swap(0)
		return node
		end
	end

	def insert! node
		@node << node
		@n += 1
		from_leaf_swap(@n - 1)
	end



	public

	def << node
		insert!(node)
	end

	def pop
		extract!
	end

	def empty?
		@node.empty?
	end

end



n = gets.to_i
m = Array.new(n){Hash.new}
n.times do |i|
	s = gets.split.map(&:to_i)
	s[1].times do |j|
		m[s[0]][s[ 2 * j + 2]] = s[2 * j + 3]
	end
end

Vector = Struct.new(:sta, :end, :cost)
pq = PriorityQueue.new
BLACK = 0
WHITE = 1
color = Array.new(n){WHITE}
cost = Array.new(n){500_001}
cost[0] = 0
color[0] = BLACK
u = 0
loop{

	m[u].to_a.each do |mm|
		pq << Vector.new(u, mm[0], cost[u] + mm[1] ) if color[mm[0]] == WHITE 
	end

	break if pq.empty? 
	vector = pq.pop
	break if vector.cost == 500_001

	color[vector.end] = BLACK
	 cost[vector.end] = vector.cost if cost[vector.end] > vector.cost
	 u = vector.end
}

n.times do |i|
	puts "#{i} #{cost[i]}"
end