跳到正文

题解

题解:P13427 [COCI 2020/2021 #2] Odasiljaci

#SUSPENDED#约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月11日
篇幅
约 3 分钟
题目
P13427

版本

  1. v1当前

P13427

由题可得,因为是要找答案满足单调性,又是判断联通块,所以不难想到用二分答案+判断连通块解决。

但是

因为蒟蒻发现已经有二分答案的题解了。于是,我又想到了用最小生成树处理。又鉴于楼上的dalao们没有用prim算法解决的,于是蒟蒻在此提供一份最小生成树prim的题解。

C++
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>

using namespace std;

const int N = 1e3 + 10;

struct node {
	double x, y; //记录每一个天线的位置
};

int n;
node a[N];
bool vis[N];
double dis[N];
double ans;

void prim() {  //prim算法
  // 初始化  注意:dis数组不能用memset,因为dis是double类型数组,memset中的0x3f在double中存储会出问题
	for (int i = 1; i <= n; i++) dis[i] = 1e9;
	priority_queue<pair<double, int>, vector<pair<double, int> >, greater<pair<double, int> > > pq;
// 推入起点
	pq.push({0, 1});
	dis[1] = 0;
	int cnt = 0;// 记录最小生成树大小
	while (!pq.empty()) {
		if (cnt >= n) return;
		int x = pq.top().second;
		ans = max(ans, pq.top().first);// 更新答案
		pq.pop();
		if (vis[x]) continue;
		vis[x] = true;
		cnt++;
		for (int y = 1; y <= n; y++) {
			if (y == x) continue;
			double d = sqrt((a[y].x - a[x].x) * (a[y].x - a[x].x) + (a[y].y - a[x].y) * (a[y].y - a[x].y)); //当前天线与下一个的距离
			if (d <= dis[y]) {
				dis[y] = d;
				pq.push({d, y});// 推入下一个天线
			}
		}
	}
}
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i].x >> a[i].y;
	prim();
	printf("%.7f\n", ans / 2); //保留7位小数
//注意:不要单输出ans,ans是两点的距离,是两倍的半径
	return 0;
}
原文