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. mysql使用sql语句根据经纬度计算距离排序

    CREATE TABLE `locationpoint` ( `id` int(11) NOT NULL, `province` varchar(20) NOT NULL, `city` varcha ...

  2. vue中使用transition标签底部导航闪烁问题

    <transition :name="transitionName" :duration="{ enter: 500, leave: 0 }" > ...

  3. 焦作网赛-G-欧拉降幂

    https://nanti.jisuanke.com/t/31716 答案就是2^(n-1)%mod ,n非常的大,由欧拉降幂公式    AB%C=AB%phi(C)+phi(C)%C  化简 2n- ...

  4. 数据结构与算法之PHP查找算法(二分查找)

    二分查找又称折半查找,只对有序的数组有效. 优点是比较次数少,查找速度快,平均性能好,占用系统内存较少: 缺点是要求待查表为有序表,且插入删除困难. 因此,折半查找方法适用于不经常变动而查找频繁的有序 ...

  5. 【LeetCode】数组移除元素

    链表等复杂数据结构用多了,简单的数组操作也不能遗忘! 1. 给定一个有序数组,移除所有重复元素并返回新的数组长度,不能分配额外数组的内存空间. e.g. 给定输入的数组 = [1,1,2],函数应当返 ...

  6. [luogu P1552] [APIO2012]派遣

    [luogu P1552] [APIO2012]派遣 题目背景 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿. 题目描述 在这个帮派里,有一名忍者被称之为Master.除 ...

  7. 《高性能SQL调优精要与案例解析》新书样章

    该书样章已上传,需要的同学可以通过如下地址下载:http://www.itpub.net/thread-2091839-1-1.html http://www.itpub.net/thread-209 ...

  8. win10输入法五笔设置

    win10 settings inputApp 1●安装五笔qq ignore / ign ɔ:   2● 操作步骤   3● 五笔设置  

  9. linux命令--文件查询

    ls [ -lahid ] [ /* ] ls  --   默认查询当前目录下的显性文件 -l  --  显示文件的详细信息 -a --  显示所有文件(包括隐藏文件) -h --  文件大小显示为 ...

  10. Win10系列:VC++媒体播放控制2

    (3)停止视频播放 接下来添加对视频文件播放的停止控制,打开MainPage.xaml文件,并在Grid元素中添加一个"停止"按钮,用于停止视频的播放,代码如下所示: <Bu ...