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,从第一个点出 ...
随机推荐
- iSCSI 原理和基础使用
终于完成最后一篇了,一上午的时间就过去了. 下文主要是对基本操作和我对iSCSI的理解,网上有很多iSCSI原理,在这里我就不写了,请自行学习. 这篇文章仅对iSCSI的很多误解做一次梳理,你必须对所 ...
- Math Show CodeForces - 846B
题目 题意: 有n个任务,每个任务有k个子任务,有m的时间来完成任务.每个任务的第i个子任务需要时间都是ti.完成一个子任务获得一分,完成一个大任务的所有子任务额外得一分.问如何在时间不超过m的情况下 ...
- 人品问题 树形dp
题目 网上出现了一种高科技产品——人品测试器.只要你把你的真实姓名输入进去,系统将自动输出你的人品指数.把儿不相信自己的人品为0.经过了许多研究后,把儿得出了一个更为科学的人品计算方法.这种方法的理论 ...
- Linux、Windows 下分割、合并rar文件
1.分割rar 1.1 linux下分割压缩rar 安装rar和unrar和序 $sudo aptitude install rar unrar 示例,分割压缩temp文件,每个包为1MB $rar ...
- Java 修改编码格式的几种方式
1.工作空间 workspase Window→Preferences→General→Workspace→Text file encoding→other→UTF-8 2.项目编码格式 右键项目名→ ...
- codeblock 16.01
1.不希望for (int i = 1; i <= n; ++i) { } 中间按下去后,自动缩进到中间,只缩进到下一行. 可以去掉其中一个. 2.不自动完成,就是出现一个括号后不补充另一个. ...
- dangerouslySetHTML 和 style 属性
这一节我们来补充两个之前没有提到的属性,但是在 React.js 组件开发中也非常常用,但是它们也很简单. dangerouslySetHTML 出于安全考虑的原因(XSS 攻击),在 React.j ...
- AJPFX总结String类的特点
String str = "abc"; str就是String的一个对象 字符串一旦被赋值, 值就不能再被改变了 举例:String s ...
- Ubuntu16下查看CPU、内存和磁盘相关信息
1.内存 查看内存#free -m total used free shared buff/cache available Mem: Swap: 2.CPU 查看逻辑cpu个数: #cat /proc ...
- 原生js的容易忽略的相似点(二)
1.new Object 和字面量 {}测试; <script type="text/javascript"> //1.new出来对象 console.log(obj, ...