ZOJ3762:Pan's Labyrinth(点到直线最短距离)

来源:互联网 发布:linux scan 编辑:程序博客网 时间:2024/05/23 22:14

Ofelia is chased by her evil stepfather, and now she finds herself lost in a labyrinth. She needs your help to run away from her tragic family.

There's a huge metal door standing in front of the exit of the labyrinth. There are n dots on the metal door. Pan (the god of the labyrinth) asks Ofelia to find out a triangle which has the largest height. The triangle's three vertexes must be the dots on the door, and its area must be positive. Ofelia should tell Pan the triangle's height so Pan will let Ofelia go.

Input

There are multiple cases (About 100 cases). For each case, the first line contains an integer n (3<=n<=500). In the next n lines, each line contains two real number x[i], y[i] (0<=x[i], y[i]<=10000) which indicates each dot's coordinate. There is no two dots in the same coordinate. The answers are strictly greater than 0.

Output

For each test case, output one line with the maximum triangle height. Any solution with a relative or absolute error of at most 1e-6 will be accepted.

Sample Input

60.000 4.0002.000 4.0001.000 0.0001.000 8.0000.900 7.0001.100 7.00076967.54555 3457.712003.52325 1273.859127755.35733 9812.97643753.00303 2124.709377896.71246 8877.780545832.77264 5213.704784629.38110 8159.01498

Sample Output

7.000008940.96643
 
题意:给出一系列的坐标,要求出这些坐标中组成的三角形中最大的高是多少
思路:

假设拥有最大高的三角形是ABC,如下图

结合此图我们可以看出最大高是点C到直线AB,在这种情况下,下列两个假设必然有一个成立

1.点C是所有点中距离点A最远的

2.点C是所有点中距离点B最远的

证明:(这里先证明在AB上侧成立的情况)

首先过点C做直线A'B'平行于AB,再分别以点A、点B为圆心,AC、BC长为半径做圆

假设上述两条都不成立,则显然只能在直线A'B'以上且在圆A、B外区域的点才符合

假设这个区域存在一点D,显然有点D到直线AB的距离 大于 点C到直线的距离

即△DAB的最大高 大于 △CAB的最大高,与前提“拥有最大高的三角形是ABC”矛盾

得证

在AB下侧,那两个圆的交点和点C是关于AB对称的,道理也一样,就不赘述了

因此我们可以枚举点A,先求得距离他最远的点C,再枚举点B就可以了

 
以上解释来自:http://blog.csdn.net/accelerator_916852/article/details/20394799
 
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;struct point{    double x,y;}p[505];double dist(point a,point b,point c){    if(b.x!=a.x)    {        double k=(b.y-a.y)/(b.x-a.x);        return fabs((c.y-a.y)-k*(c.x-a.x))/sqrt(1+k*k);    }    else        return fabs(c.x-a.x);}double len(point a,point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){    int n,i,j,f;    double maxn,t,ans;    while(~scanf("%d",&n))    {        ans = 0;        for(i = 1;i<=n;i++)        scanf("%lf%lf",&p[i].x,&p[i].y);        for(i = 1;i<=n;i++)        {            maxn = 0;            for(j = 1;j<=n;j++)            {                if(i==j)                continue;                t = len(p[i],p[j]);                if(t>maxn)                {                    maxn = t;                    f = j;                }            }            for(j = 1;j<=n;j++)            {                if(j == i || j == f)                continue;                ans = max(dist(p[i],p[j],p[f]),ans);                ans = max(dist(p[i],p[f],p[j]),ans);                ans = max(dist(p[f],p[j],p[i]),ans);            }        }        printf("%.6f\n",ans);    }    return 0;}

1 0
原创粉丝点击