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

Problem Statement

You are now participating in the Summer Training Camp for Programming Contests with your friend Jiro, who is an enthusiast of the ramen chain SIRO.
Since every SIRO restaurant has its own tasteful ramen, he wants to try them at as many different restaurants as possible in the night.
He doesn't have plenty of time tonight, however, because he has to get up early in the morning tomorrow to join a training session.
So he asked you to find the maximum number of different restaurants to which he would be able to go to eat ramen in the limited time.

There are $n$ railway stations in the city, which are numbered $1$ through $n$. The station $s$ is the nearest to the camp venue.
$m$ pairs of stations are directly connected by the railway: you can move between the stations $a_i$ and $b_i$ in $c_i$ minutes in the both directions.
Among the stations, there are $l$ stations where a SIRO restaurant is located nearby. There is at most one SIRO restaurant around each of the stations, and there are no restaurants near the station $s$.
It takes $e_i$ minutes for Jiro to eat ramen at the restaurant near the station $j_i$.

It takes only a negligibly short time to go back and forth between a station and its nearby SIRO restaurant.
You can also assume that Jiro doesn't have to wait for the ramen to be served in the restaurants.

Jiro is now at the station $s$ and have to come back to the station in $t$ minutes. How many different SIRO's can he taste?

Input

The input is a sequence of datasets. The number of the datasets does not exceed $100$. Each dataset is formatted as follows:

$n$ $m$ $l$ $s$ $t$
$a_1$ $b_1$ $c_1$
:
:
$a_m$ $b_m$ $c_m$
$j_1$ $e_1$
:
:
$j_l$ $e_l$

The first line of each dataset contains five integers:

$n$ for the number of stations,

$m$ for the number of directly connected pairs of stations,

$l$ for the number of SIRO restaurants,

$s$ for the starting-point station, and

$t$ for the time limit for Jiro.

Each of the following $m$ lines contains three integers:

$a_i$ and $b_i$ for the connected stations, and

$c_i$ for the time it takes to move between the two stations.

Each of the following $l$ lines contains two integers:

$j_i$ for the station where a SIRO restaurant is located, and

$e_i$ for the time it takes for Jiro to eat at the restaurant.

The end of the input is indicated by a line with five zeros, which is not included in the datasets.

The datasets satisfy the following constraints:

$2 \le n \le 300$

$1 \le m \le 5{,}000$

$1 \le l \le 16$

$1 \le s \le n$

$1 \le t \le 100{,}000$

$1 \le a_i, b_i \le n$

$1 \le c_i \le 1{,}000$

$1 \le j_i \le n$

$1 \le e_i \le 15$

$s \ne j_i$

$j_i$'s are distinct.

$a_i \ne b_i$

$(a_i, b_i) \ne (a_j, b_j)$ and $(a_i, b_i) \ne (b_j, a_j)$ for any $i \ne j$

Note that there may be some stations not reachable from the starting point $s$.

Output

For each data set, output the maximum number of different restaurants where Jiro can go within the time limit.

Sample Input

2 1 1 1 10
1 2 3
2 4
2 1 1 1 9
1 2 3
2 4
4 2 2 4 50
1 2 5
3 4 5
2 15
3 15
4 6 3 1 29
1 2 20
3 2 10
4 1 5
3 1 5
2 4 3
3 4 4
2 1
4 5
3 3
0 0 0 0 0

Output for the Sample Input

1
0
1
3

Solution in Ruby (judge verdict: Accepted)

class Heap
	require 'forwardable'
	extend Forwardable

	def initialize
		@array = []
		@index = {}
	end

	def add(obj, key)
		@array << [obj, key]
		@index[obj] = @array.size - 1
		lift_up(@array.size - 1)
	end

	def swap(i, j)
		u = @array[i]
		v = @array[j]
		@index[u[0]], @index[v[0]] = j, i
		@array[i], @array[j] = v, u
	end

	def lift_up(i)
		return if i == 0
		j = (i - 1) / 2
		if (@array[i][1] <=> @array[j][1]) < 0
			swap(i, j)
			lift_up(j)
		end
	end

	def del_min
		raise "heap is empty!" if empty?
		min = @array[0]
		if size > 1
			swap(0, @array.size-1)
			@array.pop
			lift_down(0)
		else
			@array.pop
		end
		@index.delete(min[0])
		min
	end

	def inc_key(obj, diff)
		i = @index[obj]
		@array[i][1] += diff
		lift_down(i)
	end

	def dec_key(obj, diff)
		i = @index[obj]
		@array[i][1] -= diff
		lift_up(i)
	end

	def update_key(obj, new_key)
		i = @index[obj]
		return unless i
		return if (new_key <=> @array[i][1]) >= 0
		@array[i][1] = new_key
		lift_up(i)
	end

	def min_obj
		@array[0][0]
	end

	def min_key
		@array[0][1]
	end

	def lift_down(i)
		j = 2 * i + 1
		k = j + 1
		if k >= size
			return if j >= size
			if (@array[i][1] <=> @array[j][1]) > 0
				swap(i, j)
				lift_down(j)
			end
		else
			l = ((@array[j][1] <=> @array[k][1]) < 0 ? j : k)
			if (@array[i][1] <=> @array[l][1]) > 0
				swap(i, l)
				lift_down(l)
			end
		end
	end

	def_delegators :@array, :empty?, :size
