You are a guitar player and because you are really good, several music stores are giving you guitars for free! Unfortunately, you will have to travel to all the music stores to pick up your guitars. Because you don't have a drivers license and it's too far to go by bike, you decide to travel by train. But before you go, you first write a program to determine the minimum amount of money you have to spend to get to all the music stores by train.
You will be given an int N, the number of music stores there are (so also the numbers of guitars you get!), and a String[] trainRoutes, containing a list of train routes between the music stores. Each element of trainRoutes will be in the form of:
"STORE1 STORE2 TICKET" (quotes for clarity only)
STORE1 and STORE2 will be integers between 1 and N, inclusive, and TICKET will be the price for a round trip ticket from STORE1 to STORE2 and back. There will no more then 1 train route between each pair of stores, and there will not be a train route from a store to itself.
A round trip ticket is a ticket that allows you to travel the route in both directions exactly once. So buying a ticket between 3 and 5 means that you can travel from 3 to 5 one time, and from 5 to 3 one time. The 2 trips do not necessarily have to be in that order or directly after each other.
Return an int containing the minimum amount of money you will need to spend on train tickets to pick up all your free guitars. If it is not possible to pick up all N guitars, return -1. Initially, you are at store 1, and you want to return there after you picked up all guitars.
|