Sensor Network

看着是比较难做的。

考虑一个转化:若 (i,j)(i,j) 的距离 d\leq d,连 iji \leftrightarrow j 的边。相当于在图上找最大团。

不过最大团搜索算法不一定能过,考虑一个随机化算法:随机若干次,每次对点的顺序 random_shuffle。从前往后贪心,如果能添加到当前集合后,则添加,否则跳过。随机化 500500 次左右就能过了。

#pragma GCC optimize("-Ofast,fast-math,-inline")
#include <bits/stdc++.h>
using namespace std;
//#define int long long

const int N = 105, MOD = 1e9 + 7; // Remember to change

int n, m, q, t, a[N];

namespace FastIo
{
	#define QUICKCIN ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
	int read()
	{
		char ch = getchar();
		int x = 0, f = 1;
		while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
		while (ch == '-')
		{
			f = -f;
			ch = getchar();
		}
		while (ch >= '0' && ch <= '9')
		{
			x = (x << 1) + (x << 3) + (ch ^ 48);
			ch = getchar();
		}
		return x * f;
	}
	template<class T>
	void write(T x)
	{
		if (x < 0)
		{
			putchar('-');
			x = -x;
		}
		if (x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
	template<class T>
	void writeln(T x)
	{
		write(x);
		putchar('\n');
	}
}

struct Node
{
	double x, y;
	Node(double _x, double _y): x(_x), y(_y){}
	Node(){}
}p[N];
bool w[N][N];
double d;

inline double dist(double X1, double Y1, double X2, double Y2)
{
	return sqrt((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2));
}

vector<pair<double, double> > v;

void check()
{
	for (int i = 1; i <= n; i++) 
	{
		for (int j = 1; j <= n; j++) 
		{
			if (dist(p[i].x, p[i].y, p[j].x, p[j].y) <= d) w[i][j] = 1;
			else w[i][j] = 0;
		}
	}
	vector<int> np;
	vector<pair<double, double> > g;
	for (int i = 1; i <= n; i++)
	{
		for (auto &j : np)
		{
			if (!w[j][i])
			{
				goto Ed;
			}
		}
		g.emplace_back(make_pair(p[i].x, p[i].y));
		np.emplace_back(i);
		Ed:;
	}
	if (g.size() > v.size())
	{
		v.swap(g);
	}
}

map<pair<int, int>, int> mp;

int main()
{
	ios::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
	while (scanf("%d%lf", &n, &d) != EOF) 
	{
		v.clear();
		mp.clear();
		memset(w, 0, sizeof w);
		for (int i = 1; i <= n; i++)
		{
			scanf("%lf%lf", &p[i].x, &p[i].y);
			mp[make_pair(p[i].x, p[i].y)] = i;
		}
		for (int i = 1; i <= 500; i++)
		{
			random_shuffle(p + 1, p + n + 1);
			check();
		}
		printf("%d\n", v.size());
		for (auto &i : v)
		{
			printf("%d ", mp[make_pair(i.first, i.second)]);
		}
		printf("\n");
	}
	return 0;
}
posted @   HappyBobb  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示