题目链接 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会重新对边集排序。

#include<bits/stdc++.h>
using namespace std;const int maxn = 220;
const int maxm = 6050;int n, m, cnt;
int par[maxn];struct Edge {int from, to, dist;Edge(int f = 0, int t = 0, int d = 0) :from(f), to(t), dist(d) {}
}edges[maxm];bool cmp(Edge x, Edge y) { return x.dist < y.dist; }
int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }int kruscal() {int cpy = cnt, num = 0, ans = 0;for (int i = 0; i <= n; ++i) par[i] = i;for (int i = 0; i < cpy; ++i) {int x = find(edges[i].from);int y = find(edges[i].to);if (x == y) {edges[i] = edges[cnt - 1];--cnt;continue;}++num;par[x] = y;ans += edges[i].dist;}return num < n - 1 ? -1 : ans;
}int main() {int t;scanf("%d", &t);for (int kase = 1; kase <= t; ++kase) {scanf("%d%d", &n, &m);printf("Case %d:\n", kase);cnt = 0;for (int i = 0; i < m; ++i) {int u, v, c;scanf("%d%d%d", &u, &v, &c);edges[cnt++] = Edge(u, v, c);sort(edges, edges + cnt, cmp);int ans = kruscal();printf("%d\n", ans);}}return 0;
}

转载于:https://www.cnblogs.com/wafish/p/10465410.html

Lightoj 1123 - Trail Maintenance(最小增量生成树)相关推荐

  1. java算法----排序----(6)希尔排序(最小增量排序)

    1 package log; 2 3 public class Test4 { 4 5 /** 6 * java算法---希尔排序(最小增量排序) 7 * 8 * @param args 9 */ 1 ...

  2. python【力扣LeetCode算法题库】945- 使数组唯一的最小增量

    使数组唯一的最小增量 给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1. 返回使 A 中的每个值都是唯一的最少操作次数. 示例 1: 输入:[1,2,2] 输出:1 解释: ...

  3. 最小代价生成树Prim/Kruskal(c/c++)

    常用的求最小代价生成树的方法有两种:Prim算法和Kruskal算法,这两种算法都是贪心算法的应用, Prim算法 在一个无向带权图中求得最小生成树得思路是:从a顶点出发,将a放入u集合(表示已选), ...

  4. POJ3522Slim Span(最大边与最小边差值最小的生成树)

    感谢这篇文章 本文对其代码,进行一些解释. 这道题的题意很明了.求最大边与最小边差值最小的生成树 首先,把所有的生成树都求出来是不可能的,所以,必须用别的方法. 在学习次小生成树的过程中,知道了一个最 ...

  5. HDU5697 刷题计划 dp+最小乘积生成树

    分析:就是不断递归寻找靠近边界的最优解 学习博客(必须先看这个): 1:http://www.cnblogs.com/autsky-jadek/p/3959446.html 2:http://blog ...

  6. TZOJ 5471: 数据结构实验--图的最小代价生成树

    题目描述 求带权无向图的最小代价生成树. 输入 输入数据为多组,每组数据包含多行,第一行为2个整数n,e,n为图的顶点数,e为边数,接下来是e行,每行3个整数,前两个整数是一个顶点对,代表一条边所依附 ...

  7. 图论 —— 生成树 —— 最小瓶颈生成树

    [概述] 所谓瓶颈生成树,即对于图 G 中的生成树树上最大的边权值在所有生成树中最小. 对于无向图来说,无向图的最小生成树一定是最小瓶颈生成树,但最小瓶颈生成树不一定是最小生成树. 因此,使用 Kru ...

  8. LeetCode 945. 使数组唯一的最小增量

    945. 使数组唯一的最小增量 思路:预留多一点空间给它:用哈希表计算有多少个相同的值 class Solution { public:int minIncrementForUnique(vector ...

  9. 图的绝对中心(bzoj 2180: 最小直径生成树)

    2180: 最小直径生成树 Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 219  Solved: 105 [Submit][Status][Dis ...

最新文章

  1. JavaScript 定义类时如何将方法提取出来
  2. 查看服务器配置信息prtdiag与systeminfo实用命令
  3. 前戴尔EMC中国研究院院长创业:推出AI加速虚拟化平台,开发者可免费使用
  4. HTML5中的websocket实现直播
  5. Intel Realsense D435 Realsense View 错误 RT IC2 Config error
  6. c 冒泡排序_C语言中选择排序和冒泡排序
  7. Cross_validation.train_test_split 中 stratify这个参数的意义是什么?
  8. PS如何对JPG文件直接抠图
  9. 7宗命案,潜逃23年,大数据还是认出了她
  10. 超60亿元,新华三领衔华为锐捷中兴中标中国移动高端路由器和交换机集采
  11. opengl学习笔记(三)
  12. java代码生成器_java代码生成器怎么用
  13. (45)FPGA条件编译(选择模块)
  14. python爬视频网站数据_python爬虫基础应用----爬取无反爬视频网站
  15. 三星a60android9,三星A6060官方港版安卓9固件rom线刷包:TGY-A6060ZHU1ASH3
  16. IBM Cloud VPC网络与本地办公网络的互通
  17. 【iOS】—— KVC与KVO
  18. 读1968图灵奖获得者 哈明演讲的感悟
  19. 黑客攻防(一)网站信息收集
  20. 做产品的3个敬畏:敬畏用户、敬畏行业、敬畏生活

热门文章

  1. CVE-2018-8120 Windows权限提升
  2. g711u与g729比较编码格式
  3. ubuntu lamp配置多域名服务器
  4. Makefile与Shell的问题
  5. [导入]如何禁止掉SharePoint页面个性化?(续)
  6. java的String构造对象的几种方法以及内存运行过程
  7. CODEVS——T1979 第K个数
  8. Python开发【第八篇】:网络编程 Socket
  9. 四种launchMode
  10. Leetcode: Remove Element