TopCoder problem "FreeGuitars" used in TC China 08 - 1D (Division I Level Three)



Problem Statement

    

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.

 

Definition

    
Class:FreeGuitars
Method:minimumCosts
Parameters:int, String[]
Returns:int
Method signature:int minimumCosts(int N, String[] trainRoutes)
(be sure your method is public)
    
 

Constraints

-N will be between 2 and 50, inclusive.
-trainRoutes will contain between 1 and 50 elements, inclusive.
-Each element of trainRoutes will be formatted "STORE1 STORE2 TICKET" (quotes for clarity).
-Each STORE1 and STORE2 will be integers between 1 and N, inclusive, with no leading zeroes.
-Each TICKET will be an integer between 0 and 100, inclusive, with no extra leading zeroes.
-All elements of trainRoutes will describe different routes (so there will no more then 1 train route between each pair of stores).
-No elements of trainRoutes will describe a route from a store to itself, so STORE1 never equals STORE2.
 

Examples

0)
    
3
{"1 2 6", "1 3 4", "2 3 1"}
Returns: 5
Here we take the train from 1 to 3, pick up the guitar at 3, then go from 3 to 2, pick up the guitar, and go back to 3 and 2 (you already paid for those trips) to 1 (where you can pick up the last guitar).
1)
    
3
{"1 3 56"}
Returns: -1
Whoops, we can not reach store 2!
2)
    
5
{"1 2 88",
"1 3 37",
"1 4 73",
"1 5 58",
"2 3 59",
"2 4 30",
"2 5 98",
"3 4 28",
"3 5 85",
"4 5 82"}
Returns: 153
3)
    
15
{"12 2 90", "14 4 11", "6 4 18", "5 8 35", "7 13 54", "11 2 33", "12 5 52",
 "13 2 98", "10 3 3", "4 7 63", "15 11 46", "11 7 4", "11 6 24", "9 7 30",
 "13 12 19", "5 10 82", "9 1 94", "13 3 30", "11 5 12", "10 1 10", "6 9 74",
 "12 8 55", "4 11 3", "12 4 71", "9 10 90"}
Returns: 306

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7783

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13676&pm=7783

Writer:

jthread

Testers:

PabloGilberto , Olexiy , Andrew_Lazarev

Problem categories:

Graph Theory