题目链接

本题思路:模版的次小生成树问题,输出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. java代码实现H5页面

    public void getH5(HttpServletResponse response){ StringBuffer res=new StringBuffer(); res.append(&qu ...

  2. ubuntu重装--备份/配置

    https://github.com/wenlin-gk/document/blob/master/ubuntu%E5%A4%87%E4%BB%BD%2B%E9%85%8D%E7%BD%AE.txt

  3. ubuntu16.04 安装mysql

    安装mysql 1.sudo apt-get install mysql-server 2.sudo apt install mysql-client 3.sudo apt install libmy ...

  4. display:line-block

    1.那是因为第二个标签是inline-block,它的对齐方式是基线对齐,对齐的是第一个元素里面字的下划线,所以第二个元素的下边缘对齐的是1的下划线,只要在第二个元素里面加内容或者加个空格( )就可以 ...

  5. OCP

    desc dba_objects; select * from dba_objects where rownum = 6; select owner, object_id from dba_objec ...

  6. linux C++ 通讯架构(一)nginx安装、目录、进程模型

    nginx是C语言开发的,号称并发处理百万级别的TCP连接,稳定,热部署(运行时升级),高度模块化设计,可以用C++开发. 一.安装和目录 1.1 前提 epoll,linux内核版本为2.6或以上 ...

  7. unittest----skip(跳过用例)

    我们在执行测试用例时,有时有些用例时不需要执行的,这时就需要用到unittest给我们提供的跳过用例的方法 @unittest.skip(reason):强制跳过,不需要判断条件.reason是跳过原 ...

  8. hbase完全分布式搭建

    1.解压缩hbase的软件包,使用命令: tar -zxvf hbase-1.3.0-bin.tar.gz 2.进入hbase的配置目录,在hbase-env.sh文件里面加入java环境变量.即: ...

  9. 【leetcode】313. Super Ugly Number

    题目如下: 解题思路:总结一下这么几点,一出一进,优先级队列排序,保证每次输出的都是当前的最小值.解法大致如图: 代码如下: #include<map> #include<queue ...

  10. 浙大PAT CCCC L3-014 周游世界 ( 最短路变形 )

    题目链接 题意 : 中文题请点链接,挺复杂的... 分析 : 乍一看是个最短路,实际就真的是个最短路.如果没有 “ 在有多条最短路径的时候输出换乘次数最少的” 这一条件的约束,那么这题就是直接建图然后 ...