Блог пользователя noobinho

Автор noobinho, история, 8 лет назад, По-английски

I'm trying to do this problem: http://acm.timus.ru/problem.aspx?space=1&num=1416. It consists in a simple, undirected and weighted graph for which is demanded to calculate the second minimum total cost: a cost of a configuration (different than the minimum-spanning-tree) which connects all vertex and has the smallest cost not less than the cost given by the minimum-spanning-tree. If there isn't such a configuration it's demanded to indicate that. I did the following code: https://ideone.com/e.js/y4CcBz. It generates the minimum-spanning-tree with Kruskal and tries to eliminate the edges of this tree one by one from the bigger to the smallest. For each edge eliminated, it tries to connect the edges that weren't in the minimum-spanning-tree, one by one, from the bigger to the smallest, and verifies with a DFS if the resulting graph has only one connected component. If so, it stops. Else, it reconnects the edge of the minimum-spanning-tree and goes to the next iteration. Since my approach gives wrong answer, I want to know what is wrong with it and what is the correct approach. Thanks in advance.

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
8 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

How to find 2nd Minimal Spanning Tree? At first, let's build MST. Consider all edges that doesn't belong to MST. We can try to add them independently to MST. YEA, we've got a cycle. We can erase any edge in a cycle and rest edges will be Spanning Tree. Obviously, it's good idea to erase edge with maximal weight (and this edge mustn't be our added edge, off course:) ). Then relax our answer.

Code: https://pastebin.com/TraXQSY0

»
8 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

By the way, there is, perhaps a simpler solution: find a mst, then for each edge from the mst to find a mst in the graph without it, the answer is the minimum of the resulting mst.

upd: I did not quite understand the constraints in the problem, so maybe this solution is too slow

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

about your solution: on the test "2 2 1 1 1 2 2 2" it displays "0, -1", is this normal?