end

module Graphical
	require 'set'

	def get_path_by_bfs(s, t)
		prev_node = {}
		reached = Set[s]
		queue = [s]
		while !queue.empty? 
			u = queue.shift
			each_out_connection_of(u){|v, _|
				next if reached.include?(v)	
				reached << v
				queue << v
				prev_node[v] = u

				if v == t
					path = [v]
					while v = prev_node[v]
						path.unshift v
					end
					return path
				end
			}
		end	
		return nil
	end

	def get_path_by_dfs(s, t, reached = Set.new)
		reached << s
		return [s] if s == t
		each_out_connection_of(s){|v|
			next if reached.include?(v)
			path = get_path_by_dfs(v, t, reached) 
			return [s] + path if path
		}
		nil
	end

	def reachable_from(s, max_depth = nil)
		reached = Set[s]
		queue = [s]
		depth = 0
		loop {
			break if max_depth && depth >= max_depth
			next_queue = []
			until queue.empty?
				u = queue.shift
				each_out_connection_of(u){|v, _|
					next if reached.include?(v)
					reached << v
					next_queue << v
				}
			end
			break if next_queue.empty?
			queue = next_queue
			depth += 1
		}
		return reached
	end

	def bfs(start)
		raise "start must be Enumerable" if !start.kind_of?(Enumerable)
		reached = Set[*start.to_a]
		queue = []
		start.each{|s| queue << s}
		dist = {}
		loop.with_index {|_, depth|
			next_queue = []
			until queue.empty?
				u = queue.shift
				return depth if block_given? && yield(u, depth)
				dist[u] = depth
				each_out_connection_of(u){|v, _|
					next if reached.include?(v)
					reached << v
					next_queue << v
				}
			end
			break if next_queue.empty?
			queue = next_queue
		}
		block_given? ? nil : dist
	end

	def dijkstra(start)
		raise "start must be Enumerable" if !start.kind_of?(Enumerable)
		h = Heap.new
		reached = Set[*start.to_a]
		start.each{|s| h.add(s, 0)}

		dist = {}
		until h.empty?
			u, key = h.del_min
			return key if block_given? && yield(u, key)
			dist[u] = key
			each_out_connection_of(u){|v, l|
				d = dist[u] + l
				if reached.include?(v)
					h.update_key(v, d)
				else
					h.add(v, d)
					reached << v
				end
			}
		end
		block_given? ? nil : dist
	end

	def bidir_dijkstra(s, t)
		f_dist = {}
		b_dist = {}

		f_heap = Heap.new
		b_heap = Heap.new

		f_heap.add(s, 0)
		b_heap.add(t, 0)

		f_reached = Set[s]
		b_reached = Set[t]

		f_current_dist = b_current_dist = 0
		best = Float::INFINITY

		loop {
			if !f_heap.empty?
				u, key = f_heap.del_min
				f_dist[u] = key
				f_current_dist = key
				each_out_connection_of(u){|v, l|
					d = f_dist[u] + l
					if b_dist[v]
						path_len = d + b_dist[v]
						best = path_len if path_len < best
					end
					if f_reached.include?(v)
						f_heap.update_key(v, d)
					else
						f_heap.add(v, d)
						f_reached << v
					end
				}
			end

			if !b_heap.empty?
				u, key = b_heap.del_min
				b_dist[u] = key
				b_current_dist = key
				each_in_connection_of(u){|v, l|
					d = b_dist[u] + l
					if f_dist[v]
						path_len = d + f_dist[v]
						best = path_len if path_len < best
					end
					if b_reached.include?(v)
						b_heap.update_key(v, d)
					else
						b_heap.add(v, d)
						b_reached << v
					end
				}
			end

			return best if f_current_dist + b_current_dist >= best
			return nil if f_heap.empty? && b_heap.empty?
		}
	end

	def a_star(start)
		raise "a_star need termination condition" unless block_given?
		h = Heap.new
		reached = Set[start]
		h.add(start, [a_star_heuristic(start), 0])

		dist = {}
		until h.empty?
			u, (e, d) = h.del_min
			return d if yield(u, dist)
			dist[u] = d
			each_out_connection_of(u){|v, l|
				d = dist[u] + l
				e = d + a_star_heuristic(v)
				if reached.include?(v)
					h.update_key(v, [e, d])
				else
					h.add(v, [e, d])
					reached << v
				end
			}
		end
		block_given? ? nil : dist
	end

	def prim(start)
		h = Heap.new
		h.add(start, 0)
		reached = Set[start]
		tree_len = 0

		until h.empty?
			u, key = h.del_min
			tree_len += key

			each_out_connection_of(u){|v, d|
				if reached.include?(v)
					h.update_key(v, d)
				else
					h.add(v, d)
					reached << v
				end
			}
		end
		tree_len
	end

	def each_layer_from(start)
		raise "start must be Enumerable" if !start.kind_of?(Enumerable)
		return enum_for(__method__, start) unless block_given?
		reached = Set[*start]
		current_layer = start

		loop {
			yield current_layer
			next_layer = []
			current_layer.each{|u|
				each_out_connection_of(u){|v|
					next if reached.include?(v)
					reached << v
					next_layer << v
				}
			}	

			return if next_layer.empty?
			current_layer = next_layer
		}
	end
