- The building is a graph, rooms are vertices, hallways are edges connecting them. Therefore this is an undirected connected graph.
- You can solve this by getting the weight of graph's minimal spanning tree, then doing complete weight minus the weight of MST - the result is sum of weights of hallways that can be removed.
Yes, both of these are correct (modulo the nitpick that the building is not a graph with rooms as vertices and hallways as edges, but can be viewed as such). And if you view it thus, the difference between the total weight of the original graph and the total weight of a minimum spanning tree is the maximal possible reduction of weight without making some rooms unreachable from others (i.e. making the graph disconnected).
So I see two possibilities,
- You have a subtle bug in your implementation of Prim's algorithm that is triggered by a testcase on the grading server but not by the testcases you checked.
- The grading server has a wrong answer.
Without any further information, I would consider 1 more likely.
Is there any other way to solve this problem? I would happily try anything with the grading server.
Since you need to find the weight of an MST, I don't see how you could do it without finding an MST. So the other ways are different algorithms to find an MST. Kruskal's algorithm comes to mind.