Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=3548
Test instructions
Given the coordinates of n points, the perimeter of the triangle with the smallest perimeter can be calculated by these points.
Analysis:
The three variable lengths of triangles are, a,b,c; a<b+c; L=a+b+c;
So when our current minimum perimeter is less than twice times that of one of the sides,
Then it is clear that the perimeter of the triangle formed by this side must be greater than the current perimeter,
We can do this by the nature of the branch.
The code is as follows:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm>using namespace std; const int MAXN = 1010;struct point{double x, y; BOOL operator < (const struct point &tmp) const{return x<tmp.x; }}p[maxn];inline double dis (point A,point b) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} inline bool Check (point A,point b,point c) {if ((a.x-b.x) * (c.y-b.y) = = (A.Y-B.Y) * (c.x-b.x)) return false; return true;} int main () {int n,t,cas=1; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i=0;i<n;i++) scanf ("%lf%lf", &p[i].x,&p[i].y); Sort (p,p+n); BOOL f=0; Double ans = 1000000000; for (int i=0;i<n;i++) {for (int j=i+1;j<n;j++) {if (ans <=-p[j].x-p[i].x) break; Double DD =dis (P[i],p[j]); for (int k=j+1;k<n;k++) {if (ans<=2* (p[k].x-p[i].x)) break; if (check (P[i],p[j],p[k])) {f=1; if (Ans>dis (P[i],p[k]) +dis (P[j],p[k]) +dd) ans = dis (p[i],p[k]) +dis (p[j],p[k]) +dd; }}}} if (f) printf ("Case%d:%.3lf\n", Cas++,ans); else printf ("Case%d:no solution\n", cas++); } return 0;}
HDU 3458 Enumerate the triangles (minimum perimeter triangle)