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; }