POJ 1751 - Highways(最小生成树)

memset用法 & int、long long、float、double 的极值怎么清 memset 是计算机中C/C++语言初始化函数。作用是将某一块内存中的内容全部(以字节为单位)设置为指定的值。在竞赛中,常用来进行数组的初始化。memset 函数原型定义如下: Copy正因为 memset 函数是以字节为单位赋值的,所以使用它并不能将数组元素修改为任意值,而竞赛中又常常需要将数组初始化为 0、最大值或最小值。写循环赋值的话,也不是不可以,不过显得不那么“优雅”,针对这一点,下面整理了使用 memset 实现数组初始化的一些常用方法。1、memset 可以对 字符数组 进行赋值吗?可以 阅读详情

Highways
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7371 Accepted: 2035 Special Judge

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length. 

Input

The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built. 

The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ith town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location. 

The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway. 

Output

Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space. 

If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty. 

Sample Input

9
1 5
0 0 
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2

Sample Output

1 6
3 7
4 9
5 7
8 3


==========================

用并查集做的


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

int n,m,start,end,par[1111],k,cnt=0;

struct point
{
    int x,y;
}p[1111];

struct edge
{
    int u,v;
    double w;
}e[1111111];

bool cmp(edge a,edge b)
{
    return a.w<b.w;
}

void makeset()
{
    for(int i=1;i<=n;i++) par[i]=i;
}

int findset(int x)
{
    if(x!=par[x]) par[x]=findset(par[x]);
    return par[x];
}

void unionset(int x,int y)
{
    x=findset(x);
    y=findset(y);
    if(x!=y) par[x]=y;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&p[i].x,&p[i].y);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            e[cnt].u=i;
            e[cnt].v=j;
            e[cnt].w=sqrt(1.0*(p[i].x-p[j].x)*(p[i].x-p[j].x)+1.0*(p[i].y-p[j].y)*(p[i].y-p[j].y));
            cnt++;
        }
    }
    sort(e,e+cnt,cmp);
    makeset();
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&start,&end);
        unionset(start,end);
    }
    for(int i=0;i<cnt;i++)
    {
        if(findset(e[i].u)!=findset(e[i].v))
        {
            unionset(e[i].u,e[i].v);
            printf("%d %d\n",e[i].u,e[i].v);
        }
    }
    return 0;
}


安装破解IAR EWARM 8.2,提供注册机 IAR Embedded Workbench for ARM(IAR for ARM)是一款微处理器的集成开发环境软件,该集成开发环境中包含了IAR的C/C++编译器、 汇编工具、链接器、库管理器、文本编辑器、工程管理器和C-SPY调试器,支持ARM,AVR,MSP430等芯片。   一、安装 双击运行安装程序,点击Install IAR Embedded Workbench for ARM... 阅读详情

相关推荐

基于C#的软PLC梯形图编辑器开发实战

本文详细介绍了基于C#开发软PLC梯形图编辑器的实战过程。文章从核心数据结构设计入手,阐述了如何利用C#的面向对象特性构建梯形图元素模型,并实现了图形编辑、指令表输入、逻辑连接拓扑重建等关键功能。重点剖析了模拟PLC执行引擎的扫描周期与能流传递算法,让梯形图逻辑在PC上“活”起来。最后探讨了定时器指令实现、性能优化及项目集成等进阶内容,为工业自动化软件集成提供了灵活高效的解决方案。

weixin_28314575的博客 404

如何对double型变量进行memset获得极大值或极小值

我们都知道,如果想对int型变量清极大值或极小值,我们一般选择memset(a,0x3f,sizeof a);或者memset(a,0xef,sizeof a);。然而,如果对double型清0x3f,我们经常会得到一个连1都不到的小数。那么对double清极值是否智能手动或者fill(a+1,a+n,0x7fffffff);呢? 首先我们运行以下程序 #include #incl

世界 8863

BUSCO评估基因组的完整性

