Networking

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

Appoint description: 
System Crawler  (2015-06-02)

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. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <queue>
  5. #include <vector>
  6. #include <map>
  7. #include <algorithm>
  8. #include <cstring>
  9. #include <cctype>
  10. #include <cstdlib>
  11. #include <cmath>
  12. #include <ctime>
  13. using namespace std;
  14.  
  15. const int SIZE = ;
  16. int N,M;
  17. int FATHER[SIZE];
  18. int MAP[SIZE][SIZE];
  19. struct Node
  20. {
  21. int from,to,cost;
  22. }G[SIZE];
  23.  
  24. void ini(void);
  25. int find_father(int);
  26. void unite(int,int);
  27. bool same(int,int);
  28. bool comp(const Node &,const Node &);
  29. int kruskal(void);
  30. int main(void)
  31. {
  32. int from,to,cost;
  33.  
  34. while(~scanf("%d",&N))
  35. {
  36. if(!N)
  37. break;
  38. scanf("%d",&M);
  39. ini();
  40. for(int i = ;i < M;i ++)
  41. scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);
  42. sort(G,G + M,comp);
  43. printf("%d\n",kruskal());
  44. }
  45.  
  46. return ;
  47. }
  48.  
  49. void ini(void)
  50. {
  51. fill(&MAP[][],&MAP[SIZE - ][SIZE - ],-);
  52. for(int i = ;i <= N;i ++)
  53. FATHER[i] = i;
  54. }
  55.  
  56. int find_father(int n)
  57. {
  58. if(n == FATHER[n])
  59. return n;
  60. return FATHER[n] = find_father(FATHER[n]);
  61. }
  62.  
  63. void unite(int x,int y)
  64. {
  65. x = find_father(x);
  66. y = find_father(y);
  67.  
  68. if(x == y)
  69. return ;
  70. FATHER[x] = y;
  71. }
  72.  
  73. bool same(int x,int y)
  74. {
  75. return find_father(x) == find_father(y);
  76. }
  77.  
  78. int kruskal(void)
  79. {
  80. int ans = ,count = ;
  81.  
  82. for(int i = ;i < M;i ++)
  83. if(!same(G[i].from,G[i].to))
  84. {
  85. unite(G[i].from,G[i].to);
  86. ans += G[i].cost;
  87. count ++;
  88.  
  89. if(count == N - )
  90. break;
  91. }
  92. return ans;
  93. }
  94.  
  95. bool comp(const Node & a,const Node & b)
  96. {
  97. return a.cost < b.cost;
  98. }

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

  1. POJ 1287 Networking (最小生成树模板题)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

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

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

  3. POJ.1287 Networking (Prim)

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

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

    Networking 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/B Description You are assigned ...

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

    题意  给你n个点 m条边  求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...

  6. poj 1287 Networking【最小生成树prime】

    Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7321   Accepted: 3977 Descri ...

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

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

  8. POJ - 1287 Networking 【最小生成树Kruskal】

    Networking Description You are assigned to design network connections between certain points in a wi ...

  9. POJ 1287 Networking(最小生成树裸题有重边)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

随机推荐

  1. Unity3d:使用uWebKit插件嵌入网页,网页中的flv视频无法播放

    问题描述:unity3d程序,使用uWebKit插件嵌入网页,用来播放FLV视频,有的电脑可以正常播放,有的电脑在网页中播放不了ps:网页中的播放器用的是player.swf解决方案:是由于网页中的播 ...

  2. maven 基础整理

    教程 依赖管理 IDE设置121 IntelliJ,Edit Configurations中添加maven,选中 Resolve Workspace artifacts能自动编译依赖模块 内置命令 m ...

  3. 第三章TP-Link 703N OpenWrt设置网络

    默认情况下不开启wifi,另外需要连接到网络来安装软件,所以需要修正配置文件. 可以用vi修改相关配置(不会用vim的同学悲剧了). 首先修改/etc/config/wireless文件,注释掉 # ...

  4. js 如何验证字符串里是否包含汉字?

    1.用正则表达式判断<input  type="text" id="name" placeholder="请输入用户名" value= ...

  5. Chrome的JS调试工具

    你是怎么调试 JavaScript 程序的?最原始的方法是用 alert() 在页面上打印内容,稍微改进一点的方法是用 console.log() 在 JavaScript 控制台上输出内容.嗯~,用 ...

  6. 【转】Android 属性动画(Property Animation) 完全解析 (上)

    http://blog.csdn.net/lmj623565791/article/details/38067475 1.概述 Android提供了几种动画类型:View Animation .Dra ...

  7. ASP.NET加载主题和皮肤样式的各种方式

    一.加载主题(皮肤.样式表)的多种方式 除了在页面指令中采用Theme或者StylesheetTheme为单个页面加载主题外,还可以通过配置文件为多个页面批量加载主题,另外,还可以通过改变页面的The ...

  8. pjsip视频通信开发(上层应用)之EditText重写

    我们经常使用手机的打电话功能,当我们按键盘的时候,有一个地方显示我们按键的内容,当我们的手点击那个地方的时候,并没有弹出软件盘,所以我们再有数字键盘的时候,要屏蔽系统的软件盘. 我们分析一下,软件盘弹 ...

  9. 今天弱爆了,svn创建项目

    今天弱爆了 1.再svnRoot下新建你要建的项目名如:hqdj  文件夹,然后选中它点击右键选中create repository here... ,选择文件系统类型 2.进入conf文件夹进行配置 ...

  10. 谷歌技术&quot;三宝&quot;之MapReduce

    江湖传说永流传:谷歌技术有"三宝",GFS.MapReduce和大表(BigTable)! 谷歌在03到06年间连续发表了三篇非常有影响力的文章,各自是03年SOSP的GFS,04 ...