Submit | All submissions | Best solutions | Back to list |
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
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 |
hide comments
|
||||||||||
2020-04-01 20:28:58
Dijkastra practice problem for beginners like me! |
||||||||||
2020-03-24 08:02:03
just remember that E is the exit cell number(index) not number of exit cells. |
||||||||||
2020-03-19 20:47:07
@sapjv Dijkstra's algorithm is meant to solve the Single Source shortest path problem, which essentially means that the source vertex is fixed and we want to find the shortest(minimum weight) path to all other vertices. Notice here that in this problem, the destination vertex is fixed (the exit cell) while the source vertex is different for each mouse. It would be computationally inefficient if we run a Dijkstra for n different source vertices. Hence a better approach would be to reverse the edges so that we can treat the destination vertex (as the source vertex) and see how many mice are able to reach their starting vertex within the given time limit. This can be done by reversing the edges and then applying Dijkstra treating the exit cell as the "source vertex". |
||||||||||
2019-11-25 07:26:19
Can someone explain why we are reversing the edges before implementing dijkstra ? I am not getting it. Please explain it with one simple example. Thanks for the help in advance ! |
||||||||||
2019-11-23 09:33:42
dijkstra with reverse edges :-) |
||||||||||
2019-11-13 16:43:19
Apply Floyd-Warshall and then loop if(d[i][e]<=t) cnt++; |
||||||||||
2019-08-16 18:35:53
Hint: reverse the edges is important! |
||||||||||
2019-05-13 06:01:21
AC in 1 go :D hint-floyd warshall |
||||||||||
2019-01-22 15:05:13
@vaishcr7 the shortest path from 2 to 1 is having length 3 in your test case. Thus the answer must be 2. Any of the shortest path algorithms are acceptable. One can solve it using bfs/dfs too :)) |
||||||||||
2018-12-02 21:20:23
SPOJ toolkit is a mess for this problem... Many wrong and even invalid testcases... However, with N <= 100 its easy to realize that any shortest path algorithm should get AC. (Dijkstra's, Bellman-Ford, Floyd-Warshall, ...) Floyd-Warshall is O(n^3) and had 0.00s of runtime here. |