poj1861 network(并查集+kruskal最小生成树
题目地址:http://poj.org/problem?id=1861
题意:输入点数n和边数n,m组边(点a,点b,a到b的权值)。要求单条边权值的最大值最小,其他无所谓(所以多解:(。输出单条边最大值,边的数量,每条边(点a,点b)。
思路:结构体记录节点x,y和权值d。写一个比较函数+sort使结构体按权值由小到大排序。
并查集:两个集合被选中的和没被选中的。
kruskal:初始化每个节点独自一个集合,每次输入不在一个集合的就合并并记录,在一个集合的不管。输出记录数组最后一个节点的权值(edge[c-1].d),记录数组大小(c-1),每条边(从1到c-1)【kruskal函数最后一次多了一个c++,脚标又是从1开始。
#include <cstdio>
#include <algorithm> using namespace std; const int N = +; struct bian
{
int x, y, d;
}edge[N], ans[N]; int fa[N], c = , n, m; int cmp(bian a, bian b)
{
return a.d < b.d;
} void init(int n)
{
for(int i = ; i <= n; i++)
fa[i] = i;
} int found(int x)
{
if(fa[x] == x)
return x;
else
fa[x] = found(fa[x]);
return fa[x];
} void unite(int x, int y)
{
int px = found(x);
int py = found(y);
if(px == py)
return;
fa[py] = px;
} void kruskal()
{
for(int i = ; i <= m; i++)
{
int x = edge[i].x;
int y = edge[i].y;
if(found(x) != found(y))//两顶点不在一个集合则把该边放入
{
unite(x, y);
ans[c].x = x;
ans[c].y = y;
ans[c].d = edge[i].d;
c++;
}
}
} int main()
{
scanf("%d%d", &n, &m);//n个点,m条边
init(n);
for(int i = ; i <= m; i++)
{
scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].d);
}
sort(edge+, edge+m+, cmp);//sort函数是从0到n-1的!!!!!!!!!!!!!!!
kruskal();
printf("%d\n", ans[c-].d);
printf("%d\n", c-);
for(int i = ; i < c; i++)
{
printf("%d %d\n", ans[i].x, ans[i].y);
}
return ;
}
【玛德!!!!!!!!!!!sort默认从0到n-1!!!!!!!!!!要是脚标从1开始记得改成sort(a+1, a+n+1)!!!!!!!!!!!】
poj1861 network(并查集+kruskal最小生成树的更多相关文章
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
- POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Description There are N v ...
- POJ 3723 Conscription (Kruskal并查集求最小生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14661 Accepted: 5102 Des ...
- 并查集与最小生成树Kruskal算法
一.什么是并查集 在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集的合并及查询问题.有一个联合-查找算法(union-find algorithm)定义了两个用于次数据结构的操作: Fi ...
- 并查集和kruskal最小生成树算法
并查集 先定义 int f[10100];//定义祖先 之后初始化 for(int i=1;i<=n;++i) f[i]=i; //初始化 下面为并查集操作 int find(int x)//i ...
- POJ 2236 Wireless Network (并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 18066 Accepted: 761 ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)
还是畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
随机推荐
- pywinauto教程
转:pywinauto教程https://blog.csdn.net/weixin_40161673/article/details/83246861 ** 一.环境安装**1.命令行安装方法pip ...
- python2.7环境下升级pip3,及出错解决办法
执行 pip3 install --upgrade pip 进行升级 升级后若出现, Import Error:cannot import name main 是因为将pip更新为10.0.0后库里面 ...
- Codeforces Round #611 (Div. 3) C
There are nn friends who want to give gifts for the New Year to each other. Each friend should give ...
- ConcurrentHashMap 实现缓存类
参考:https://blog.csdn.net/woshilijiuyi/article/details/81335497 在规定时间内,使用 hashMap 实现一个缓存工具类,需要考虑一下几点 ...
- NSObject类的API介绍
这篇文章围绕的对象就是NSObject.h文件,对声明文件中的属性.方法进行必要的“翻译”. 该文件大致由两部分组成:NSObject协议和NSObject类. (一)NSObject协议 - (BO ...
- HTML5中新增的主体结构元素
article元素 article元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容. 它可以使一篇博客或者报刊中的文章,一篇论坛帖子.一段用户评论或独立的插件,或其他任何独立的内 ...
- 用Jackson进行Json序列化时的常用注解
Jackson时spring boot默认使用的json格式化的包,它的几个常用注解: @JsonIgnore 用在属性上面,在序列化和反序列化时都自动忽略掉该属性 @JsonProperty(&qu ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:地址(Address)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:内联子标题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 【PAT甲级】1007 Maximum Subsequence Sum (25 分)
题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...