【LightOJ - 1123】Trail Maintenance(在线维护最小生成树,删边思维)

题干:

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 WW 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个边顺序添加,每次添加,要求在线输出MST的权值,如果凑不出MST就输出-1。

解题报告:

  直接暴力肯定会超时。

  考虑到n只有200:因为最差情况:大于n条边的时候,肯定会生成一个环,那么最终生成环的这一条边,肯定是我们最后遍历到的,而恰好肯定是环内权值最大的(因为按边权排序了之后再遍历的。),所以这一条边可以直接扔掉(简化操作就是记录一下这个边的位置,然后遍历结束后把最后一条边直接赋值过来。)

  下面给出简要证明:为什么这一条边e一定不会要,也就是,不管后面的边如何添加的,这一条边一定没用了(也就是一定不会是MST中的边):因为每次加边操作都是只加一条,所以最多替换一条边,考虑每次替换边,都是去掉一条边w1,换上一条边w2,那么这个替换执行 当且仅当w1>w2,也就是新边的权值肯定要更小,我们才考虑替换他。由上一段的分析得知,这条边e的权值we>w1,所以这一条边we>w2,所以肯定这条边没用了。因为他只要是棵树,我们不需要看边选的是啥,因为可以确定的是,树上的点一定就是那一些,所以具体哪些边没影响。至此,我们可以放心大胆的删掉这条边了。

  这次是体会到了常数的可怕。。。把自定义比较函数放在结构体内部确实是快(160ms级别和240ms级别的区别。)另外getf也是这样,uv直接变成祖先节点才能冲进200ms,不然就是200多ms。不过如果不卡并查集的话问题就不大。

   另外对于这题,不能加cnt==n-1则break。因为可能需要删除的边在后面。总之这一点可以被卡。所以要注意。不过其实最多稳定在200条边,所以加不加这个优化影响不大。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int f[222];
struct Edge {int u,v,w;bool operator<(const Edge b) const {return w < b.w;}
} e[MAX];
int tot,n,m;
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void init() {for(int i = 1; i<=n; i++) f[i] = i;
}
int klu() {sort(e+1,e+tot+1);int cnt = 0,pos=-1,res = 0;for(int i = 1; i<=tot; i++) {int u = getf(e[i].u),v = getf(e[i].v);if(u==v) pos = i;else {f[v]=u;res += e[i].w;cnt++;}
//		if(cnt == n-1) break; } if(pos != -1) {e[pos] = e[tot];tot--;}if(cnt == n-1) return res;else return -1;
}
int main()
{int t,iCase = 0;scanf("%d",&t);while(t--) {printf("Case %d:\n",++iCase);tot=0;scanf("%d%d",&n,&m);for(int a,b,c,i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&c);e[++tot].u = a;e[tot].v = b;e[tot].w = c;init();printf("%d\n",klu());}}return 0 ;
}

换了一种优化反而更慢(161ms)注意这样写的话要提前赋值tot给tott,并且遍历边的时候还是遍历到tott。因为你后面的那些边虽然可能要删除,但是前提是在你找到MST的时候,你在线就tot--了,可能人家本来就是第tot条边凑出MST,但是你这样操作就输出-1了。

总结一下这样慢的原因,其实没必要加那个cnt==n-1就break。因为本来边就不多,动态稳定在n-1附近,,所以没必要,,因为反而增加了很多次if判断,所以耗时了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int f[222];
struct Edge {int u,v,w;bool operator<(const Edge b) const {return w < b.w;}
} e[MAX];
int tot,n,m;
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void init() {for(int i = 1; i<=n; i++) f[i] = i;
}
int klu() {sort(e+1,e+tot+1);int cnt = 0,pos=-1,res = 0,tott=tot;for(int i = 1; i<=tott; i++) {int u = getf(e[i].u),v = getf(e[i].v);if(u==v) {e[i] = e[tot];tot--;}else {f[v]=u;res += e[i].w;cnt++;}if(cnt == n-1) {tot=i;break; }} 
//	if(pos != -1) {
//		e[pos] = e[tot];
//		tot--;
//	}if(cnt == n-1) return res;else return -1;
}
int main()
{int t,iCase = 0;scanf("%d",&t);while(t--) {printf("Case %d:\n",++iCase);tot=0;scanf("%d%d",&n,&m);for(int a,b,c,i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&c);e[++tot].u = a;e[tot].v = b;e[tot].w = c;init();printf("%d\n",klu());}}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440360.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

