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 0

2 3
1 2 37
2 1 17
1 2 68 3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32 5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12 0
#include<iostream>
#include<algorithm>
using namespace std;
int ans=,tot=;
const int N = 1e5;
int f[];
struct ac{
int v,u,w;
}edge[N];
bool cmp(ac a,ac b){
return a.w<b.w;
}
inline int find(int x){
if(x!=f[x])f[x]=find(f[x]);return f[x];
}
inline int join(int x,int y,int w){
int fx=find(x),fy=find(y);
if(fx!=fy){
f[fx]=fy;ans+=w;tot++;
}
}
int main()
{
int n;string a,b;int m,w,cnt=;
while(cin>>n>>m){
ans=tot=cnt=;
for(int i = ;i <=n;++i)f[i]=i;
for(int i = ;i < m;++i){
cin>>edge[i].u>>edge[i].v>>edge[i].w;
}
sort(edge,edge+m,cmp);
for(int i = ;i < m;++i){
join(edge[i].u,edge[i].v,edge[i].w);
if(tot==n-)break;
}
cout<<ans<<endl;
}
return ;
}

POJ - 1287 Networking (最小生成树&并查集的更多相关文章

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

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

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

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

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

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

  4. UVA 1395 苗条的生成树(最小生成树+并查集)

    苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...

  5. POJ.1287 Networking (Prim)

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

  6. CSP 201703-4 地铁修建【最小生成树+并查集】

    问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市 ...

  7. 关于最小生成树(并查集)prime和kruskal

    适合对并查集有一定理解的人.  新手可能看不懂吧.... 并查集简单点说就是将相关的2个数字联系起来 比如 房子                      1   2    3   4  5   6 ...

  8. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  9. poj 1182 食物链 (并查集)

    http://poj.org/problem?id=1182 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...

  10. POJ 1182 食物链(并查集拆点)

    [题目链接] http://poj.org/problem?id=1182 [题目大意] 草原上有三种物种,分别为A,B,C A吃B,B吃C,C吃A. 1 x y表示x和y是同类,2 x y表示x吃y ...

随机推荐

  1. 12、label控件

    label可以展示文本.超链接.图片.动图 新建项目Demo526,QMainWindow,勾选ui.将image文件夹(有2个图片)拷贝到项目路径下. [添加图像资源文件] 项目Demo526处,右 ...

  2. 修改 mvc webapi 默认返回 json 格式

    web api 默认的已 xml 格式返回数据 现在开发一般都是以 json 格式为主 下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=x ...

  3. JS基础-连续赋值

    重点:先声明,从左向右,声明变量分配内存,后赋值,从右向左 问题 var a = {n: 1}; var b = a; a.x = a = {n: 2}; console.log(a.x); cons ...

  4. windows基础提权

    Window基础提权 提到system权限 甚至让他变成你的肉鸡 我们了解一下windows下面有那些用户 Guests是用户最低的权限 而且一般是被禁用的 User权限也很低 连关机都不行 还有wi ...

  5. 如何限制修改IP地址;如何禁止显示的本地连接属性

    现在很多单位都配置了局域网,为了便于进行网络管理,同时为了提高的登录网络的速度,网管人员一般都为局域网中的每台电脑都指定了IP地址.但是在windows环境下其他用户很容易修改IP地址配置,这样就很容 ...

  6. 字符 kotlin(3)

    字符 用 Char 类型表示.它们不能直接当作数字) { // 错误:类型不兼容 // …… }} 字符字面值用单引号括起来: '1' . 特殊字符可以用反斜杠转义. 支持这几个转义序列: \t . ...

  7. BuiltIn库

    简介 作为一门表格语言,为了保持简单的结构,RF没有像别的高级语言那样提供类似ifelsewhile等内置关键字来实现各种逻辑功能(注1),而是提供给了用户BuiltIn库.如果用户想在测试用例中实现 ...

  8. leetcode 63 不同路径II

    二维数组动态规划,还可以采用一维数组进行动态规划. class Solution { public: int uniquePathsWithObstacles(vector<vector< ...

  9. linux下jmap,jstat和jstack使用

    刚好用到,转自http://blog.csdn.net/sinat_29581293/article/details/70214436 有空再整理: 先jps -ml 再sudo -u hive /u ...

  10. 阶段3 2.Spring_04.Spring的常用注解_4 由Component衍生的注解

    为什么要使用者三个注解 Controller:表现层 Service:业务层 Repository:持久层 在这里就是用Controller 运行也没问题 用Service Repository同样也 ...