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.


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.


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.


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


1 2 1
1 3 1
2 1 1
2 4 1
3 1 1
3 4 1
4 2 1
4 3 1


hide comments
praney_rai: 2017-05-31 09:35:51

:D blunder mistake cost me 5 WA :/

ajeetk_973: 2017-04-01 20:51:46

easy one with dijkstra's, AC in one go(-_-)

nilabja16180: 2017-03-14 12:48:38

@vladamir, dijsktra's algo is itself bfs with relaxation!

Last edit: 2017-03-14 12:53:36
vladimira: 2017-03-02 12:22:39

Didn't use dijkstra. Reversed connections and counted cells that can be reached starting from exit cell in time less then T. If I could reach cell in less time than already did earlier added cell to deque and changed time[cell]. Just bfs with a little additions.

Last edit: 2017-03-02 12:23:39
vengatesh15: 2016-11-12 18:30:42

Easy one with dijkstra's :-)

Sahil: 2016-11-10 17:29:10

Flloyd Warshall works well here.

bartol3141: 2016-09-24 14:50:16

Nice dijkstra problem :D

atul_agarwal: 2016-08-25 19:33:13

any one please help me about constraints..??

xinnix: 2016-07-05 17:59:28

No one ever told me that I can't start a sized array from 1. Costed me 8 runtime error... :( ;(

Last edit: 2016-07-06 09:10:42
Shubhransh Srivastav: 2016-06-24 06:03:47

need to clear the vector first... cost me 4 wa!!!
thanks ajay_5097 :)

Added by:overwise
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