因为数据库收集的时候找的就是在不同的物种间都是单拷贝的基因,所以说在评估的物种里面应该也是单拷贝的状态才对。基因组层面评估:先去基因组上预测这些基因,再去做评估,如果说给他是一个注释好的基因集,这个时候提供的是一个蛋白集合,它就直接是去进行同源搜索,再去评估,看BUSCO数据库里面的这些单拷贝基因有或者无;首先,tblastn也是同源搜索,同源搜索之后,去对转录本进行读码框预测,预测完的结果还是去进行hmmsearch的搜索(hmmsearch是做蛋白质结构域搜索的),最终拿到BUSCO评估结果。

qq_64400864的博客 2959

memset()函数用法

memset()用于对整型数组或者结构体进行清0或者字符型数组赋初值或某个特定值。而不可以给整型或者double类型数组赋值: 例如 1.对整型数组全体赋某一个值。结果:错误。   如果是对整型数组清0即memset(a,0,5*sizeof(int));改成那么是正确的。 int main(int argc, char **argv) {  int a[5] ={1,2,3,4,5}

CHF_VIP的专栏 1552

从SPI协议到文件系统:手把手教你用HAL库给W25Q16移植FATFS(附扇区管理优化方案)

本文详细介绍了如何在STM32平台上,通过HAL库的SPI接口为W25Q16 Flash芯片移植FATFS文件系统。文章从SPI通信基础讲起,深入解析了FATFS底层驱动(diskio.c)的移植要点,并重点探讨了针对W25Q16的4KB物理扇区特性进行扇区管理优化的两种方案,旨在帮助开发者构建稳定高效的文件存储方案。

happy2的博客 1043

Linux下Catapro酶动力学预测工具保姆级安装指南(PyTorch+CUDA11.8适配版)

本文提供了Linux系统下Catapro酶动力学预测工具的详细安装指南,特别适配PyTorch+CUDA11.8环境。从环境准备、源码获取到虚拟环境创建、依赖包安装和模型配置,逐步指导用户完成安装流程,并包含常见问题排查和性能优化建议,帮助研究者快速部署这一高效酶动力学参数预测工具。

weixin_29278805的博客 212

数组的初始化与memset的注意事项

如果全部初始化为0,可以int count[26]={0};这样初始化为26个0     如果int count[26]={2015};,则只有第一个按照给定值初始化,其余初始化为0     这种方法对于double数组同样适用     对char数组也适用,只是第一个元素按照给定值初始化外,其余的初始化为小写字母a     这种方法并不具有普适性,因为编译器的差异会导致不一样的结果,比如

聚精会神搞建设 4723

DeepLabv3+改进专栏

🔥【DeepLabv3+改进专栏!探索语义分割新高度】🌟 你是否在为图像分割的精度与效率发愁?📢 本专栏重磅推出:✅ 独家改进策略:融合注意力机制、轻量化设计与多尺度优化✅ 即插即用模块:ASPP+升级、解码器。

AIcurator的博客 2576

memset另类初始化测试 (二)——double

前一篇:http://blog.csdn.net/synapse7/article/details/9884135 试着对double数组进行初始化? 测试代码: #include #include int main(void) { double db[10]; for (int i = -4; i < 260; ++i)///已经覆盖了char的范围 { memset(

AC,∑ndless 2447

关于 Nginx 监听地址 0.0.0.0 的详细解释

0.0.0.0在Nginx配置中表示监听服务器所有网络接口(包括公网IP、内网IP和本地回环地址),这是默认配置。当使用listen 80;时,Nginx会接受通过服务器任何IP地址的80端口访问请求。相比只监听特定IP,这种配置更通用,但也意味着服务会对外网开放,需注意安全风险。可通过netstat命令验证监听状态,显示0.0.0.0:80即表示正确配置。

weixin_43674738的博客 1566

置顶必读(1) |《YOLOv8实战:从入门到深度优化》专栏导读,带你一文了解本专栏!

🏆 本文收录于 《YOLOv8实战:从入门到深度优化》 专栏,该专栏专注于分享我在YOLOv8目标检测模型中的实战经验,涵盖从入门基础到深度优化的全方位解决方案。无论你是计算机视觉的新手,还是具有多年深度学习经验的专家,本专栏都将为你提供高效、可复现的技术方案,助你掌握YOLOv8的核心技术,提升目标检测性能,突破瓶颈,优化模型,并加速项目开发🚀!

**My Coding Family** 14万+

POJ 1751 Highways 最小生成树

POJ 1751 Highways 最小生成树  题意:给你n个村庄的坐标,然后再给你m行,每行的两个数字代表公路两端的村庄编号(1~n),问在花费最少的情况下还需建哪些路才能使所有村庄连通。  思路:kruskal的话思路很明确,先建好边集,然后将每条已经建好的路执行合并操作,即Add(a,b);,然后再用kruskal输出一遍每次建边的两个村庄编号就OK。prim的话,没想到该怎么写,网上很多题

LucienShui 574

k8s篇之underlay网络和overlay区别

underlay和overlay

小橙子的笔记屋 2142

poj1751 highways 最小生成树记录路径

题目链接:http://poj.org/problem?id=1751 题目大意:给出一些点,代表一个地方的坐标,还有给出一些已经修好的路,问还要修那几条,使得修的路的和长度最小。 分析:这道题,很明显最小生成树啊,可是有的地方已经修了,那也是最小生成树,那就在初始化完成后,让这两个地方距离为0就可以了,然后还需要记录路径。我开始是用kruskal做的,原因嘛,因为这个算法是加边的嘛,而pri

Na_OH的博客 706

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

(最小生成树)POJ1751Highways

POJ1751Highways 题意&思路: 给你n个城市的坐标,m组已经连通的城市,使两两城市连通的需要修的道路的总和最小,输出修建公路的两城市的坐标。 代码: #include<iostream> #include<stdio.h> #include<stdlib.h> #include<algorithm> #include<st...

Z7784562的博客 308

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

POJ 1751 Highways 最小生成树

一、内容 The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a...

Tizzi的博客 896

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Highways POJ - 1751(最小生成树+剪枝)

题目链接 大意:给你n个城市的坐标,再给你m条已经连起来的路,问你还有那些城市需要连起来并且路长最小。 思路:一眼就可以看出是最小生成树,但直接提交会超时,需要剪枝,这题剪枝的技巧在其他最小生成树的题也可以使用(ps:参考博客) 在这里插入代码片 ...

qq_43697256的博客 454

POJ 1751:Highways(最小生成树)

http://poj.org/problem?id=1751 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of th...

231

最小生成树 - Highways - POJ - 1751

最小生成树

奥里给 176

poj-1751 Highways 最小生成树

http://poj.org/problem?id=1751 Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13180   Accepted: 3813   Special Judge Description The isl

wudi_00的博客 262

POJ 1751 Highways 【最小生成树 Kruskal】

Highways Time Limit:1000MS Memory Limit:10000K Total Submissions:23070 Accepted:6760 Special Judge Description The island nation of Flatopia is perfectly flat. Unfort...

NoobPlayer_llke的博客 222

Highways POJ-1751 最小生成树 Prim算法

Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输出需要添加边的两端点编号即可。 解题思路 这个可以使用最短路里面的Prim算法来实现,对于已经连接的城市,处理方式是令这两个城市之间的距离等于0即可。 prim算法可以实现我们具体的路径输出...

weixin_30715523的博客 781

POJ1751 Highways——————最小生成树

Highways Language:DefaultHighways Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 20875Accepted: 6166Special JudgeDescriptionThe island nation of Flatopia is perfectly flat. Unfortunately, ...

鸿杰 的博客 303

【最小生成树专题】POJ 1751 Highways

 POJ 1751 Highways Highways 题目链接-&gt;http://poj.org/problem?id=1751 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18416   Accepted: 5415   Special Judge Descrip...

荷叶田田 413

【题解】Highways POJ - 1751 ⭐⭐⭐ 【最小生成树】

Highways POJ - 1751 The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has alread...

Suprit's blog 908

算法学习 POJ 1751 highways 最小生成树

题目: Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has alread

PiscesCrab的博客 302
上一篇: UVA 10006 - Carmichael Numbers(快速幂取模)
下一篇: POJ 1789 - Truck History(最小生成树)
coco430
博客等级 码龄14年 9粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值