Algorithm
problem link- https://www.spoj.com/problems/MICEMAZE/
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,…,N, where N is the total number of cells. You can assume that N≤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,…,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
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <map>
#include <list>
using namespace std;
int main(){
int n,e,t,m;
scanf("%d%d%d%d",&n,&e,&t,&m);
long dp[n][n];
memset(dp,100100,sizeof(dp[0][0])*n*n);
while(m--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
dp[a-1][b-1]=c;
}
for(int i=0;i<n;i++){
dp[i][i]=0;
}
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(dp[i][j]>dp[i][k]+dp[k][j]){
dp[i][j]=dp[i][k]+dp[k][j];
}
}
}
}
/*for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
int count=0;
for(int i=0;i<n;i++){
if(dp[i][e-1]<=t)
count++;
}
printf("%d\n",count);
return 0;
}
Copy The Code &
Try With Live Editor
Input
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
Demonstration
SPOJ Solution-Mice and Maze-Solution in C, C++, Java, Python