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 ...
随机推荐
- TOM大叔的几道Javascript题目与解答
几道JS题目 之前没有深入研究js语言,最近几年前端越来越工程化,需要扎实的js基础,看到博客园上有很多大牛分享JS学习文章,幸运看到tom大叔的blog,抽时间潜心学习了其文章,遇到到其出的几道题目 ...
- 《30天自制操作系统》09_day_学习笔记
harib06a: 在昨天的最后一部分,我们已经变成了32位的模式,目的就是希望能够使用电脑的全部内存. 虽然鼠标的显示处理有一些叠加问题,不过笔者为了不让我们感到腻烦,先带我们折腾一下内存 这里笔者 ...
- PostgreSQL Obtaining the Result Status
There are several ways to determine the effect of a command. The first method is to use the GETDIAGN ...
- ADO.net 防止SQL 字符串注入攻击
规避SQL注入 如果不规避,在黑窗口里面输入内容时利用拼接语句可以对数据进行攻击 如:输入Code值 p001' union select * from Info where '1'='1 //这样可 ...
- iOS 检查版本号的代码
- (void)checkNewVersion{ if ([@"appStore" isEqualToString:CHANNEL]) { AFHTTPRequestOperati ...
- 搜集好的java技术帖子,持续更新,java程序员的要求
1.Java NIO 系列教程 2.Java实现 二叉搜索树算法(BST) 3. Java 并发工具包 java.util.concurrent 用户指南 4.架构师之路系列:http://blog. ...
- paper 75:使用MATLAB的神经网络工具箱创建神经网络
% 生成训练样本集 clear all; clc; P=[110 0.807 240 0.2 15 1 18 2 1.5; 110 2.865 240 0.1 15 2 12 1 2; 110 2.5 ...
- oracle随机取数据
select * from (select rownum,KEYWORD, CATEGORY,CREATE_DATE,UPDATE_DATE from (select * from knet_keyw ...
- 夺命雷公狗---node.js---7fs模块初步
目录结构如下所示: /** * Created by leigood on 2016/8/13. */ var http = require("http"); var fs = r ...
- 《OpenGL游戏编程》第9章-PlanarShadow关键代码注释
阴影这块确实是难点.说到阴影就必须提到投影矩阵.模板值为1和2时分别渲染.说来话长,仅仅放上代码,供日后查阅. /** 渲染墙面和阴影 */ void CPlanarShadow::Render() ...