1845. 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.

 

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.

 

Sample input

Sample output

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

3

 

 

 

РЕШЕНИЕ

алгоритм Дейкстры

 

Анализ алгоритма

Задан ориентированный взвешенный граф из n вершин. В каждой вершине находится мышка. В нулевой момент времени все мыши начинают бежать на выход – в вершину e. Требуется узнать, сколько мышей прибежит к выходу в момент времени t.

Запустим алгоритм Дейкстры из вершины e по обратным ребрам. Таким образом для каждой мыши мы узнаем наименьшее время, за которое она сможет спастись. Подсчитываем количество мышей, для которых время спасения не больше t.

 

Реализация алгоритма

 

#include <stdio.h>

#include <string.h>

#define MAX 110

#define INF 0x3F3F3F3F

 

int i, j, min, n, e, t, m;

int from, to, dist, w, res;

int mas[MAX][MAX], used[MAX], d[MAX], parent[MAX];

 

void Relax(int w, int j)

{

  if (d[w] + mas[w][j] < d[j])

    d[j] = d[w] + mas[w][j];

}

 

int main(void)

{

  scanf("%d %d %d %d",&n,&e,&t,&m);

 

  memset(mas,0x3F,sizeof(mas));

  memset(used,0,sizeof(used));

  for(i = 1; i <= m; i++)

  {

    scanf("%d %d %d",&from,&to,&dist);

    mas[to][from] = dist; // reverse

  }

 

  memset(d,0x3F,sizeof(d));

  d[e] = 0; // start from end

 

  for(i = 1; i < n; i++)

  {

    min = INF;

    for(j = 1; j <= n; j++)

      if (!used[j] && d[j] < min) {min = d[j]; w = j;}

    for(j = 1; j <= n; j++)

      if (!used[j]) Relax(w,j);

    used[w] = 1;

  }

 

  res = 0;

  for(i = 1; i <= n; i++)

    if (d[i] <= t) res++;

  printf("%d\n",res);

 

  return 0;

}