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
ajayc1007: 2018-06-22 20:39:23

Remember, at each cell, there is a mice !

ankit1cool: 2018-06-17 21:28:39

Just reverse the edges and apply dijkstra's

m2do: 2018-05-13 10:56:09

The mouse is kept in the E cell as well. Hence answer will always be > 1. Use Dijkstra and Floyd-Warshall <3

ameyanator: 2018-04-11 23:29:14

I think i too made a blunder mistake while solving it with dijkstra. Floyd Warshal is <3

prestonbui97: 2018-04-08 06:08:27

AC in one go !!!!!!

ramini1996: 2018-02-02 12:35:17

Just use Floyd Warshall algorithm !!!

ayushtewary07: 2018-02-01 19:58:36

AC in one go!!!
used dijsktra only

Last edit: 2018-02-01 19:59:21
chetan4060: 2017-12-12 10:29:48

dijkstra's rocks:-)

thanhbinh84: 2017-12-07 03:11:01

Don't be fool by ideal sample TC. >_<
5
1
4
7
1 3 3
2 1 4
4 1 4
3 1 3
2 4 4
5 3 2
5 1 6

vishesh197: 2017-11-04 13:25:38

you can do it in two ways first is do bfs from all vertices to exit cell adding the time(cost) and checking if time is less
or equal to T countdown time and other way is to go from exit cell to all vertices.AC in 2nd go....


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