HDU 3458 Enumerate the triangles (minimum perimeter triangle)

Source: Internet
Author: User
Tags cas

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.