MICEMAZE - Mice and Maze


A set of laboratory mice is being trained to escape a maze. The maze is made up of cells, and each cell is connected to some other cells. However, there are obstacles in the passage between cells and therefore there is a time penalty to overcome the passage Also, some passages allow mice to go one-way, but not the other way round.

Suppose that all mice are now trained and, when placed in an arbitrary cell in the maze, take a path that leads them to the exit cell in minimum time.

We are going to conduct the following experiment: a mouse is placed in each cell of the maze and a count-down timer is started. When the timer stops we count the number of mice out of the maze.

Problem

Write a program that, given a description of the maze and the time limit, predicts the number of mice that will exit the maze. Assume that there are no bottlenecks is the maze, i.e. that all cells have room for an arbitrary number of mice.

Input

The maze cells are numbered $1, 2, \ldots, N$, where $N$ is the total number of cells. You can assume that $N \le 100$.

The first three input lines contain $N$, the number of cells in the maze, $E$, the number of the exit cell, and the starting value $T$ for the count-down timer (in some arbitrary time unit).

The fourth line contains the number $M$ of connections in the maze, and is followed by $M$ lines, each specifying a connection with three integer numbers: two cell numbers $a$ and $b$ (in the range $1, \ldots, N$) and the number of time units it takes to travel from $a$ to $b$.

Notice that each connection is one-way, i.e., the mice can't travel from $b$ to $a$ unless there is another line specifying that passage. Notice also that the time required to travel in each direction might be different.

Output

The output consists of a single line with the number of mice that reached the exit cell $E$ in at most $T$ time units.

Example

Input:
4 
2 
1
8
1 2 1
1 3 1
2 1 1
2 4 1
3 1 1
3 4 1
4 2 1
4 3 1

Output:
3

hide comments
naruto09: 2015-10-04 21:14:33

any tricky test case..??

Shashank Tiwari: 2015-09-07 15:02:06

This problem statement have many gaps which i try to fill up ....

1. All values are in int
2. Graph can be disconnected too ...
3. No self loops or parallel edges
4. Graph is directed
5. Final ans also in int

Last edit: 2015-09-07 15:02:47
tarunsai: 2015-06-28 04:57:13

which cells are exit cells

aqua: 2015-06-19 01:54:05

simple bruteforce !!

Kunwar Sachin Singh: 2015-05-26 08:52:59

can be solved by using dijkstra

GAURAV CHANDEL: 2015-04-14 15:33:43

good problem to practice floyd warshall algorithm....

sai krishna: 2015-03-07 14:53:12

simpe floyd warshall

Rishav Jain: 2015-02-21 19:13:42

images are nt opening:(
at most wht time & which cell r exit cell?


(Francky) => Image now visible. Please use full words in comments.

Last edit: 2015-02-21 20:58:00
Prasanth: 2015-02-01 06:01:24

Images links in the input description are broken. Please update it

S: 2015-01-11 15:08:06

got it accepted in first Attempt ..


Added by:overwise
Date:2007-10-04
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ACM ICPC -- SWERC 2001