题目链接

本题思路:模版的次小生成树问题,输出MST and Second_MST的值。

参考代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std; const int maxn = + , maxe = * / + , INF = 0x3f3f3f3f;
int n, m, Max[maxn][maxn], pir[maxn];
struct Edge {
int u, v, w;
bool vis;
}edge[maxe];
vector<int> G[maxn]; bool cmp(const Edge &a, const Edge &b) {
return a.w < b.w;
} int Find(int x) {
if(x == pir[x]) return x;
return pir[x] = Find(pir[x]);
} int Kruskal() {
sort(edge + , edge + m + , cmp);
for(int i = ; i <= n; i ++) {
G[i].clear();
pir[i] = i;
G[i].push_back(i);
}
int cnt = , ans = ;
for(int i = ; i <= m; i ++) {
int fx = Find(edge[i].u), fy = Find(edge[i].v);
if(cnt == n - ) break;
if(fx != fy) {
cnt ++;
int len_fx = G[fx].size(), len_fy = G[fy].size();
edge[i].vis = true;
ans += edge[i].w;
for(int j = ; j < len_fx; j ++) {
for(int k = ; k < len_fy; k ++) {
Max[G[fx][j]][G[fy][k]] = Max[G[fy][k]][G[fx][j]] = edge[i].w;
}
}
pir[fx] = fy;
for(int j = ; j < len_fx; j ++)
G[fy].push_back(G[fx][j]);
}
}
if(cnt < n - ) return INF;
else return ans;
} int Second_Kruskal(int MST) {
int ans = INF;
for(int i = ; i <= m; i ++) {
if(!edge[i].vis)
ans = min(ans, MST + edge[i].w - Max[edge[i].u][edge[i].v]);
}
return ans;
} int main () {
int t;
scanf("%d", &t);
while(t --) {
scanf("%d %d", &n, &m);
for(int i = ; i <= m; i ++) {
scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w);
edge[i].vis = false;
}
int MST = Kruskal();
int Second_MST = Second_Kruskal(MST);
printf("%d %d\n", MST, Second_MST);
}
return ;
}

UVA-10600.Contest and Blackout.(Kruskal + 次小生成树)的更多相关文章

  1. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树

    题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...

  2. UVA10600:ACM Contest and Blackout(次小生成树)

    ACM Contest and Blackout 题目链接:https://vjudge.net/problem/UVA-10600 Description: In order to prepare ...

  3. UVA-10600 ACM Contest and Blackout (次小生成树)

    题目大意:给一张无向图,找出最小生成树和次小生成树. 题目分析:模板题...方法就是枚举所有的比最小生成树中两端点之间的最长边还要长的边,用它替换,再取一个最小的值便是次小生成树了. 代码如下: # ...

  4. UVA-10462.Is There A Second Way Left(Kruskal+次小生成树)

    题目链接 本题大意:这道题用Kruskal较为容易 参考代码: #include <cstdio> #include <cstring> #include <vector ...

  5. UVA 10600 ACM Contest and Blackout 次小生成树

    又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的 #include <iostream> #include <al ...

  6. 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)

    [题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...

  7. 【uva 10600】ACM Contest and Blackout(图论--次小生成树 模版题)

    题意:有T组数据,N个点,M条边,每条边有一定的花费.问最小生成树和次小生成树的权值. 解法:具体请见 关于生成树的拓展 {附[转]最小瓶颈路与次小生成树}(图论--生成树) 1 #include&l ...

  8. uva 10600 ACM Contest And Blackout

    题意: 求最小生成树和次小生成树的总权值. 思路: 第一种做法,适用于规模较小的时候,prim算法进行的时候维护在树中两点之间路径中边的最大值,复杂度O(n^2),枚举边O(m),总复杂度O(n^2) ...

  9. UVA - 10462-Is There A Second Way Left? Kruskal求次小生成树

    UVA - 10462 题意: 求次小生成树的模板题,这道题因为有重边的存在,所以用kruskal求比较好. #include <iostream> #include <cstdio ...

随机推荐

  1. GUI学习之十三——QPlainTextEdit学习总结

    QPlainTextEdit可以说是一个简化版的QTextEdit类控件,和QTextEdit的功能差不多,使用了QTextEdit和QTextDocument作为背后实现的技术支撑. 由于QPlai ...

  2. jenkins上job误删除怎么恢复

    1.点击jobConfigHistory 2.点击Show deleted jobs only 3.找到被删除的 记录,点击Restore

  3. AtomicReference和AtomicStampedReference

    AtomicReference类提供了一个可以原子读写的对象引用变量. 原子意味着尝试更改相同AtomicReference的多个线程(例如,使用比较和交换操作)不会使AtomicReference最 ...

  4. 1--面试总结-js深入理解,对象,原型链,构造函数,执行上下文堆栈,执行上下文,变量对象,活动对象,作用域链,闭包,This

    参考一手资料:http://dmitrysoshnikov.com/ecmascript/javascript-the-core/中文翻译版本:https://zhuanlan.zhihu.com/p ...

  5. springboot 集成oss

    集成aliyun oss 结构如下: pom.xml <dependency> <groupId>org.springframework.boot</groupId> ...

  6. Android PdfViewer案例使用

    今天按项目要求找了一个android的PDF控件,各种操作效果都非常好,在这里和大家分享一下. com.joanzapata.pdfview:android-pdfview  该PDF控件加载大存储的 ...

  7. TinyMCE不可编辑

    1. 通过配置在控件初始化时设置 tinyMCE.init({ readonly : 1 }); 2.tinymce.activeEditor.getBody().setAttribute('cont ...

  8. Python的命令行参数(argparse)

    参考:https://www.cnblogs.com/lindaxin/p/7975697.html 参考:https://www.cnblogs.com/dengtou/p/8413609.html ...

  9. iOS---实现在屏幕上实时绘图的简单效果---CAShaperLayer和UIBezierPath的简单运用

    首先,声明几个属性 @property(nonatomic,strong)UIBezierPath * beizer; @property(nonatomic,assign)CGPoint start ...

  10. ASP.NET上传一个文件夹

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...