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
求最小生成树基本思想
  1. 定义结构体保存两节点及其距离
  2. 对结构体排序(按两节点距离从小到大)
  3. 对边的数量进行查询,若两节点父节点不同则连接两父节点,记录边的大小sum及有效边的数量k
  4. 在循环中判断有效边数量,若等于节点数减一则结束循环
  5. 判断有效边数量若等于节点数减一,则能连接所有节点输出值,否则不能

 #include<cstdio>
#include<algorithm>
using namespace std;
int n,m,fa[],i,sum,k; struct stu
{
int from,to,al;
}st[]; bool cmp(stu a,stu b)
{
return a.al < b.al;
} int find(int a)
{
int r=a;
while(r!=fa[r])
{
r=fa[r];
}
return r;
} void init()
{
for(i = ; i <= n ;i++)
{
fa[i]=i;
}
} int judge(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx != yy)
{
fa[xx]=yy;
return ;
}
return ;
} int main()
{
while(scanf("%d",&n) && n)
{
init();
scanf("%d",&m);
for(i = ; i < m ; i++)
{
scanf("%d %d %d",&st[i].from,&st[i].to,&st[i].al); //定义结构体保存两节点及其距离
}
sort(st,st+m,cmp); //对结构体排序(按两节点距离从小到大)
int k = ;
int sum=;
for(i = ; k < n- ; i++) //对边的数量进行查询
{
if(judge(st[i].from,st[i].to)) //若两节点父节点不同则连接两父节点
{
k++; //
sum+=st[i].al;
} //在循环中判断有效边数量,若等于节点数减一则结束循环(写在循环里看k<n-1 )
}
printf("%d\n",sum);
}
}

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. hdu 2189 悼念512汶川大地震遇难同胞——来生一起走 基础母函数

    #include <iostream> #include <algorithm> #include <cstring> using namespace std; ] ...

  2. 一些CSS的备忘

    text-transform 文本转换 属性值是 none表示没有 不转换 同时也是默认的 capitalize 表示首字母大写 uppercase全部转换为大写 lowercase全部转为小写 te ...

  3. Caffe实战四(Caffe可视化方法)

    面对一堆文件,一行行的数据,确实很难理解深度学习相关的概念,好比训练的数据.构建的网络是怎样的?今天按照书中第16天的内容实践了一翻,终于看到了所谓的深度神经网络的模样.(参考:<深度学习 21 ...

  4. Hdu 1358 Period (KMP 求最小循环节)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...

  5. 喵哈哈村的魔法考试 Round #5 (Div.2) ABCC2

    官方题解:http://www.cnblogs.com/qscqesze/p/6516139.html 喵哈哈村的狼人杀大战(1) 描述 喵哈哈村最近热衷于玩一个叫做狼人杀的游戏! 张小田今天她抽到的 ...

  6. c8051单片机注意事项:

    一定要注意交叉开关问题:外设要想正确分配到指定引脚,一定要用配置工具确定分配到指定引脚:如果手动分配一定要仔细验证.这方面有个深刻的教训. 有个项目用c8051f020,用到2个串口,硬件已经确定好了 ...

  7. 通用maper无法获取实体类com.qmtt.model.PhWxUser对应的表名问题

    spring boot在采用了热加载后,可能会出现“无法获取实体类com.qmtt.model.PhWxUser对应的表名!”的异常, 解决办法 在resources新建一个文件夹META-INF,新 ...

  8. 让WPS10显示为offic97效果

    让WPS10显示为offic97效果2019/1/26 22:02 OS:win7 64位使用的WPS_10.1.0.5603_setup.1460689247.exe 衣不如旧,人不如新.最开始接触 ...

  9. Load average in Linux的精确含义

    Man 上的解释: load average System load averages is the average number of processes that are either in a ...

  10. C#创建任务计划

    因写的调用DiskPart程序是要用管理员身份运行的,这样每次开机检查都弹个框出来确认肯定不行.搜了下,似乎也只是使用任务计划程序运行来绕过UAC提升权限比较靠谱,网上的都是添加到计算机启动的,不是指 ...