UVA-10600.Contest and Blackout.(Kruskal + 次小生成树)
本题思路:模版的次小生成树问题,输出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 + 次小生成树)的更多相关文章
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
- UVA10600:ACM Contest and Blackout(次小生成树)
ACM Contest and Blackout 题目链接:https://vjudge.net/problem/UVA-10600 Description: In order to prepare ...
- UVA-10600 ACM Contest and Blackout (次小生成树)
题目大意:给一张无向图,找出最小生成树和次小生成树. 题目分析:模板题...方法就是枚举所有的比最小生成树中两端点之间的最长边还要长的边,用它替换,再取一个最小的值便是次小生成树了. 代码如下: # ...
- UVA-10462.Is There A Second Way Left(Kruskal+次小生成树)
题目链接 本题大意:这道题用Kruskal较为容易 参考代码: #include <cstdio> #include <cstring> #include <vector ...
- UVA 10600 ACM Contest and Blackout 次小生成树
又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的 #include <iostream> #include <al ...
- 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
[题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...
- 【uva 10600】ACM Contest and Blackout(图论--次小生成树 模版题)
题意:有T组数据,N个点,M条边,每条边有一定的花费.问最小生成树和次小生成树的权值. 解法:具体请见 关于生成树的拓展 {附[转]最小瓶颈路与次小生成树}(图论--生成树) 1 #include&l ...
- uva 10600 ACM Contest And Blackout
题意: 求最小生成树和次小生成树的总权值. 思路: 第一种做法,适用于规模较小的时候,prim算法进行的时候维护在树中两点之间路径中边的最大值,复杂度O(n^2),枚举边O(m),总复杂度O(n^2) ...
- UVA - 10462-Is There A Second Way Left? Kruskal求次小生成树
UVA - 10462 题意: 求次小生成树的模板题,这道题因为有重边的存在,所以用kruskal求比较好. #include <iostream> #include <cstdio ...
随机推荐
- 闲着没事写点啥之springmvc梳理
用户通过浏览器向服务器发送请求,请求被springmvc的前端控制器dispatcherservlet拦截. dispatcherservlet拦截到请求后,会调用handlermapping处理器映 ...
- python-类对象的比较
#类对象的比较 class Person: def __init__(self,age,height): self.age=age self.height=height def __eq__(self ...
- CSS插入的四种方式
一.什么是CSS CSS(Cascading style sheets 层叠样式表),CSS可以用以为网页构建样式表,通过样式表来达到对网页进行美化的效果.所谓层叠可以将网页想象成一层层的结构,高层 ...
- MySQL两个时间相减
SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01'); interval可是: SECOND 秒 SECONDS MINUTE 分钟 MINUTE ...
- mysql DEFAULT约束 语法
mysql DEFAULT约束 语法 作用:用于向列中插入默认值. 说明:如果没有规定其他的值,那么会将默认值添加到所有的新记录.直线电机 mysql DEFAULT约束 示例 //在 "P ...
- makefile 赋值
= 是最基本的赋值:= 是覆盖之前的值?= 是如果没有被赋值过就赋予等号后面的值+= 是添加等号后面的值
- 安装JDK ,提示 错误1316 指定的账户已存在
基于情况: 安装了一个JDK 后,在文件目录中删除了相关文件,之后再次安装,提示 错误1316 指定的账户已存在 造成原因:安装JDK,相当于安装了一个软件,要使用系统的软件卸载功能卸载,不能只删除 ...
- 20181019-JSP 教程/简介
JSP 教程 这是第一篇JSP JSP与PHP.ASP.ASP.NET等语言类似,运行在服务端的语言. JSP(全称Java Server Pages)是由Sun Microsystems公司倡导和许 ...
- AC自动机2
AC自动机 给N个模式串,求文本串中出现次数最多的模式串出现次数. #include<bits/stdc++.h> using namespace std; #define maxn 10 ...
- 170826-关于spring的知识点及练习
1.Spring作用: 1.生态体系庞大,全能型选手![springmvc是其一个子模块,jdbcTemplate能直接操作数据库!] 2.将其他组件粘合在一起 3.IOC容器和AOP[Aspect ...