Lightoj 1123 - Trail Maintenance(最小增量生成树)
題目鏈接 https://vjudge.net/problem/LightOJ-1123
Tigers in the Sunderbans wish to travel freely among the N fields (numbered from 1 to N), even though they are separated by trees. The tigers wish to maintain trails between pairs of fields so that they can travel from any field to any other field using the maintained trails. Tigers may travel along a maintained trail in either direction.
The tigers do not build trails. Instead, they maintain deer trails that they have discovered. On any week, they can choose to maintain any or all of the deer animal trails they know about. Always curious, the tigers discover one new deer trail at the beginning of each week. They must then decide the set of trails to maintain for that week so that they can travel from any field to any other field. Tigers can only use trails which they are currently maintaining.
The tigers always want to minimize the total length of trail they must maintain. The tigers can choose to maintain any subset of the deer trails they know about, regardless of which trails were maintained the previous week. Deer trails (even when maintained) are never straight. Two trails that connect the same two fields might have different lengths. While two trails might cross, tigers are so focused; they refuse to switch trails except when they are in a field. At the beginning of each week, the tigers will describe the deer trail they discovered. Your program must then output the minimum total length of trail the tigers must maintain that week so that they can travel from any field to any other field, if there is such a set of trails.
Input
Input starts with an integer T (≤ 25), denoting the number of test cases.
Each case starts with two integers N (1 ≤ N ≤ 200) and W. W is the number of weeks the program will cover (1 ≤ W ≤ 6000).
Each of the next W lines will contain three integers describing the trail the tigers found that week. The first two numbers denote the end points (filed numbers) and the third number denotes the length of the trail (1 to 10000). No trail has the same field as both of its end points.
Output
For each case, print the case number in a line. Then for every week, output a single line with the minimum total length of trail the tigers must maintain so that they can travel from any field to any other field. If no set of trails allows the tigers to travel from any field to any other field, output “-1”.
Sample Input
1
4 6
1 2 10
1 3 8
3 2 3
1 4 3
1 3 6
2 1 2
Sample Output
Case 1:
-1
-1
-1
14
12
8
【題意】
從包含n個點的空圖開始,依次加入m條帶權無向邊,每加入一條邊就立即輸出當前圖中最小生成樹的權值,如果還未連通則輸出-1.
【思路】
題目描述即最小增量生成樹的定義,如果在每次加邊后都調用一次kruscal算法求解,那么復雜度是O(m^2logn),難以承受。正確的做法是根據生成樹的回路性質刪除一些邊,回路性質即圖中任意一條回路上邊權最大的邊一定不在最小生成樹當中,所以我們還是仿照kruscal算法的過程,從空圖開始不斷加邊,假設加入若干條邊以后求出了當前的最小生成樹,那么下一次加邊后一定會構成一個環,我們只需要把這個環上邊權最大的邊刪除就會得到新的最小生成樹,怎么去找邊權最大的那條邊呢?其實根本不用找,因為在調用kruscal算法之前我們肯定會對邊集按照邊權升序排序的,當新加入的這條邊構成環路時,這條邊肯定是環路中權值最大的邊,也是我們要刪掉的邊,因為每次操作最多刪一條邊,所以直接用最后一條邊覆蓋掉當前邊即可,下一次調用前的sort會重新對邊集排序。
轉載于:https://www.cnblogs.com/wafish/p/10465410.html
總結
以上是生活随笔為你收集整理的Lightoj 1123 - Trail Maintenance(最小增量生成树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下sort命令使用详解---l
- 下一篇: itchat 动态注册