#C1285. Intra-Galactic Transport

    ID: 42322 Type: Default 1000ms 256MiB

Intra-Galactic Transport

Intra-Galactic Transport

You are tasked with optimizing interplanetary travel by finding the shortest travel cost between two given planets. The galaxy is modeled as a weighted undirected graph where nodes represent planets and edges represent bidirectional travel routes with associated costs. The objective is to compute the minimum cost path from a start planet to a destination planet using Dijkstra's algorithm. If the destination is unreachable, output UNREACHABLE.

The cost function is based on the sum of the weights along the path. Mathematically, if a path from planet \(S\) to \(T\) is represented as \(p = \{S = v_0, v_1, \dots, v_k = T\}\), the travel cost is given by:

\[ \text{cost}(p) = \sum_{i=0}^{k-1} w(v_i, v_{i+1}) \]

Your solution should read input from standard input (stdin) and output the results on standard output (stdout).

inputFormat

The input consists of multiple datasets. Each dataset is defined as follows:

  • The first line contains two integers \(N\) and \(M\), where \(N\) is the number of planets (nodes) and \(M\) is the number of travel routes (edges).
  • The next \(M\) lines each contain three integers \(U\), \(V\), \(W\), representing a bidirectional route between planets \(U\) and \(V\) with travel cost \(W\).
  • The following line contains two integers \(S\) and \(T\), representing the starting planet and the destination planet respectively.
  • A line containing -1 indicates the end of input.

All input should be read from stdin.

outputFormat

For each dataset, output a single line containing the shortest travel cost from planet \(S\) to \(T\). If there is no path from \(S\) to \(T\), output UNREACHABLE. All output should be written to stdout.

## sample
5 6
1 2 10
1 3 5
2 3 2
3 4 2
2 4 5
4 5 1
1 5
3 2
1 2 3
2 3 4
1 3
6 3
1 2 7
1 3 9
4 5 6
1 4
-1
8

7 UNREACHABLE

</p>