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 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

Sample Output

0
17
16
26
 #include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<math.h>
using namespace std; int fa[]; struct node
{
int u,v,c;
}a[]; bool cmp(node b,node d)
{
return b.c<d.c;
} int find(int x)
{
return fa[x]==x ? x:fa[x]=find(fa[x]);
} int main()
{ int n,m,ans;
while(~scanf("%d",&n)&&n)
{
ans=;
scanf("%d",&m);
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].c);
}
sort(a,a+m,cmp);
for(int i=;i<=n;i++)
fa[i]=i;
for(int i=;i<m;i++)
{
int p=find(a[i].u),q=find(a[i].v);
//因为排过序了所以有重边也是直接取小的
if(p!=q)
{
fa[p]=q;
ans+=a[i].c;
}
}
printf("%d\n",ans); }
return ;
}
//直接拿前面一篇博客改的代码

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 (最小生成树模板题)

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

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

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

  4. BZOJ 1083 [SCOI2005]繁忙的都市 (最小生成树裸题无重边) 超简单写法!!

    Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口 ...

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

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

  6. POJ.1287 Networking (Prim)

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

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

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

  8. HDU 1102 最小生成树裸题,kruskal,prim

    1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...

  9. POJ 3468 线段树裸题

    这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...

随机推荐

  1. js对象合并

    实现js对象大合并,ES6之前就只有循环遍历咯.可以用ES6的话可以用Object.assign(). 以下是Object.assign()示例: var o1 = { a: 1 }; var o2 ...

  2. PAT 1023 Have Fun with Numbers

    1023 Have Fun with Numbers (20 分)   Notice that the number 123456789 is a 9-digit number consisting ...

  3. [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)

    https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...

  4. maven plugins

    <build> <finalName>lessons</finalName> <plugins> <plugin> <groupId& ...

  5. h5的坑

    转自 http://www.mahaixiang.cn 解决各种坑 http://www.mahaixiang.cn/ydseo/1529.html

  6. Vmware tools install

    Vmware tools 1◆ 下载 2◆ diagram   Ifcfg-eth0   =====>关闭防火墙 systemctl stop firewalld.service   ===== ...

  7. xubuntu无法进图形界面问题

    http://www.ubuntugeek.com/fix-for-cant-login-after-upgrading-from-ubuntu-13-04-to-ubuntu-13-10.html ...

  8. learning scala output to console

    控制台输出语句: print println example: scala> print("i=");print(i)i=345scala> println(" ...

  9. js 小说格式整理

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  10. 遍历所有子物体中renderer(渲染器)中的material(材质)

    //得到所有可渲染的子物体Renderer[] rds = transform.GetComponentsInChildren<Renderer>();//逐一遍历他的子物体中的Rende ...