#K43747. Minimum Signal Travel Time

    ID: 27378 Type: Default 1000ms 256MiB

Minimum Signal Travel Time

Minimum Signal Travel Time

You are given a directed weighted graph representing a series of gears and the connections between them. Each connection (or edge) from gear u to gear v has an associated travel time w that indicates the time needed for a signal to move from one gear to another.

Your task is to determine the minimum time required for the signal to travel from gear $1$ to gear $n$. If no valid path exists between these gears, output "Timeless machinery".

This problem can be efficiently solved using Dijkstra's algorithm. In this algorithm, each gear is represented as a node, and each connection as a directed edge with an associated weight.

inputFormat

The first line contains two space-separated integers $n$ and $m$, representing the number of gears and connections respectively.

Then, $m$ lines follow. Each line contains three space-separated integers $u$, $v$, $w$, where:

  • $u$ is the starting gear,
  • $v$ is the destination gear, and
  • $w$ is the signal travel time between them.

outputFormat

If there is a path from gear $1$ to gear $n$, output a single integer representing the minimum travel time. Otherwise, output the string "Timeless machinery".

## sample
4 4
1 2 3
2 4 5
1 3 1
3 4 4
5