题意:连通各点最短距离,最小生成树。
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

思路:两种算法kruskal算法还有prime。

方法一:kruskal算法

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
int u,v,w;
};
int n,m,pre[55];
node g[3000];
int init()
{
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&g[i].u,&g[i].v,&g[i].w);
}
}
bool cmp(node a,node b)
{
return a.w<b.w;
}
int find(int x)
{
return x==pre[x]?x:find(pre[x]);
}
void krus()
{
int sum=0;
for(int i=1;i<=n;i++)
pre[i]=i;
sort(g+1,g+1+m,cmp);
for(int i=1;i<=m;i++)
{
int tx=find(g[i].u);
int ty=find(g[i].v);
if(tx!=ty)
{
pre[tx]=ty;
sum+=g[i].w;
}
}
printf("%d\n",sum);
}
int main()
{
while(~scanf("%d",&n)&&n)
{
init();
krus();
}
}

F - F(最小生成树)的更多相关文章

  1. 请教下 f = f.replace('\n', '\r')这条没起作用

    !/usr/bin/env python -- coding: utf-8 -- import json import string import sys reload(sys) sys.setdef ...

  2. python中F/f表达式优于format()表达式

    F/f表达式可以解析任意类型的数据 具体实现,看下面示例: 1.解析变量 1 a = 10 3 b = 20 5 res1 = F"a+b的值:{a+b}" 7 print(res ...

  3. F - F HDU - 1173(二维化一维-思维)

    F - F HDU - 1173 一个邮递员每次只能从邮局拿走一封信送信.在一个二维的直角坐标系中,邮递员只能朝四个方向移动,正北.正东.正南.正西. 有n个需要收信的地址,现在需要你帮助找到一个地方 ...

  4. 前序遍历 排序 二叉搜索树 递归函数的数学定义 return 递归函数不能定义为内联函数 f(x0)由f(f(x0))决定

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  5. Python格式化字符串(f,F,format,%)

    # 格式化字符串: 在字符串前加上 f 或者 F 使用 {变量名} 的形式来使用变量名的值 year = 2020 event = 'Referendum' value = f'Results of ...

  6. 【Wannafly挑战赛4】F 线路规划 倍增+Kruskal+归并

    [Wannafly挑战赛4]F 线路规划 题目描述 Q国的监察院是一个神秘的组织.这个组织掌握了整个帝国的地下力量,监察着Q国的每一个人.监察院一共有N个成员,每一个成员都有且仅有1个直接上司,而他只 ...

  7. 如果你也会C#,那不妨了解下F#(6):面向对象编程之“类”

    前言 面向对象的思想已经非常成熟,而使用C#的程序员对面向对象也是非常熟悉,所以我就不对面向对象进行介绍了,在这篇文章中将只会介绍面向对象在F#中的使用. F#是支持面向对象的函数式编程语言,所以你用 ...

  8. 如果你也会C#,那不妨了解下F#(4):了解函数及常用函数

    函数式编程其实就是按照数学上的函数运算思想来实现计算机上的运算.虽然我们不需要深入了解数学函数的知识,但应该清楚函数式编程的基础是来自于数学. 例如数学函数\(f(x) = x^2+x\),并没有指定 ...

  9. HDU 1005 F(Contest #1)

    题意: 已知f[1] = f[2] = 1,输入三个数a,b,n,求f[n] = (a*f[n-1]+b*f[n-2])%7的结果 分析: f[n-1]和f[n-2]最多为7种情况(0,1,2,3,4 ...

随机推荐

  1. 哎,这让人抠脑壳的 LFU。

    这是why哥的第 83 篇原创文章 让人抠脑壳的 LFU 前几天在某APP看到了这样的一个讨论: 看到一个有点意思的评论: LFU 是真的难,脑壳都给我抠疼了. 如果说 LRU 是 Easy 模式的话 ...

  2. 醒醒!Python已经支持中文变量名啦!

    最近,我在翻阅两本比较新的 Python 书籍时,发现它们都犯了一个严重的低级错误! 这两本书分别是<Python编程:从入门到实践>和<父与子的编程之旅>,它们都是畅销书,都 ...

  3. uber_go_guide解析(二)

    前言 接上回 正文 错误消息 Go中声明错误有几种方式 errors.New() 简单的声明静态字符串信息的错误 fmt.Errorf 可以格式化插入信息的错误 自己实现 Error() 方法 使用e ...

  4. MyISAM与InnoDB两者之间区别与选择(转)

    Mysql在V5.1之前默认存储引擎是MyISAM:在此之后默认存储引擎是InnoDB MyISAM:默认表类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Acces ...

  5. zabbix客户端安装配置

    1.下载,解压并安装zabbixtar zxvf zabbix-2.0.12.tar.gzcd zabbix-2.0.12./configure --prefix=/usr/local/zabbix ...

  6. SAP RFC的相关的术语说明

    工作比较忙,很少有时间写点文章,抽空写点吧,给需要的人看看,虽然徒弟很多了,不过还是不要固步自封,在这里也指导更多的人进步吧. RFC(Remote Function Call)是SAP系统和其他(S ...

  7. 单线程的as-if-serial语义

    单线程的as-if-serial语义 关于指令重排序有个问题不明白的一个问题 int a = 2; int c = 1 + a; float b = 3f / 2f; 举个栗子,从CPU的设计者以及编 ...

  8. inode占满导致No space left on device inode快速解决方法

    暂未发现其他比我这个更快的方法. 因为其他方法会展示那个非常卡的目录,导致效率极低.而我这个方法不会去展示那个目录. 查找占用的目录 find / -type d -size +1M -maxdept ...

  9. Flask的配置文件加载两种方式

    配置文件 1 基于全局变量 2 基于类的方式 配置文件的加载需要将配合文件的相对路径添加到app.config.from_object("文件路径"),类的方式也是一样,需要将类的 ...

  10. [APUE] 进程环境

    APUE 一书的第七章学习笔记. 进程终止 有 8 种方式可以使得进程终止,5 种为正常方式: Return from main Calling exit() Calling _exit or _Ex ...