 <blockquote> <ol> <li>The building is a graph, rooms are vertices, hallways are edges connecting them. Therefore this is an undirected connected graph.</li> <li>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.</li> </ol> </blockquote> <p>Yes, both of these are correct (modulo the nitpick that the building <em>is</em> 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).</p> <p>So I see two possibilities,</p> <ol> <li>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.</li> <li>The grading server has a wrong answer.</li> </ol> <p>Without any further information, I would consider 1 more likely.</p> <blockquote> <p>Is there any other way to solve this problem? I would happily try anything with the grading server.</p> </blockquote> <p>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. <a href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm" rel="nofollow">Kruskal's algorithm</a> comes to mind.</p>