### Wiring the house problem - Kruskalâ€™s algorithm

You have decided to build a new house, but you are hoping to save costs by doing some of the work yourself. Specifically, you think you are able to do the electrical wiring. The wiring works by using different types of junction points and running electrical wires between these junction points. Complicating things slightly, there are several types of junction points:

Â

â€˘ Breaker Box: This is a special node in from which the electricity is sourced. There will only be one breaker box that supplies electricity to your entire house.

â€˘ Switch: A switch is a special node that can affect the current moving along a path from the breaker box, and can be turned off to not allow current to continue past the switch onward.

â€˘ Light: A light is a node that is controlled by a switch.

Thus, there MUST be exactly one switch between the light and the breaker box. Each light has a specific switch you want to control it with, and thus that particular switch must be the one between the light and breaker box.

â€˘ Electrical Outlet: You want your outlets to be ac- tive all of the time. Thus,Â  anÂ  outletÂ  mustÂ  haveÂ  a path directly to the breaker box with NO switches in between.

â€˘ Junction Box: A junction box is simply a location that can connect multiply wires together. For exam- ple, one wire from the breaker can go into a junction box, and three wires can fan out of the box to dis- tribute electricity to the house.

Â

Given the layout of the different junction points and the cost of wiring them to each other, can you figure out the cheapest way to wire your house? Consider the following when implementing your solution:

Â

â€˘ Your solution MUST use Kruskalâ€™s algorithm from class. The purpose of this assignment is to practice with this algorithm, though you may need to adjust the strategy very slightly.

â€˘ You must implement your OWN custom find-union data structure for this assignment. Though you can pass the test cases with other approaches, a human will sight check this when awarding you your â€ťpdf pointâ€ť.

â€˘ Your wired house must be a spanning tree (there can be no cycles)

â€˘ Lights can be wired to one another if they have the same desired switch, as long as the switch is between both of them and the breaker.

Â

â€˘ Every type of junction point (breaker, switch, boxes, etc.) must be connected into your wiring.

â€˘ junction boxes (and outlets) should never be behind switches in your spanning tree. Thus, once your spanning tree reaches a switch, only lights (though many of them potentially) will be behind the switch.

Â

Input

Â

The input file will begin with one line containing integers J and C, the number of junction points andÂ  Â  Â  Â the number of possible connections respectively. The next J lines will each specify the name of a junction point along with the type (breaker, switch, light, outlet, box).Â  When a switch is listed, the lights that need to be behind that switch will be always listed next to indicate this dependency. There will only be one breaker box. The next C lines specify the connections by providing the name of two junction points and the cost between them.

Â

Output

Â

Output the cost of the minimum wiring for the house that adheres to all of the constraints

Â

Sample Input

Â

6 8

b1 breakerÂ

j1 box

s1 switchÂ

l1 lightÂ

l2 lightÂ

o1 outletÂ

b1 j1 5

b1Â  o1 1

j1Â  s1 1

j1Â  o1 2

o1Â  l1 1

l1Â  l2 2

s1Â  l1 6

s1Â  l2 1

Â

Â

Sample Output

7

Â

### Need a custom answer at your budget?

This assignment has been answered 6 times in private sessions.