题目链接

本题大意:这道题用Kruskal较为容易

参考代码:

 #include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; const int maxn = + , maxe = + , INF = 0x3f3f3f3f;
int n, m, pir[maxn], Max[maxn][maxn];
vector <int> G[maxn];
struct Edge {
int u, v, w;
bool vis;
}edge[maxe]; int Find(int x) {
if(x == pir[x]) return x;
return pir[x] = Find(pir[x]);
} bool cmp(const Edge &a, const Edge &b) {
return a.w < b.w;
} int Kruskal() {
sort(edge + , edge + m + , cmp);
int ans = , cnt = ;
for(int i = ; i <= n; i ++) {
G[i].clear();
G[i].push_back(i);
pir[i] = i;
}
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 ++;
edge[i].vis = true;
ans += edge[i].w;
int len_fx = G[fx].size(), len_fy = G[fy].size();
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;
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, Case = ;
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;
}
printf("Case #%d : ", ++Case);
int MST = Kruskal();
if(MST == INF) {
printf("No way\n");
continue;
}
int Second_MST = Second_Kruskal(MST);
if(Second_MST == INF) printf("No second way\n");
else printf("%d\n", Second_MST);
}
return ;
}

UVA-10462.Is There A Second Way Left(Kruskal+次小生成树)的更多相关文章

  1. UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解

    思路: Prim: 这道题目中有重边 Prim可以先加一个sec数组来保存重边的次小边,这样不会影响到最小生成树,在算次小生成树时要同时判断次小边(不需判断是否在MST中) Kruskal: Krus ...

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

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

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

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

  4. UVA 10462 Is There A Second Way Left? 次小生成树

    模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdli ...

  5. UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】

    Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently a ...

  6. UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)

    题目大意: Nasa应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...

  7. UVA - 10462 Is There A Second Way Left?

    题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...

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

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

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

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

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

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

随机推荐

  1. centos 安装代理服务 proxy server squild

    以下内容复制自https://blog.csdn.net/qingfengxulai/article/details/80853870 放置此处便于检索 1 安装 sudo yum install s ...

  2. u-boot-2016.09 make编译过程分析(二)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/guyongqiangx/article/ ...

  3. Ansible笔记(2)--配置清单

    一.Ansible Inventory配置及详解 Inventory是ansible管理主机信息的配置文件,默认存放在/etc/ansible/hosts.在使用时通过 -i 或 --inventor ...

  4. SQL查询优化的步骤

    一.定位慢查询 SQL优化的一般步骤:先查询mysql数据库运行状况,然后定位慢查询,再分析sql的执行过程,然后进行优化 1.使用show status查询数据库的运行状况 //显示数据库运行状态 ...

  5. 线段树维护区间前k小

    线段树维护区间前k小 $ solution: $ 觉得超级钢琴太麻烦?在这里线段树提供一条龙服务 . 咳咳,开始讲正题!这道题我们有一个和超级钢琴复杂度一样 $ ~O(~\sum x\times lo ...

  6. 【hackerrank】Weather Observation Station 18

    题目如下: Consider  and  to be two points on a 2D plane. happens to equal the minimum value in Northern ...

  7. npm cache clean --force

    当出现这个问题时npm ERR! Unexpected end of JSON input while parsing near '...,"dist":{"shasum ...

  8. LeetCode--072--编辑距离(python)

    给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符删除一个字符替换一个字符示例 1: 输入: ...

  9. LTM_本地流量管理(二)

    会话保持 首先要熟悉两个概念:连接connect和会话session 连接:在四层负载均衡中,连接是最小元素. l  源端口:客户端随机产生的端口. l  源地址:发起请求的源IP地址. l  目的端 ...

  10. SQL的七种连接

    book表: t_book表: 一:inner join  AB共有的. select * from book inner join t_book on book.t_id=t_book.t_id 查 ...