end

class Graph
	include Graphical

	def initialize(*options)
		@options = Set[*options]
	end

	def add_node(property = {})
		Node.new(property)
	end

	def add_edge(u, v, property = {})
		e = Edge.new(u, v, property)
		u.add_edge(e)
		v.add_edge(e)
		e
	end
	
	def del_edge(e)
		u, v = e.opposite.keys
		u.del_edge(e)
		v.del_edge(e)
		e
	end

	class Node
		def initialize(property)
			@property = property
			@edges = Set.new
		end

		def add_edge(e)
			@edges << e
		end

		def del_edge(e)
			@edges.delete(e)
		end

		def to_s
			@property[:name] || super
		end

		def each_edge
			@edges.each{|e| 
				yield e
			}
		end

		attr_reader :property
	end

	class Edge
		def initialize(u, v, property)
			@opposite = {u => v, v => u}
			@property = property
		end
		attr_reader :opposite, :property

		def to_s
			@property[:name] || super
		end

		def length
			@property[:length] || 1
		end

		def actual_capacity(from)
			v = @opposite[from]
			cap = @property[:capacity]
			flow = (@property[:flow] ||= 0)
			to = (@property[:flow_to] ||= v)
			to == v ? cap - flow : cap + flow
		end
	end

	def each_out_connection_of(u)
		u.each_edge{|e|
			yield(e.opposite[u], e.length)
		}
	end

	alias :each_in_connection_of :each_out_connection_of

	def get_augmenting_path(s, t)
		reached = Set[s]
		queue = [s]
		comming_edge = {}
		until queue.empty?
			u = queue.shift
			u.each_edge do |e|	
				v = e.opposite[u]
				next if reached.include?(v)

				next if e.actual_capacity(u) <= 0

				reached << v
				comming_edge[v] = e
				queue << v

				if v == t
					queue = []
					break
				end
			end	
		end

		if comming_edge[t]
			path = [t]
			v = t
			until path[0] == s
				e = comming_edge[v]
				u = e.opposite[v]
				path = [u, e, *path]
				v = u
			end
			return path
		end
		return nil
	end
end

loop do
	n, m, l, s, t = gets.split.map(&:to_i)
	break if n == 0

	g = Graph.new
	nodes = (0..n).map { g.add_node }
	m.times do
		a, b, c = gets.split.map(&:to_i)
		a = 0 if a == s
		b = 0 if b == s
		g.add_edge(nodes[a], nodes[b], {:length => c})
	end

	siros = [[0, 0]] + (1..l).map { gets.split.map(&:to_i) }
	dist = []
	siros.each do |j, e|
		dij = g.dijkstra([nodes[j]])	
		dist << siros.map {|i, f| f + (dij[nodes[i]] || Float::INFINITY)}
	end

	max_num = 0
	hash = { 0 => {0 => 0} }
	(1..2**l-1).each do |visited|
		(1..l).each do |last_idx|
			next if visited[last_idx-1] == 0
			min = Float::INFINITY
			hash[visited - (1 << last_idx-1)].each do |k, d|
				new_d = d + dist[k][last_idx]
				min = new_d if new_d < min
			end
			(hash[visited] ||= {})[last_idx] = min
			return_dist = min + dist[last_idx][0]
			if return_dist <= t
				c = visited.to_s(2).count(?1)
				max_num = c if c > max_num
			else
				hash[visited].delete(last_idx)
			end
		end
	end

	p max_num
end