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 ...
随机推荐
- sql语句感想
select出来内容可以当成表拿来用,,比如取别名什么的. union是纵向的,追加记录(行) join on是横向的,追加列
- SQLSERVER:Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
背景: 在最近开发中遇到一个问题,对一个数据库进行操作时,我采用64个并行的任务每个任务保证一个数据库连接对象:但是每个任务内部均包含有24个文件需要读取,在读取文件之后,我们需要快速将这24个文件批 ...
- 6.理解DispatcherServlet
DispatcherServlet的作用 DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,负责职责的分派, 且与Spring IoC容器无缝 ...
- editplus3运行Python程序
editplus3是一款不错的编辑器,他可以编译,运行java,php等各种程序,现把他运行Python程序的方法贴出来,首先得安装python,然后打开editplug3,工具——配置用户工具——组 ...
- csu oj 1339: 最后一滴血
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1339 1339: 最后一滴血 Time Limit: 1 Sec Memory Limit: 1 ...
- C++之路进阶——codevs1204(寻找子串位置)
1204 寻找子串位置 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 给出字符串a和字符串b,保证b是a的一个子 ...
- HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)
Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node ...
- 变形--矩阵 matrix()
matrix() 是一个含六个值的(a,b,c,d,e,f)变换矩阵,用来指定一个2D变换,相当于直接应用一个[a b c d e f]变换矩阵.就是基于水平方向(X轴)和垂直方向(Y轴)重新定位元素 ...
- CCF真题之图像旋转
201503-1 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON SetLineStyle1
zw版[转发·台湾nvp系列Delphi例程]HALCON SetLineStyle1 procedure TForm1.Button1Click(Sender: TObject);var img : ...