POJ 1287 Networking (最小生成树模板题)
Description
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 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
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
求最小生成树基本思想
- 定义结构体保存两节点及其距离
- 对结构体排序(按两节点距离从小到大)
- 对边的数量进行查询,若两节点父节点不同则连接两父节点,记录边的大小sum及有效边的数量k
- 在循环中判断有效边数量,若等于节点数减一则结束循环
- 判断有效边数量若等于节点数减一,则能连接所有节点输出值,否则不能
#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 (最小生成树模板题)的更多相关文章
- POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...
- POJ 1287 Networking (最小生成树)
Networking Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit S ...
- [kuangbin带你飞]专题六 最小生成树 POJ 1287 Networking
最小生成树模板题 跑一次kruskal就可以了 /* *********************************************** Author :Sun Yuefeng Creat ...
- ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法
题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds ...
- POJ.1287 Networking (Prim)
POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- POJ 1287 Networking【kruskal模板题】
传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include< ...
- 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 ...
- 最小生成树模板题POJ - 1287-prim+kruskal
POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出 ...
随机推荐
- 1392:繁忙的都市(city)
[题目描述] 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有 ...
- gvim 常用键
按i 切换到插入模式,ESC退出 命令模式下 dd 删除一行 dw 删除一个字 x 删除一个字符 :set num 设置行号 :reg 查看寄存器 p 粘贴 y 复制 "*p 不同环境下 ...
- Histogram LightOJ - 1083
Histogram LightOJ - 1083 题意:给出一个直方图,由n个长条组成,它们的x轴上坐标分别为1-n,读入n之后读入的一行中,第i个表示x轴上坐标为i的长条长度.求直方图最大的正方形面 ...
- 152 Maximum Product Subarray 乘积最大子序列
找出一个序列中乘积最大的连续子序列(该序列至少包含一个数).例如, 给定序列 [2,3,-2,4],其中乘积最大的子序列为 [2,3] 其乘积为 6.详见:https://leetcode.com/p ...
- 为页面添加favicon
<link rel="shortcut icon" href="favicon.ico" /> 还有另一种写法,但是IE对它的支持不够好: < ...
- Ionic之ui-sref引入图片,图片部分挡住解决方案
ionic图片设置大小跟图片像素相同,但是使用ui-sref="parkInfo"上半部分图片会挡住,增加height的高度,就可以显示原本图片 页面: <ion-conte ...
- 【转】java序列化一定要应该注意的6个事项!
1.如果子类实现Serializable接口而父类未实现时,父类不会被序列化,但此时父类必须有个无参构造方法,否则会抛InvalidClassException异常. 2.静态变量不会被序列化,那是类 ...
- STM32 modbus CRC16校验
typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef int int32_t; const uint16_t ...
- How the performance impacts your revenue-性能影响营收
看完国外一个APM厂商最后的一个业务介绍视频,终于想通了PE领域中最顶层的应用目标,也就是如标题所云.那么这个影响效果是如何做到的?最终的步骤其实很简单,也就是利用大数据进行分析.而自己先前还没有想到 ...
- 一条陌生的出路【过往d心声】
一条陌生的出路 Vashon的心声 人生就像一列车,车上总有形形色色的人穿梭往来.你也可能会在车上遇到很多你以为有缘分的人,但是车也会有停下来的时候,总会有人从人生这列车上上下下,当你下去的时候你挥挥 ...