7.Working with External Libraries

在本课中&#xff0c;我将讨论Python中的imports&#xff0c;提供一些使用不熟悉的库&#xff08;以及它们返回的对象&#xff09;的技巧&#xff0c;并深入研究Python的内容&#xff0c;以及谈谈运算符重载。 Imports 到目前为止&#xff0c;我们已经讨论了内置于该语言的类…

计算机原理期中考试,计算机组成原理期中考试试题

一、单选题(每小题2分&#xff0c;共34分)1&#xff0e;完整的计算机系统应包括__________。A&#xff0e;运算器、存储器、控制器 B&#xff0e; 主机和实用程序C&#xff0e;配套的硬件设备和软件系统 D&#xff0e; 外部设备和主机2&#xff0e;下列数中真值最小的数是_____…

【HDU - 1839】Delay Constrained Maximum Capacity Path(最短路 + 二分)

题干&#xff1a; 考虑一个包含 N 个顶点的无向图&#xff0c;编号从 1 到 N&#xff0c;并包含 M 条边。编号为 1 的顶点对应了一个矿藏&#xff0c;可从中提取珍稀的矿石。编号为 N 的顶点对应了一个矿石加工厂。每条边有相应的通行时间 (以时间单位计)&#xff0c;以及相应…

0.Overview

本文为Kaggle Learn的Python课程的中文翻译&#xff0c;原文链接为&#xff1a;https://www.kaggle.com/learn/python 欢迎来到Kaggle Learn的Python课程。本课程将介绍在开始使用Python进行数据科学之前需要的基本Python技能。这些课程针对那些具有一些编程经验的人&#xff…

量子计算机的体积有多大,量子计算机也能实现摩尔定律

原标题&#xff1a;量子计算机也能实现摩尔定律量子计算机拥有很强大的计算力&#xff0c;但是这对IBM来说&#xff0c;似乎还不够。据CNET消息&#xff0c;IBM制作了一个路线图&#xff0c;表达出了自己在量子计算领域的野心。IBM在图表的纵轴上列出了一个单位“量子体积(Quan…

1.How Models work

Introduction 我们首先概述机器学习模型如何工作以及如何使用它们。如果您之前已完成统计建模或机器学习&#xff0c;这可能会感觉很基础。别担心&#xff0c;我们很快就会建立强大的模型。 本课程将为您构建以下场景的模型&#xff1a; 你的堂兄已经花了数百万美元预测房地产…

【BZOJ - 1059】矩阵游戏(二分图匹配,建图,最小边覆盖)

题干&#xff1a; 小Q是一个非常聪明的孩子&#xff0c;除了国际象棋&#xff0c;他还很喜欢玩一个电脑益智游戏——矩阵游戏。矩阵游戏在一个N *N黑白方阵进行&#xff08;如同国际象棋一般&#xff0c;只是颜色是随意的&#xff09;。每次可以对该矩阵进行两种操作&#xff…

ji计算机一级题库,全国计算机等级考试一级题库0l0ji.doc

全国计算机等级考试一级题库0l0ji全国计算机等级考试一级题库(1)1&#xff0e;微机中1K字节表示的二进制位数是( )。D?   A、1000   B、8x1000   C、1024   D、8x1024??2&#xff0e;计算机硬件能直接识别和执行的只有( )。D?   A、高级语言   B、符号语言   …

2.Explore Your Data

Using Pandas to Get Familiar With Your Data 任何机器学习项目的第一步都是熟悉数据。 您将使用Pandas库。 Pandas是科学家用于挖掘和处理数据的主要工具。 大多数人在他们的代码中将pandas缩写为pd。 我们使用如下命令执行此操作。 [1] import pandas as pd Pandas库中最…

【POJ - 3026】Borg Maze(bfs预处理 + 最小生成树,建图)

题干&#xff1a; The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the c…

计算机统考测试,计算机统考专业测试题.doc

文档介绍&#xff1a;应用所有单选题1、下面是某单位主页地址的,其中符合格式的是。A:B:C:D:答案:C知识点:应用部分\和的使用\浏览器的使用\1网页的几个基本术语2、用浏览器浏览网页,在地址栏中输入网址时,通常可以省略的是。A:B:C:D:答案:A知识点:应用部分\和的使用\浏览器的使…

3.Your First Machine Learning Model

Selecting Data for Modeling 你的数据集有太多的变量包裹住你的头。你怎么能把这些压倒性的数据削减到你能理解的东西&#xff1f; 我们首先使用我们的直觉选择一些变量。 后面的课程将向您展示自动确定变量优先级的统计技巧。 要选择变量/列&#xff0c;我们需要查看数据集中…

【POJ - 3020】Antenna Placement (匈牙利算法,二分图最小边覆盖)

题干&#xff1a; The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It …

计算机教室安全预案 博客,校园安全应急预案

校园安全应急预案为了确保师生的人身安全&#xff0c;严格执行上级安全工作的管理要求&#xff0c;保证一旦发生安全事故能够及时处理&#xff0c;特制定我校安全应急预案。一、领导小组组 长&#xff1a;副组长&#xff1a;成 员&#xff1a;全体教师二、主要职责1、组长任校…

4.Model Validation

你已经建立了一个模型。 但它有多好&#xff1f; 在本课程中&#xff0c;您将学习如何使用模型验证来衡量模型的质量。 测量模型质量是迭代改进模型的关键。 What is Model Validation 你几乎要评估你构建的每个模型。在大多数&#xff08;尽管不是全部&#xff09;应用中&am…

【POJ - 2195】Going Home(二分图最优匹配,费用流 或 KM)

题干&#xff1a; On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step h…

微机原理实验8254计算机钢琴,GitHub - SincereXIA/PianoMFC: 西电微机原理课设项目,键盘电子乐器演奏程序设计(电子琴),MFC...

PianoMFC西电微机原理课设项目&#xff0c;键盘电子乐器演奏程序设计(电子琴)&#xff0c;MFC需要连接西电微机原理实验室提供的 QTH9054 微机试验箱&#xff0c;使用其蜂鸣器发声&#xff0c;若不连接&#xff0c;程序会直接播放 mp3 文件模拟钢琴声。请在 release 处下载编译…

5.Underfitting and Overfitting

在这一步结束时&#xff0c;您将了解欠拟合和过拟合的概念&#xff0c;并且您将能够应用这些办法来使您的模型更准确。 Experimenting With Different Models 现在您已经有了一种可靠的方法来测量模型精度&#xff0c;您可以尝试使用其他模型&#xff0c;并查看哪种模型可以提…

福建省计算机初级职称,2019福建助理工程师职称评定条件材料及审核管理制度...

一学历、资历条件要求(破格申报不在此列&#xff0c;详情请咨询了解)申报工程技术系列中级工程师须符合下列条件之一&#xff1a;1.博士研究生毕业&#xff1b;2.硕士研究生毕业后&#xff0c;从事所申报专业工作满3年&#xff1b;3.本科毕业后&#xff0c;从事所申报专业工作满…

【POJ - 2594】Treasure Exploration(floyd传递闭包 + 最小路径覆盖,图论)

题干&#xff1a; Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings…