POJ1751 Highways(kru)

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: 11163 Accepted: 3174 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



给了n个城市坐标,m条已经建好的高速路,问你还需要建多少条路使得城市相连,要求加入边的总和最小。


edge数组开成[MAXN << 1]就MLE,[MAXN * MAXN]就ac,不知何故。。


AC代码:


#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 755;
struct Point
{
	/* data */
	int x, y;
}point[MAXN];
struct Edge
{
	/* data */
	int a, b;
}edge[MAXN * MAXN];
int n, m, par[MAXN];
int dis(Point a, Point b)
{
	return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
bool cmp(Edge a, Edge b)
{
	return dis(point[a.a], point[a.b]) < dis(point[b.a], point[b.b]);
}
int find(int x)
{
	if(x == par[x]) return x;
	return par[x] = find(par[x]);
}
void merge(int x, int y)
{
	par[find(x)] = find(y);
}
void kru()
{
	for(int i = 0; i < m; ++i)
		if(find(edge[i].a) != find(edge[i].b)) {
			int x = edge[i].a, y = edge[i].b;
			merge(x, y);
			printf("%d %d\n", x + 1, y + 1);
		}
}
int main(int argc, char const *argv[])
{
	scanf("%d", &n);
	for(int i = 0; i < n; ++i) {
		scanf("%d%d", &point[i].x, &point[i].y);
		par[i] = i;
	}
	scanf("%d", &m);
	for(int i = 0; i < m; ++i) {
		int x, y;
		scanf("%d%d", &x, &y);
		x--, y--;
		merge(x, y);
	}
	m = 0;
	for(int i = 0; i < n - 1; ++i)
		for(int j = i + 1; j < n; ++j) {
			edge[m].a = i;
			edge[m].b = j;
			m++;;
		}
	sort(edge, edge + m, cmp);
	kru();
	return 0;
}


告别信号干扰!用Altium Designer 19搞定4层/6层PCB叠层设计的保姆级流程 本文详细介绍了如何使用Altium Designer 19进行4层/6层PCB叠层设计,解决信号干扰问题。通过实际案例和系统方法论,涵盖高速数字电路、混合信号系统等典型场景的设计优化,帮助硬件工程师和学生掌握电磁兼容(EMC)与信号完整性(SI)的核心技术。 阅读详情

相关推荐

MATLAB控制信号发生器(USB-GPIB-HS)GPIB仪器控制教程(含matlab代码)

自动化测试时需要自动产生信号,通过matlab控制信号发生器的频率与功率,以实现自动化信号生成输出

qq_43686057的博客 1617

如何对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

世界 8869

Windows环境下DataEase一站式安装指南(含WSL2+Docker配置)

本文详细介绍了在Windows环境下通过WSL2和Docker快速部署DataEase的完整教程。从WSL2配置、Docker环境搭建到DataEase离线安装,逐步指导用户完成一站式部署,特别适合需要快速搭建数据分析平台的开发者。教程包含常见问题解决方案和性能优化技巧,帮助用户高效使用这款国产开源数据可视化工具。

weixin_42526166的博客 403

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的专栏 1553

有源滤波电路设计

目录一、低通滤波器1、同相输入低通滤波器(1)一阶低通滤波电路(2)二阶低通滤波电路(压控电压源;Sallen-Key)2、反相输入低通滤波器(1)一阶低通滤波电路(2)二阶低通滤波电路(无限增益多路反馈)二、高通滤波器1、同相输入高通滤波器(1)一阶高通滤波电路(2)二阶高通滤波电路(压控电压源;Sallen-Key)2、反相输入高通滤波电路(1)二阶高通滤波电路(无限增益多路反馈) 一、低通滤波器 1、同相输入低通滤波器 (1)一阶低通滤波电路 即在一阶无源低通滤波器的基础上,加了一个集成运放。 它的

qq_41873236的博客 3万+

深入理解DQN中的Q值:核心概念和应用

在强化学习中,Q值(或称为动作价值函数)是核心概念之一。Q值函数 ( Q(s, a) ) 表示在给定状态 ( s ) 下,采取动作 ( a ) 并遵循某策略后,智能体从当前状态到未来状态所能获得的预期总回报。简言之,Q值是评估特定状态和动作对的好坏的指标。

qlkaicx的博客 2988

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

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

聚精会神搞建设 4724

Markdown编辑器介绍

这篇文章介绍了Markdown编辑器的功能和使用方法。主要内容包括:Markdown的基本语法如标题、列表、链接插入等;新增功能如图片拖拽、数学公式支持、甘特图和UML图表绘制;常用快捷键说明;以及文本导出导入操作。文章详细说明了如何通过Markdown语法实现文本格式化、代码高亮、表格创建等功能,并提供了多种图表绘制的语法示例,帮助用户更好地利用Markdown编辑器进行写作和内容创作。

soso1968的博客 1185

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 2449

国产系统迁移实战:在银河麒麟V10上搞定Wine32位支持,让老Windows软件跑起来

本文详细介绍了在银河麒麟V10操作系统上构建Wine32位支持的全过程,帮助用户解决Windows应用迁移难题。从系统架构差异分析到Wine环境配置,再到实战应用部署与优化,提供了一套完整的解决方案,确保老Windows软件在国产系统上稳定运行。

weixin_32288959的博客 663

[Dify实战] 招聘筛简历机器人:自动筛选、排序、生成面试要点-带绑定资源

摘要:本文介绍了如何利用Dify优化简历筛选流程,解决人工筛选耗时、标准不统一等问题。通过结构化输出"评分+亮点+风险+面试关注点",将主观判断转化为规则化流程。关键步骤包括:设计标准化Prompt、配置权重排序策略、确保公平合规性、与ATS系统对接,以及持续优化评估指标。该方法能显著提升筛选效率(缩短招聘周期)、增强一致性(避免主观偏差),并为面试官提供结构化参考信息,最终实现更高效精准的人才筛选。

技术小甜甜:传播可落地技术,帮助个人和企业降低解决真实场景问题的成本 585

memset()相关使用

1、memset函数及其用法 每种类型的变量都有各自的初始化方法,memset() 函数可以说是初始化内存的“万能函数”,通常为新申请的内存进行初始化工作。 它是直接操作内存空间,mem即“内存”(memory)的意思。该函数的原型为: # include <string.h> void *memset(void *s, int c, unsigned long n); 函数的功能是: 将指针变量 s 所指向的前 n 字节的内存单元用一个“整数” c 替换,注意 c 是 int 型。s 是

xuyin1204的博客 3万+

RT-Thread Studio 使用笔记(七)| 配合STM32CubeMX添加裸机驱动(以ADC为例)

1. 配置STM32CubeMX 2. 复制stm32xxxx_hal_msp.c文件 将 CubeMx 生成的代码 stm32f7xx_hal_msp.c 函数复制到 RT-Thread Studio 生成的工程中,并参与工程编译: 由于我们并没有使用 CubeMx 生成的工程,所以这里需要将 stm32f7xx_hal_msp.c 文件中 #include “main.h” 替换为 #i...

Mculover666的博客(嵌入式) 9638

Highways(p1751)

感受:搞了好久好久, 关键:最小生成树的路径, #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; double map[1000][1000]; struct my { dou

jiangjiashi的博客 788

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

memset初始化详解+变量初始化最大值、最小值

memset初始化详解+变量初始化最大值、最小值 计算机是使用二进制进行存储的,每一个二进制称为1位(bit),每8位称为1字节(Byte,B),1024字节称为1千字节(KB),1024千字节称为1兆字节(MB)。 换算关系: 1MB=1024KB 1KB=1024B 1B=8bit int大小为4字节,double大小为8字节,long long 大小为8字节,char大小为1字节。 mems...

_BOSS_的博客 1万+

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

memset初始化最大最小常用值总结

如果是int数组: int类型范围:-2147483648~2147483647. memset(a,0x7fffffff ,sizeof(a)),0x7fffffff 或 0x7FFFFFFF,全部初始化为int的最大值,即2147483647; memset(a,127,sizeof(a)),全部初始化为int的较大值,即2139062143; memset(a,0,sizeof(a)),全...

WELCOME!!! 2092

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

memset对于int、long long、float、double 的极值怎么清

int ”较“的原则:加法不爆。 极大值:0x7f 较大值:0x3f 较小值:0xc0 极小值:0x80 long long ”较“的原则:加法不爆。 极大值:0x7f 较大值:0x3f 较小值:0xc0 极小值:0x80 float ”较“的原则:保证一定位精度。 7f以上一直到be都是-0 (实际上是一个很小的>-1.0的负数) 极大值:0x7f 较大值:0x4f 较小值:0xce ...

WELCOME!!! 1542

关于memset的注意事项

1.memset只对int,bool型有效 2.只能赋值为0,-1

AI_xue 485

ZOJ 2048 Highways 最小生成树 Kruskal && Prim

Highways 最小生成树 Kruscal && Prim 给定n个城市,给定了已经连接起来的道路,输出的是城镇的编号, 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 cons

jianjun的专栏 1067

poj1751 最小生成树(Prim算法和Kruskal算法)

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 numb...

weixin_43965698的博客 424

图论trainning-part-1 F. Highways

F. Highways Time Limit: 1000ms Memory Limit: 10000KB 64-bit integer IO format:%lld Java class name:Main The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia ...

weixin_30439131的博客 375

zoj 2048 Highways(最小生成树))

Highways Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways.

小张的专栏 856

zoj - 2048 - Highways

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1048 题意:N个城镇之间已有M条高速公路,问还需建哪几条高速公路使所有的城镇都相通,并且路程最短。 ——>>这道题与zoj1203很是相像,不同的是这题已有互通的边。 计算任意两个town之间的距离并存起来,排序,并查并记录要连的边。处理输出顺序,在这里,可能前

无节操 1587

(POJ - 2485)Highways

(POJ - 2485)Highways The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem.

The wind of freedom blows 435

POJ - 2485 Highways(最小生成树 prime)

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible

沐雨风栉 257

Highways-高速公路(Prim算法)

Highways高速公路 目录 Highways高速公路 题意描述 解题思路 易错分析 AC代码 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 nu..

m0_58245389的博客 566
上一篇: CSU1217 奇数个的那个数(位运算)
下一篇: HDOJ1175 连连看(bfs)
GKHack
博客等级 码龄12年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值