问题描述:

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.


样例输入:

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


样例输出:

0
17
16
26


思路分析:

题目大意:将所给出的点相互连通,求所需要的最小长度。

本题要先将各个点联通,只需应用最小生成树的算法就行了。


解决方案:

#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;
int f[1100],a[1100];
int n,m;
struct note
{int x,y,w;
}t[5200];
bool cmp(note m,note n)
{return m.w<n.w;
}
void init()
{for(int i=1;i<=n;i++)f[i]=i;
}int find(int v){if(f[v]==v)return v;elsereturn find(f[v]);}
int merge(int u,int v){int t1,t2;t1=find(u);t2=find(v);if(t1!=t2){f[t2]=t1;return 1;}return 0;}
int main ()
{int i,j;while(~scanf("%d",&n)){if(n==0)break;init();int ans=0;int sum=0;scanf("%d",&m);for(i=1;i<=m;i++){scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].w);}sort(t+1,t+m+1,cmp);for(i=1;i<=m;i++){if(merge(t[i].x,t[i].y)){ans++;sum=sum+t[i].w;}if(ans==n-1)break; }printf("%d\n",sum);}
} 

求电缆最小长度——最小生成树相关推荐

  1. 算法:通过普利姆(Prim)算法,求出图的最小生成树

    请看如下的示例图,该图有 V1-V7 七个顶点,每个顶点之间的距离如图所示: 如果上面的图为七个城市的地理分布图,城市间相连的边上的数字为城市间的距离.我们要在这七个城市里面架设电线,使得每一个城市都 ...

  2. poj 2914(stoer_wanger算法求全局最小割)

    题目链接:http://poj.org/problem?id=2914 思路:算法基于这样一个定理:对于任意s, t   V ∈ ,全局最小割或者等于原图的s-t 最小割,或者等于将原图进行 Cont ...

  3. 算法:通过克鲁斯卡尔(Kruskal)算法,求出图的最小生成树

    之前我给大家分享过用普利姆(Prim)算法来求出图的最小生成树(点我去看看),今天我再给大家分享一个也是求图的最小生成树的克鲁斯卡尔(Kruskal)算法 克鲁斯卡尔(Kruskal)算法,就相当于先 ...

  4. Java黑皮书课后题第2章:2.12(物理:求出跑道长度)编写程序,提示用户输入以米/秒为单位的速度v和加速度a,然后显示最短跑道长度

    2.12(物理:求出跑道长度)编写程序,提示用户输入以米/秒为单位的速度v和加速度a,然后显示最短跑道长度 题目 题目描述 运行示例 补充 代码块 题目 题目描述 2.12(物理:求出跑道长度) 假设 ...

  5. 求一个有限长度字符串 最长的有序可重复字符串长度

    求一个有限长度字符串 最长的有序可重复子串 package acm;/*** @author qxl*/ public class SequenceCharMax {/*** 输入一可重复的整数数组, ...

  6. 以太网帧的最小长度_802.3?以太网?看完你就懂了

    关注.星标公众号,不错过精彩内容 上一篇文章<以太网数据包结构>讲解了以太网数据包结构,其中牵扯到了802.3,以太网数据包等名词,本文将详解讲解一下这方面的内容. 在TCP/IP世界中, ...

  7. 枚举算法:最小连续n个合数。试求出最小的连续n个合数(其中n是键盘输入的任意正整数)。

    最小连续n个合数.试求出最小的连续n个合数(其中n是键盘输入的任意正整数). 思路: 判断素数合数,同时计数,然后数量满足n个的合数,得到其区间,输出. 流程图: 代码: #include<ti ...

  8. C语言求一个文件的长度,求二进制文件的长度

    求二进制文件的长度 请问c语言下,用fopen打开的文件怎么求文件的长度 if((fp=fopen("1.PMS","rb+"))==NULL) { print ...

  9. 任意给定一个正整数N,求一个最小的正整数M(M1),使得N*M的十进制表示形式里只含有1和0。...

    题目:任意给定一个正整数N,求一个最小的正整数M(M>1),使得N*M的十进制表示形式里只含有1和0. 解法一:暴力求解.从1开始查找M,然后判断M*N=X这个数字是否只含有0,1. 解法二:由 ...

最新文章

  1. 0.数据结构学习笔记大纲
  2. 一阶和二阶微分方程的物理意义???
  3. 理解SSL必须要理解的密码技术
  4. 举行可衡量自动行驶车未来发展的“智能模型车竞赛大会” 2018
  5. ADO.NET复习总结(4)--访问SqlServer的类
  6. Numpy:利用Numpy库建立可视化输入的二次函数数据点集np.linspace+np.random.shuffle+np.random.normal
  7. JS——样式类的添加
  8. 利用向量叉积求三角形的面积(+STL:nth_element求第K大的数)
  9. Lucene学习总结之二:Lucene的总体架构
  10. face alignment by 3000 fps系列学习总结(二)
  11. Yii2.0 ActiveForm Input Fields
  12. 【英语学习】【Level 08】U02 Movie Time L2 In black and white
  13. load()是python文件操作的函数_python基础总结(函数,文件操作)
  14. SAP License:SD应用要点
  15. nvl,空时的推断和取值
  16. 性能测试知多少----性能测试分类之我见
  17. 最新消息:原谷歌中国副院长刘骏任职人民搜索首席科学家
  18. MySQL 04 高级查询(二)
  19. 几种常用的服务器认证机制
  20. MAMP配置虚拟主机

热门文章

  1. mac os 触摸屏_为什么没有出现触摸屏Mac
  2. 企业微信加密消息体_企业微信机器人怎么发消息?企业微信机器人可以定时发消息吗?...
  3. 看完这篇文章APP关键词覆盖增加70000|互联网行业公会
  4. 智能车八邻域图像算法_二
  5. 盘点国内外在线app制作平台:分分钟做款app
  6. k8s的service端口暴露与代理
  7. R语言学习-复杂网络中心度计算
  8. MATLAB从入门到精通-Matlab读取fnl.grib2文件
  9. 学习打卡 2020/2/4
  10. LQBv13-Python:猜年龄