An airline company has flights to all major cities in some country. From any major city it is
possible to reach any other major city either by a direct flight, or by two or more flights.
The company has provided a matrix containing the cheapest cost when taking a trip between any pair of
major cities. When several flights are needed for a trip, the cost for that trip is the sum
of the costs of the direct flights. The cost for a direct flight is a positive integer.
Given this matrix, you want to find the least possible total cost of all direct flights. You
can assume that if a flight exists from A to B, another flight with the same cost exists from
B to A (and you should only count one of these direct flights), and that no other flights between
the same pair of cities exist with a different cost.
Create a class CostMatrix containing the method leastTotalCost which takes as input a String[]
matrix, describing the cost matrix as a symmetric table. Each row i (0-indexed) will contain
a single space separated list of integers, where the jth integer (0-indexed) in row i is the
cheapest cost to take a trip between cities i and j. The diagonal entries will all be 0.
The method should return an int, the least possible total cost for all direct flights.
If the cost matrix is inconsistent, return -1.
|