题目链接

本题思路:模版的次小生成树问题,输出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. Git之旅(1):安装git

    一.在windows中安装git git官网:https://git-scm.com/downloads 二.在centos系统安装git 2.1 配置base源和epel源 # cat /etc/y ...

  2. linux find 用法和常见用例

    Linux中find常见用法示例 ·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \; 本人qq群也有许多 ...

  3. pycharm不支持svn,是需要svn命令行工具没有安装(for windows)

    1. 安装svn命令行工具 Subversion for Windows下载https://sourceforge.net/projects/win32svn/?source=typ_redirect ...

  4. getopt:命令行选项、参数处理

    在写shell脚本时经常会用到命令行选项.参数处理方式,如: ./test.sh -f config.conf -v --prefix=/home -f 为短选项,它需要一个参数,即config.co ...

  5. delphi 10.3 控件遮挡 webbrowser

    听闻10.3的新特性之一,webbrowser可以被其他控件遮挡, 在等待10.3.1出来后才开始来尝鲜, 但在webbrowser上添加控件后, 发现控件还是被挡住了, 研究发现需要将控件的cont ...

  6. linux运维、架构之路-xtrabackup

    一.XtraBackup介绍 1.备份工具 xtrabackup:是一款基于InnoDB的在线热备工具,具有开源,免费,支持在线热备,占用磁盘空间小,能够非常快速地                   ...

  7. Headless Windows

    Google Chrome 79.0.3945.79 (正式版本) (64 位) (cohort: 79_Win_79) .\chrome --headless --user-data-dir=tmp ...

  8. word的公式编辑器在插入对象里面!!!!!!!!!!!!!

    word的公式编辑器在  插入->对象  里面!!!!!!!!!!!!!

  9. 【CF686D】Kay and Snowflake(树的重心)

    题意:给定一棵n个点的树,q次询问,每次询问以某个点为根的子树编号是多少 n,q<=3e5 思路:设sz[u]为以u为根子树的size,v为u的size最大的儿子 若sz[v]*2<sz[ ...

  10. javascript中面向对象的两种构建方式(构造函数)和(原型模式的区别)

    1.构造函数模式--->alert的结果为false <!DOCTYPE html> <html lang="en"> <head> &l ...