poj 3990 Fermat Point in Quadrangle 凸包和费马点

题意:

求一个四边形的费马点。

分析:

模拟退火要么超时要么wa,这题的数据就是不想让随机算法过的。。其实四边形的费马点很简单,如果是凸四边形的话费马点是对角线交点,如果是凹四边形费马点是凹点。但题目给的四个点顺序是不确定的,所以要先求下凸包。

代码:

//poj 3990
//sep9
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

struct Point
{
	double x,y,v;
	Point(){}
	Point(double x,double y):x(x),y(y){}
}pnt[8],rec[8];

double getSum(Point p)
{
	double sum=0;
	for(int i=0;i<4;++i)
		sum+=sqrt((p.x-pnt[i].x)*(p.x-pnt[i].x)+(p.y-pnt[i].y)*(p.y-pnt[i].y));
	return sum;
}

int cmp(Point a,Point b)
{
	if(a.y!=b.y) return a.y<b.y;
	return a.x<b.x;
}

int dbl(double x)
{
	if(fabs(x)<1e-7)
		return 0;
	return x>0?1:-1;
}

double dis(Point a,Point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int cross(Point a,Point b,Point c)
{
	double x1=b.x-a.x;
	double y1=b.y-a.y;
	double x2=c.x-a.x;
	double y2=c.y-a.y;
	return dbl(x1*y2-x2*y1);
}

int graham()
{
	int n=4;
	sort(pnt,pnt+n,cmp);
	rec[0]=pnt[0];
	rec[1]=pnt[1];
	int i,pos=1;
	for(i=2;i<n;++i){
		while(pos>0&&cross(rec[pos-1],rec[pos],pnt[i])<=0) --pos;
		rec[++pos]=pnt[i];
	}
	int bak=pos;
	for(i=n-1;i>=0;--i){
		while(pos>bak&&cross(rec[pos-1],rec[pos],pnt[i])<=0) --pos;
		rec[++pos]=pnt[i];
	}
	return pos;
}

int main()
{
	int i;
	while(1){
		for(i=0;i<4;++i)
			scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
		double ans=0;
		if(pnt[0].x<-0.5) break;
		if(graham()==4){
			ans+=dis(rec[0],rec[2]);
			ans+=dis(rec[1],rec[3]);
		}
		else{
			ans=1e10;
			for(int i=0;i<4;++i)
				ans=min(ans,getSum(pnt[i]));
		}
		printf("%.4lf\n",ans+1e-8);
	}
	return 0;
} 
时间: 2024-08-03 23:55:07

poj 3990 Fermat Point in Quadrangle 凸包和费马点的相关文章

hdu3694 Fermat Point in Quadrangle 求四边形费马点

http://acm.hdu.edu.cn/showproblem.php?pid=3694 Fermat Point in Quadrangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1973    Accepted Submission(s): 361 Problem Description In geometry the

POJ 1228 Grandpa&#39;s Estate [稳定凸包]

Grandpa's Estate Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13112   Accepted: 3697 Description Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one

【POJ 1228】Grandpa&#39;s Estate 凸包

找到凸包后暴力枚举边进行$check$,注意凸包是一条线(或者说两条线)的情况要输出$NO$ #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define N 1003 #define read(x) x = getint() using namespace std; inline int getint() { int k = 0, fh = 1; char c

HDU 3694 Fermat Point in Quadrangle (费马定理求四边形的费马点)

题意:给你四个点,找出一个点到四个点的距离最小 四边形的费马点:凸边形是两对角线的交点,凹边形式凹点. PS: 三角形的费马点: 1.若三角形3个内角均小于120°,那么3条距离连线正好三等分费马点所在的周角,即该点所对三角形三边的张角相等,均为120°.所以三角形的费马点也称为三角形的等角中心. 2.若三角形有一内角大于等于120°,则此钝角的顶点就是距离和最小的点. #include<stdio.h> #include<string.h> #include<stdlib.

poj 3608 Bridge Across Islands 两凸包间最近距离

1 /** 2 旋转卡壳,, 3 **/ 4 #include <iostream> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstdio> 8 using namespace std; 9 10 const double eps = 1e-8; 11 struct point { 12 double x,y; 13 point(double x=0,double y =0):x(x),y(

POJ 1113 || HDU 1348: wall(凸包问题)

传送门: POJ:点击打开链接 HDU:点击打开链接 下面是POJ上的题: Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29121   Accepted: 9746 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's cast

POJ SETI 高斯消元 + 费马小定理

http://poj.org/problem?id=2065 题目是要求 如果str[i] = '*'那就是等于0 求这n条方程在%p下的解. 我看了网上的题解说是高斯消元 + 扩展欧几里德. 然后我自己想了想,就用了高斯消元 + 费马小定理.因为%p是质数,所以很容易就用上了费马小定理,就是在除法的时候用一次就好了.还有就是两个模数相乘还要模一次. #include <cstdio> #include <cstdlib> #include <cstring> #inc

poj 2420,模拟退火算法,费马点

题目链接:http://poj.org/problem?id=2420 题意:给n个点,找出一个点,使这个点到其他所有点的距离之和最小,也就是求费马点. 参考链接:http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html 这一篇文章写的很好,我这个小白都有一点小明白了. 记一下笔记: 模拟退火: 就是在一定范围内接受一些不是最优的解,来跳出局部最优,靠近整体最优. 贴一下伪代码: //#pragma comment(linker,

poj 1113 Wall(标准的凸包果题)

题目链接:http://poj.org/problem?id=1113 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build