HDU——3694 Fermat Point in Quadrangle (三分套三分或模拟退火)

In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the total distance from the three vertices of the triangle to the point is the minimum. It is so named because this problem is first raised by Fermat in a private letter. In the following picture, P 0 is the Fermat point. You may have already known the property that: 


Alice and Bob are learning geometry. Recently they are studying about the Fermat Point. 

Alice: I wonder whether there is a similar point for quadrangle. 

Bob: I think there must exist one. 

Alice: Then how to know where it is? How to prove? 

Bob: I don’t know. Wait… the point may hold the similar property as the case in triangle. 

Alice: It sounds reasonable. Why not use our computer to solve the problem? Find the Fermat point, and then verify your assumption. 

Bob: A good idea. 

So they ask you, the best programmer, to solve it. Find the Fermat point for a quadrangle, i.e. find a point such that the total distance from the four vertices of the quadrangle to that point is the minimum.

Input

The input contains no more than 1000 test cases. 

Each test case is a single line which contains eight float numbers, and it is formatted as below: 

x 1 y 1 x 2 y 2 x 3 y 3 x 4 y 4 

x i, y i are the x- and y-coordinates of the ith vertices of a quadrangle. They are float numbers and satisfy 0 ≤ x i ≤ 1000 and 0 ≤ y i ≤ 1000 (i = 1, …, 4). 

The input is ended by eight -1.

Output

For each test case, find the Fermat point, and output the total distance from the four vertices to that point. The result should be rounded to four digits after the decimal point.

Sample Input

0 0 1 1 1 0 0 1
1 1 1 1 1 1 1 1
-1 -1 -1 -1 -1 -1 -1 -1

Sample Output

2.8284
0.0000

题意:找到一个点到四边形四个点的距离最小;

题解:三分套三分,先三分X,再三分Y,找到最小的,一步一步逼近答案,或者模拟退火,找点,好像三分套三分,与模拟退火有一定的关系,上代码:

三分套三分:

#include <iostream>
#include <cmath>
using namespace std;
const double eps=1e-7;
struct hh{
	double x,y;
}point[21];
double checked(double x,double y){//计算距离
	double sum=0;
	for (int i = 0; i < 4;i++){
		sum+=sqrt((x-point[i].x)*(x-point[i].x)+(y-point[i].y)*(y-point[i].y));
	}
	return sum;
}
double check(double x){//三分Y
	double l=0,r=1e4,lm,rm,ans,ansr,ansl;//三分范围,我用的1e4,满足条件不超时即可
	while(r-l>eps){
		lm=l+(r-l)/3;
		rm=r-(r-l)/3;
		ansl=checked(x,lm);
		ansr=checked(x,rm);
		if(ansl<ansr){
			r=rm;
			ans=ansl;
		}
		else{
			l=lm;
			ans=ansr;
		}
	}
	return ans;
}
int main(){
	while(true){
		for (int i = 0; i < 4;i++){
			scanf("%lf%lf",&point[i].x,&point[i].y);
		}
		if(point[0].x<0) break;
		double l=0,r=1e4,lm,rm,ansl,ansr,ans;//三分范围,我用的1e4,满足条件不超时即可
		while(r-l>eps){//三分X
			lm=l+(r-l)/3;
			rm=r-(r-l)/3;
			ansl=check(lm);
			ansr=check(rm);
			if(ansl<ansr){
				r=rm;
				ans=ansl;
			}
			else{
				l=lm;
				ans=ansr;
			}
		}
		printf("%.4f\n",ans);
	}
	return 0;
} 

模拟退火:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int MAX = 520;
const double eps = 1e-7;
const int mv[4][2]={1,0,-1,0,0,1,0,-1};//上下左右四个方向即可
struct hh{
	double x,y;
}a[MAX],now,tm;
double jisuan(double x,double y){
	double sum=0;
	for (int i = 0; i < 4;i++){
		sum+=sqrt((x-a[i].x)*(x-a[i].x)+(y-a[i].y)*(y-a[i].y));
	}
	return sum;
}
int main(){
	while(true){
		for (int i = 0; i < 4;i++){
			cin >> a[i].x >> a[i].y;
		}
		if(a[0].x<0) break;
		double ans,tt=1000;
		now.x=0;
		now.y=0;
		bool flag;
		ans=jisuan(0,0);//从0,0开始,注意!!!
		while(tt>eps){
			flag=true;
			while(flag){
				flag=false;
				for (int i = 0; i < 4;i++){//上下左右走
					double xx=now.x+mv[i][0]*tt;
					double yy=now.y+mv[i][1]*tt;
					double w=jisuan(xx,yy);
					if(w<ans){
						ans=w;
						now.x=xx;
						now.y=yy;
						flag=true;
					}
				}
			}
			tt/=1.5;//精度我用的除以1.5,不超时即可,当然要尽可能准确,而且要符合题意
		}
		printf("%.4f\n",ans);
	}
	return 0;
}

 

 

全部评论

相关推荐

Tom哥981:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务