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 ...
随机推荐
- iOS UITableView点击按钮滚到顶部
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- css 正方体
<!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...
- Java基础之处理事件——添加工具栏(Sketcher 7 with File toolbar buttons)
控制台程序. 工具栏在应用程序窗口中通常位于内容面板顶部的菜单栏下,包含直接访问菜单选项的按钮.在Sketcher程序中可以为最常用的菜单项添加工具栏. 工具栏是javax.swing.JToolBa ...
- CareerCup: 17.14 minimize unrecognized characters
Oh, no! You have just completed a lengthy document when you have an unfortu- nate Find/Replace misha ...
- SQL isnull函数
select * from emp;
- vs2010打包系统必备选择.net framework 3.5sp1编译错误的解决方法
利用visual studio 2010进行打包程序,默认安装的是Framework 4.0,如果需要将3.5sp1打包到系统中一起安装(选择了"从与我的应用程序相同的位置下载系统必备组件& ...
- Grunt 自动化部署之css、image、javascript、html压缩Gruntfile.js配置
grunt.initConfig方法 用于模块配置,它接受一个对象作为参数.该对象的成员与使用的同名模块一一对应. 每个目标的具体设置,需要参考该模板的文档.就cssmin来讲,minify目标的参数 ...
- HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...
- 夺命雷公狗ThinkPHP项目之----企业网站5之栏目的添加(主要是图片上传)
我们照老,先老搞定控CategoryController.class.php制器,代码如下所示: <?php namespace Admin\Controller; use Think\Cont ...
- 夺命雷公狗---DEDECMS----7dedecms目录结构
我们dedecms的目录结构其实只需要一张图即可明了了,如下图所示: