Difficulty: 7 - Uses Recursion
Saving Electricity
MIT is being shut down for Thanksgiving weekend, so there's no point in
keeping all the lights on. You alone have been given the job of walking
around the Institute and turning off the lights. (MIT's budget deficit prevents them from
hiring
additional people to do this job.) While you're turning off lights in one
building, the lights in other buildings stay on and waste electricity. If you start at the
main
institute, you can turn off a lot of lights quickly, and then proceed to
buildings such as the Supercomputing Facility which are further away. Because of the
deficit, it's
important to plan out the best route which will minimize the total amount of
electricity that gets wasted. (If you don't save enough money, they might decide the leave
the lights on next year, and then you'd be out of a job!)
You plan to write a computer program which will help you pick the best
route. The program will take as input a graph whose nodes represent the
buildings. Each node has a weight which gives the number of kilowatts that are burned when
the lights are on, and the edges in the graph have weights which give the
amount of time it takes to get from one building to another. The program is to pick a
start
vertex, and a tour of the buildings which will minimize the number of
kilowatt-hours that get burned.
You may assume that there are at most eight buildings. The input will start
with a line containing a string of building codes. Each building has a one
letter code which is not white-space, so the string will have size between one and eight
inclusive. Following the code string, the amount of kilowatts burned in each
building will be given Then there will be a blank line and a 2D array giving the time (in
minutes)
it takes to get from one building to another. The entries in this array will
be nonnegative, and symmetric. The time from a building to itself will be 0, but a time of
0
between two different buildings means that there is no direct route between
them. In this case there will however be an indirect route with other buildings on the
way, so
it is always possible to get from any building to any other building. All
numbers are real numbers. You may assume that the input given will be valid.
For the output you are to report as shown below the optimal route, the time
it takes to reach each building (in minutes), and the total number of
kilowatt hours that are burned. If the route visits a building more than once, then only
give
the time of the first arrival. For example, the input
2746GB
3.4
2.0
2.8
2.8
6
0.01
0 0 1 1.3 0 10
0 0 3 0 0 0
1 3 0 1 0 0
1.3 0 1 0 2 0
0 0 0 2 0 0
10 0 0 0 0 0
should yield the output
Bld G Bld 6 Bld 2 Bld 4 Bld 7 Bld 4 Bld 2 Bld B
0.00 2.00 3.30 4.30 7.30 21.30
Total kilowatt-hours: 0.7278833