题目地址: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最小生成树的更多相关文章

  1. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  2. POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K               Description There are N v ...

  3. POJ 3723 Conscription (Kruskal并查集求最小生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Des ...

  4. 并查集与最小生成树Kruskal算法

    一.什么是并查集 在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集的合并及查询问题.有一个联合-查找算法(union-find algorithm)定义了两个用于次数据结构的操作: Fi ...

  5. 并查集和kruskal最小生成树算法

    并查集 先定义 int f[10100];//定义祖先 之后初始化 for(int i=1;i<=n;++i) f[i]=i; //初始化 下面为并查集操作 int find(int x)//i ...

  6. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  7. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  8. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

  9. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. leetcode 0216

    目录 ✅ 893. 特殊等价字符串组 描述 解答 cpp py ✅ 811. 子域名访问计数 描述 解答 cpp py ✅ 509. 斐波那契数 描述 解答 cpp py ✅ 521. 最长特殊序列 ...

  2. [运维] 如何解决 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

    环境: 虚拟机 linux centos 7 64 当时正在配置 nginx , 由于解压后的 nginx 默认安装位置是在 /usr/local/ 目录下, 而这个目录是 root 用户才有权限操作 ...

  3. JAVA 常用包

    JAVA是以包的形式进行语言结构组织的. 引入这些包的关键词就是 import 下面说说 JAVA常用包有下面的几个 1. java.lang 这个是默认引入的,也是一个最基础的包.其中lang不是中 ...

  4. push 、pop 、unshift 、shift

    push .pop : 操作数组后面 unshift .shift :操作数组前面 push.unshift : 字母多的添加 pop .shift : 字母少的删除 push.unshift : 添 ...

  5. php 高级 多台web服务器共享session的方法

    解决多台web服务器共享session的问题,至少有以下三种方法:   一.将本该保存在web服务器磁盘上的session数据保存到cookie中 即用cookie会话机制替代session会话机制, ...

  6. 二分查找及几种变体的Python实现

    1. 在不重复的有序数组中,查找等于给定值的元素 循环法 def search(lst, target): n = len(lst) if n == 0: return -1 low = 0 high ...

  7. Ubuntu开启端口(持久化)

    1.查看已经开启的端口 sudo ufw status 2.打开80端口 sudo ufw allow 3.防火墙开启 sudo ufw enable 4.防火墙重启 sudo ufw reload

  8. Java 代码中如何调用 第三方Api

    在代码中调用第三方API 获取数据 package com.example.demo.utils; import com.alibaba.fastjson.JSONObject; import lom ...

  9. idea没有import project解决办法

    参考:https://blog.csdn.net/zengxiaosen/article/details/52807540

  10. Android游戏开发学习(5)--实现Button悬浮于与SurfaceView之上

    原文:http://daikainan.iteye.com/blog/1407355 实现Button悬浮于与SurfaceView之上实现 先看效果: 注意:你实现的SurfaceView和andr ...