Description

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. 
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line. 
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i. 

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

  1. 1 0
  2.  
  3. 2 3
  4. 1 2 37
  5. 2 1 17
  6. 1 2 68
  7.  
  8. 3 7
  9. 1 2 19
  10. 2 3 11
  11. 3 1 7
  12. 1 3 5
  13. 2 3 89
  14. 3 1 91
  15. 1 2 32
  16.  
  17. 5 7
  18. 1 2 5
  19. 2 3 7
  20. 2 4 8
  21. 4 5 11
  22. 3 5 10
  23. 1 5 6
  24. 4 2 12
  25.  
  26. 0

Sample Output

  1. 0
  2. 17
  3. 16
  4. 26
  1. 求最小生成树基本思想
  1. 定义结构体保存两节点及其距离
  2. 对结构体排序(按两节点距离从小到大)
  3. 对边的数量进行查询,若两节点父节点不同则连接两父节点,记录边的大小sum及有效边的数量k
  4. 在循环中判断有效边数量,若等于节点数减一则结束循环
  5. 判断有效边数量若等于节点数减一,则能连接所有节点输出值,否则不能
  1.  
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. int n,m,fa[],i,sum,k;
  5.  
  6. struct stu
  7. {
  8. int from,to,al;
  9. }st[];
  10.  
  11. bool cmp(stu a,stu b)
  12. {
  13. return a.al < b.al;
  14. }
  15.  
  16. int find(int a)
  17. {
  18. int r=a;
  19. while(r!=fa[r])
  20. {
  21. r=fa[r];
  22. }
  23. return r;
  24. }
  25.  
  26. void init()
  27. {
  28. for(i = ; i <= n ;i++)
  29. {
  30. fa[i]=i;
  31. }
  32. }
  33.  
  34. int judge(int x,int y)
  35. {
  36. int xx=find(x);
  37. int yy=find(y);
  38. if(xx != yy)
  39. {
  40. fa[xx]=yy;
  41. return ;
  42. }
  43. return ;
  44. }
  45.  
  46. int main()
  47. {
  48. while(scanf("%d",&n) && n)
  49. {
  50. init();
  51. scanf("%d",&m);
  52. for(i = ; i < m ; i++)
  53. {
  54. scanf("%d %d %d",&st[i].from,&st[i].to,&st[i].al); //定义结构体保存两节点及其距离
  55. }
  56. sort(st,st+m,cmp); //对结构体排序(按两节点距离从小到大)
  57. int k = ;
  58. int sum=;
  59. for(i = ; k < n- ; i++) //对边的数量进行查询
  60. {
  61. if(judge(st[i].from,st[i].to)) //若两节点父节点不同则连接两父节点
  62. {
  63. k++; //
  64. sum+=st[i].al;
  65. } //在循环中判断有效边数量,若等于节点数减一则结束循环(写在循环里看k<n-1 )
  66. }
  67. printf("%d\n",sum);
  68. }
  69. }

POJ 1287 Networking (最小生成树模板题)的更多相关文章

  1. POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...

  2. POJ 1287 Networking (最小生成树)

    Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit S ...

  3. [kuangbin带你飞]专题六 最小生成树 POJ 1287 Networking

    最小生成树模板题 跑一次kruskal就可以了 /* *********************************************** Author :Sun Yuefeng Creat ...

  4. ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

    题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds     ...

  5. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

  6. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  7. POJ 1287 Networking【kruskal模板题】

    传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include< ...

  8. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  9. 最小生成树模板题POJ - 1287-prim+kruskal

    POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出 ...

随机推荐

  1. Git如何克隆Gitlab?Git本地仓库如何上传Gitlab?

    首先确保本机已经安装上Git,其次确认可以正常访问Gitlab服务器 环境: Git:Centos 7.x  192.168.126.138 Gitlab: Centos7.x 192.168.126 ...

  2. easyUI Uncaught TypeError: Cannot read property 'length' of undefined

    dataGrid json 封装数据格式为 List<Object> 格式

  3. 使用Ctex中遇到的一些问题

    一般下载好Ctex,我是使用Latex+dvi2pdf完成编译的,但是发现推荐的使用为:1)运行CCT & Latex命令生成两次dvi和ps文件 2)使用dvi2pdf编译dvi文件生成pd ...

  4. LBP特征

    此篇摘取 <LBP特征原理及代码实现> <LBP特征 学习笔记> 另可参考实现: <LBP特征学习及实现> <LBP特征的实现及LBP+SVM分类> & ...

  5. 排序二叉树 HDOJ 5444 Elven Postman

    题目传送门 题意:给出线性排列的树,第一个数字是根节点,后面的数如果当前点小或相等往左走,否则往右走,查询一些点走的路径 分析:题意略晦涩,其实就是排序二叉树!1<<1000 普通数组开不 ...

  6. JavaScript--字符串常用方法总结

    JavaScript--字符串常用方法总结 举例模板: var str = "what are you " var str1 = "sss" var txt = ...

  7. EJB Timer Service is not available. Timers for application with id 95795415990861824 will not be deleted

    delete follows:glassfish\domains\domain1\applications\ejb-timer-service-appglassfish\domains\domain1 ...

  8. 【前端】模拟微信上传图片(带预览,支持预览gif)

    一.Html <style type="text/css"> #previewDiv{width:50px;height:50px;overflow:hidden;po ...

  9. AJPFX:求两个城市之间的距离

    键盘录入多个城市: 城市1,城市2,城市3  以 ### 结束输出然后再键盘录入各个城市之间的距离:  格式如下:0,12,4512,0,2245,22,0### 然后按照输入的两个城市,求得两个城市 ...

  10. CF782A Andryusha and Socks

    题意: Andryusha is an orderly boy and likes to keep things in their place. Today he faced a problem to ...