TORONTO KIDS COMPUTER CLUB | Aurora Saturday 14:30 Python Practice 22.06.04.
20706

# Aurora Saturday 14:30 Python Practice 22.06.04.

## 09 Jun Aurora Saturday 14:30 Python Practice 22.06.04.

Question:

When Jack’s friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a non-zero length smaller than 35,000.

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.

He wants his tour to be as short as possible, however he doesn’t want to walk on any given path more than once. Calculate the shortest tour possible. Jack is sure that some tour exists for any given farm.

Input
* Line 1: Two space-separated integers: N and M.

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path’s length.

Output
A single line containing the length of the shortest tour.

`Sample Input4 51 2 12 3 13 4 11 3 22 4 2Sample Output6`
` `