题目地址: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开始。

  1. #include <cstdio>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. const int N = +;
  7.  
  8. struct bian
  9. {
  10. int x, y, d;
  11. }edge[N], ans[N];
  12.  
  13. int fa[N], c = , n, m;
  14.  
  15. int cmp(bian a, bian b)
  16. {
  17. return a.d < b.d;
  18. }
  19.  
  20. void init(int n)
  21. {
  22. for(int i = ; i <= n; i++)
  23. fa[i] = i;
  24. }
  25.  
  26. int found(int x)
  27. {
  28. if(fa[x] == x)
  29. return x;
  30. else
  31. fa[x] = found(fa[x]);
  32. return fa[x];
  33. }
  34.  
  35. void unite(int x, int y)
  36. {
  37. int px = found(x);
  38. int py = found(y);
  39. if(px == py)
  40. return;
  41. fa[py] = px;
  42. }
  43.  
  44. void kruskal()
  45. {
  46. for(int i = ; i <= m; i++)
  47. {
  48. int x = edge[i].x;
  49. int y = edge[i].y;
  50. if(found(x) != found(y))//两顶点不在一个集合则把该边放入
  51. {
  52. unite(x, y);
  53. ans[c].x = x;
  54. ans[c].y = y;
  55. ans[c].d = edge[i].d;
  56. c++;
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. scanf("%d%d", &n, &m);//n个点,m条边
  64. init(n);
  65. for(int i = ; i <= m; i++)
  66. {
  67. scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].d);
  68. }
  69. sort(edge+, edge+m+, cmp);//sort函数是从0到n-1的!!!!!!!!!!!!!!!
  70. kruskal();
  71. printf("%d\n", ans[c-].d);
  72. printf("%d\n", c-);
  73. for(int i = ; i < c; i++)
  74. {
  75. printf("%d %d\n", ans[i].x, ans[i].y);
  76. }
  77. return ;
  78. }

【玛德!!!!!!!!!!!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. WLC-生成CSR操作

    1.生成CSR [req]req_extensions = v3_req[ v3_req ]# Extensions to add to a certificate requestbasicConst ...

  2. JAVA 通过POI 模版,导出excel

    如有不足,欢迎指正,谢谢 ! 1.Maven引入  POI jar包.模版和结果文件.rar下载 <dependency> <groupId>org.apache.poi< ...

  3. 20200213springboot日记

    ------------恢复内容开始------------ ------------恢复内容开始------------ ------------恢复内容开始------------ 数据库管理 L ...

  4. vue.js 第十课-第十六课

    第十课: http://note.youdao.com/noteshare?id=25b5ba45286464856f21eb4b6b391ecd&sub=19C4429995384F72BD ...

  5. Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter

    1.DateTimeFormatter final修饰,线程安全,用于打印和解析日期-时间对象的格式化程序. 创建DateTimeFormatter: DateTimeFormatter dateTi ...

  6. IP地址,子网掩码,网段表示法,默认网关,DNS服务器详解,DNS域名设计

    本文参考:<计算机网络: IP地址,子网掩码,网段表示法,默认网关,DNS服务器详解> IP地址 概述 计算机要实现网络通信,就必须要有一个用于快速定位的网络地址.IP地址就是计算机在网络 ...

  7. 数据库先系统与原理第三章笔记:数据库SQL查询语言

    SQL概述 SQL发展 SQL特点 SQL查询基本概念 单表查询 投影查询 1.查询指定列: SELECT 列名1,列名2,列名3,.....FROM Table_Name; #查询全部列:SELEC ...

  8. springboot 模板

    参考:https://blog.csdn.net/wangb_java/article/details/71775637

  9. 建设基于TensorFlow的深度学习环境

    一.使用yum安装git 1.查看系统是否已经安装git git --version 2.yum 安装git yum install git 3.安装成功 git --version 4.进入指定目录 ...

  10. android.view.WindowManager$BadTokenException 崩掉

    问题: 以前的项目,今天打开运行,Activity刚打开的时候,点开一个弹窗是好的,但是再点到另一个界面的时候,返回,再点弹窗就崩了. 解决: 网上查了一下,发现出现这个问题的还特别多,大体如下: 1 ...