hdu 4709

http://acm.hdu.edu.cn/showproblem.php?pid=4709

分析:此题比较简单,但是却因为初始化位置不对而WA了无数次……

           此题中说有n棵树,要连其中的一些树,使得面积最小且不为零,则当三个点能够构成三角形时,面积最小,因为不管练成什么样的多边形(大于3边)都能分割成三角形,因而三角形的面积最小,直接用三重循环做即可,计算机面积用向量的点积来计算比hailenen 公式精确度更改。


代码:

       

Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define INF 99999999
const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x)
{
        if(fabs(x) < eps)return 0;
        if(x < 0)return -1;
        else return 1;
}
struct Point
{
        double x,y;
        Point(){}
        Point(double _x,double _y)
        {
                x = _x;
                y = _y;
        }
        Point operator -(const Point &b)const
        {
                return Point(x-b.x,y-b.y);
        }
        double operator ^(const Point &b)const
        {
                return x*b.y - y*b.x;
        }
        double operator *(const Point &b)const
        {
                return x*b.x + y*b.y;
        }
};
//*两点间距离
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}

const int MAXN = 120;
Point list[MAXN],p[3];
double CalcArea()
{
    if((p[0].y-p[1].y)/(p[0].x-p[1].x)==(p[1].y-p[2].y)/(p[1].x-p[2].x))
       return 0;
    double res = 0;
    for(int i = 0;i < 3;i++)
        res += (p[i]^p[(i+1)%3])/2;
    return fabs(res);
}

int main() {
    int T,N;
    double ans;
    cin>>T;
    while(T--){
        cin>>N;ans=INF;
        for(int i=0;i<N;i++) cin>>list[i].x>>list[i].y;  
        if(N<=2) {cout<<"Impossible"<<endl;continue;}
        
        for(int i=0;i<N;i++){
            p[0]=list[i]; 
            for(int j=i+1;j<N;j++){
                p[1]=list[j];
                for(int k=j+1;k<N;k++){
                    p[2]=list[k];
                    if(ans>CalcArea()&&CalcArea()!=0) 
                       ans=CalcArea(); 
                }
            }    
         }    
         if(ans==INF) cout<<"Impossible"<<endl;        
         else         printf("%.2lf\n",ans);
     }
     return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值