ZOJ3370 Radio Waves,二分检索

二分检索。注意这里有个陷阱,存在多个共存的区域。

/*******************************************************************************
 # Author : Neo Fung
 # Email : neosfung@gmail.com
 # Last modified: 2012-06-19 21:51
 # Filename: ZOJ3370 Radio Waves.cpp
 # Description : 
 ******************************************************************************/
#ifdef _MSC_VER
#define DEBUG
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
#include <queue>
using namespace std;

const int kMAX=1210;
const double kEPS=1E-9;

double map[kMAX][kMAX];
int ans[kMAX];
// 点的定义
struct POINT{
	double x,y;
}point[kMAX];

// 计算两点的距离
double inline Distance(const POINT &lhs,const POINT &rhs)
{
	return sqrt((lhs.x-rhs.x)*(lhs.x-rhs.x)+(lhs.y-rhs.y)*(lhs.y-rhs.y));
}

int n;
int used[kMAX];
bool Check(const double &x)
{
	memset(used,-1,sizeof(used));
	for(int i=0;i<n;++i)
	{
		if(used[i]>-1)
			continue;
		queue<int> q;
		q.push(i);
		used[i]=0;
		while(!q.empty())
		{
			int now=q.front();
			q.pop();
			for(int j=0;j<n;++j)
			{
				if(j==now || map[now][j]>x)
					continue;
				if(used[j]==used[now])
					return false;
				if(used[j]==-1)
				{
					used[j]=1-used[now];
					q.push(j);
				}
			}
		}
	}
	for(int i=0;i<n;++i)
		ans[i]=used[i]+1;
	return true;
}

// 二分检索,l是左起点坐标,r是右起点坐标(包含r)
// Check 为判定函数,根据需要改变
double BinSearch(double l, double r)
{
	while(l+kEPS<r){
		double mid=(l+r)/2;
		if (Check(mid)) 
			l=mid; 
		else 
			r=mid;
	}
	return l;
}

int main(void)
{
#ifdef DEBUG  
	freopen("../stdin.txt","r",stdin);
	freopen("../stdout.txt","w",stdout); 
#endif  


	while(~scanf("%d",&n) && n)
	{
		for(int i=0;i<n;++i)
		{
			scanf("%lf%lf",&point[i].x,&point[i].y);
			for(int j=0;j<i;++j)
				map[i][j]=map[j][i]=Distance(point[i],point[j]);
		}
		double tmp=BinSearch(0.0,40000.0);
		printf("%.17lf\n",tmp*0.5);
		for(int i=0;i<n;++i)
			printf("%d%c",ans[i],i==(n-1)?'\n':' ');
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值