ACM题目————次小生成树
Description
最小生成树大家都已经很了解,次小生成树就是图中构成的树的权值和第二小的树,此值也可能等于最小生成树的权值和,你的任务就是设计一个算法计算图的最小生成树。
Input
存在多组数据,第一行一个正整数t,表示有t组数据。
每组数据第一行有两个整数n和m(2<=n<=100),之后m行,每行三个正整数s,e,w,表示s到e的双向路的权值为w。
Output
输出次小生成树的值,如果不存在输出-1。
Sample Input
2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2
Sample Output
4
6
克鲁斯卡尔算法。
求一次是最小生成树,两次就是次小生成数了。
//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <string>
#include <queue>
#define INF 100000
using namespace std;
const int maxn = ;
typedef long long ll ;
int fa[maxn];//并查集
int vis[maxn];//记录下标
int path;//记录最小生成树用到边的数量
int T, n, m;
typedef struct node{
int st;
int ed;
int w;
bool operator < (const node& A) const {
return w<A.w ;
}
}node;
//初始化
void init(){
//获取并查集的大小
int len = sizeof(fa)/sizeof(fa[]);
for(int i=; i<len; i++){
fa[i] = i;
}
}
//并查集的查找——查找父节点
int findfa(int x){
int pa;
if( x == fa[x] ) return x;
pa = findfa(fa[x]);
fa[x] = pa;
return pa;
}
//求最小生成树
int minTree(node *points, int m, int n)
{
init(); int i, count, flag, pa, pb; for (i = count = flag = path = ; i < m; i ++) {
pa = findfa(points[i].st);
pb = findfa(points[i].ed); if (pa != pb) {
vis[path ++] = i;
fa[pa] = pb;
count ++;
} if (count == n - ) {
flag = ;
break;
}
} return flag;
} // 求次小生成树
int secMinTree(node *points, int m, int n)
{
int i, j, min, tmp, pa, pb, count, flag; for (i = , min = INF; i < path; i ++) {
init(); // 求次小生成树
for (j = count = tmp = flag = ; j < m; j ++) {
if (j != vis[i]) {
pa = findfa(points[j].st);
pb = findfa(points[j].ed); if (pa != pb) {
count ++;
tmp += points[j].w;
fa[pa] = pb;
} if (count == n - ) {
flag = ;
break;
}
}
} if (flag && tmp < min) min = tmp;
} min = (min == INF) ? - : min; return min;
}
int main(){
node *p;
scanf("%d",&T);
while( T -- ){
scanf("%d %d",&n, &m);
p = (node *)malloc(sizeof(node) * m);
for(int i=; i<m; i++){
scanf("%d %d %d",&p[i].st,&p[i].ed,&p[i].w);
}
sort(p,p+m); int f = minTree(p,m,n); if( f == ){//无法生成最小生成树
printf("-1\n");
continue;
} else {
int Min = secMinTree(p,m,n);
printf("%d\n",Min);
}
//释放p
free(p);
}
return ;
}
ACM题目————次小生成树的更多相关文章
- UVA10600:ACM Contest and Blackout(次小生成树)
ACM Contest and Blackout 题目链接:https://vjudge.net/problem/UVA-10600 Description: In order to prepare ...
- UVA10600 ACM Contest and Blackout —— 次小生成树
题目链接:https://vjudge.net/problem/UVA-10600 In order to prepare the “The First National ACM School Con ...
- UVA-10600 ACM Contest and Blackout (次小生成树)
题目大意:给一张无向图,找出最小生成树和次小生成树. 题目分析:模板题...方法就是枚举所有的比最小生成树中两端点之间的最长边还要长的边,用它替换,再取一个最小的值便是次小生成树了. 代码如下: # ...
- 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 < ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
- 【uva 10600】ACM Contest and Blackout(图论--次小生成树 模版题)
题意:有T组数据,N个点,M条边,每条边有一定的花费.问最小生成树和次小生成树的权值. 解法:具体请见 关于生成树的拓展 {附[转]最小瓶颈路与次小生成树}(图论--生成树) 1 #include&l ...
- URAL 1416 Confidential(次小生成树)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1416 Zaphod Beeblebrox — President of the Impe ...
- hdu4081 次小生成树变形
pid=4081">http://acm.hdu.edu.cn/showproblem.php?pid=4081 Problem Description During the Warr ...
随机推荐
- C# 字典 Dictionary<Tkey,Tvalue>
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...
- [转]EntityFramework走马观花之CRUD(中)
学习Entity Framework技术期间查阅的优秀文章,出于以后方便查阅的缘故,转载至Blog,可查阅原文:http://blog.csdn.net/bitfan/article/details/ ...
- ios app下载跳到itunes
<body class="box"> <div class="text"> <a href="https://itune ...
- iOS8中用UIVisualEffectView实现高斯模糊视图(毛玻璃效果)
UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; UIVisualEffectView *vi ...
- Java中几种日志方案
.本文记录Java中几种常用的日志解决方案 0x01 Log4j .这应该是一个比较老牌的日志方案了,配置也比较简单,步骤如下 1)添加对应依赖,比如 Gradle 中 dependencies { ...
- php4.3.4.4、apache2.0.4.8、mysql 4.0.26、window7 配置过程
apache的安装不需要过程,直接默认安装,下一步 下一步就Ok了. php4的安装: 1 将php-4.0.4-Win32.zip(最新版本4.0.4)解压缩到硬盘的一个目录中,例如解压到E:php ...
- WebDriver:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms
今天尝试最新的webDriver与fireFox搭配: 运行代码时出现如下的问题,但是浏览器却可以打开: org.openqa.selenium.firefox.NotConnectedExcepti ...
- javascript浏览器对象
window对象 1.window对象 window对象是BOM的核心,window对象指当前的浏览器窗口 所有JS全局对象.函数以及变量均自动成为window对象的成员 全局变量是window对象的 ...
- EBS 密码相关
SELECT usr.user_name, apps.cux_fnd_web_sec.decrypt ((SELECT (SELECT apps.cux_fnd_web_sec.decrypt (fn ...
- PIC18F中断定时器
//基于MCC18编译器,使用HI-PICC不可用 //-------------------------------------------- #include <p18F452.h> ...