UVA 12386 smallest Polygon n points of arbitrary polygons for minimum perimeter science of violence

Source: Internet
Author: User

Topic Links:

Test instructions

Given n points, a polygon of n points (which can be concave polygons, but n points must be all on a polygon)

In all polygons that can consist of n points

The perimeter of the polygon with the smallest area-the minimum perimeter.

Ideas:

First we select a fixed point, then the next number of a permutation, there is (n-1)! permutations.

This sequence has a line between the two adjacent numbers.

Judge Polygon Legal: Any two line segments do not intersect. N^2

The rest is a simple update of the answer.

So the complexity is (n-1)! * N*n

#include <cstdio> #include <cstring> #include <algorithm> #include <set> #include <cmath>    using namespace Std;const int max_n = 2507;struct Point {double x, y;    Point (Double _x = 0, double _y = 0) {x = _x, y = _y;        } BOOL operator < (const point &AMP;RHS) Const {if (x! = rhs.x) return x < rhs.x;    Return y < Rhs.y; }};int n;int top;point st[100], P[100];typedef point point;double Cross (Point p0, point P1, point p2) {return (p1.x- p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (P1.Y-P0.Y);} Double Mult_cross (Point P1, point P2, point p0) {return (p1.x-p0.x) * (P2.Y-P0.Y)-(P1.Y-P0.Y) * (p2.x-p0.x);} BOOL Is_cross (Point-P1, point-P2, point-P3, point-P4) {if (Std::min (p1.x, p2.x) <= Std::max (p3.x, p4.x) && s Td::min (p3.x, p4.x) <= Std::max (p1.x, p2.x) &&std::min (p1.y, p2.y) <= Std::max (p3.y, P4.y) &AMP;&AMP;STD:: Min (p3.y, p4.y) <= Std::max (p1.y, P2.y) &&mult_cross (P1, P4, p3)* Mult_cross (P2, P4, p3) <= 0 &&mult_cross (P3, p2, p1) * Mult_cross (P4, P2, p1) <= 0) return True;return fals e;} Double dis (point p1, point p2) {return sqrt ((p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y-P2.Y));}    int B[100];bool Check () {b[n-1] = n-1;            for (int i = 0, i < n; ++i) {for (int j = 0; J < N; ++j) {if (i = = j) Continue;            int x = (i + 1)% n, y = (j + 1)% n;            if (i = = y | | j = = X | | x = = y) continue;        if (Is_cross (P[b[i]], p[b[x]], P[b[j]], p[b[y])) return false; }} return true; Inline Double Cross (const-point &a, const-point &b) {return a.x * b.y-a.y * b.x;} BOOL Equal (double A, double b) {return fabs (a) < 1e-6;}    int main () {int T;    scanf ("%d", &t);        while (t--> 0) {scanf ("%d", &n);            for (int i = 0; i < n; ++i) {int x, y;            scanf ("%d%d", &x, &y); p[i].x = 1. * x, p[I].Y = 1.        * y;            } if (n = = 3) {puts ("0.0000");        Continue        } for (int i = 0; i < n-1; ++i) b[i] = i;        Double minlength =-1, Minarea =-1, minarealength =-1;                Do {if (check ()) {Double area = 0, length = 0;                B[n] = b[0];                    for (int i = 0; i < n; i++) {length + = DIS (P[b[i]], p[b[i+1]);                Area + = Cross (P[b[i]], p[b[i+1]);              } area = fabs (area) * 0.5;               printf ("www%.4f%.4f\n", area, length); for (int i = 0; i < n; ++i) printf ("(%d%.f%.f)", B[i], p[b[i]].x, P[B[I]].Y);                Puts ("");                    if (minlength = =-1) {minlength = length;                    Minarea = area;                minarealength = length;                    } else {minlength = min (minlength, length); if (Minarea > Area | | (Equal (area, Minarea) && Length < minarealength) {Minarea = area;                    minarealength = length;        }}}//for (int i = 0; i < n; ++i) printf ("%d", b[i]);      } while (Next_permutation (b, B + n-1));        printf ("%.4f%.4f%.4f\n", minlength, Minarealength, Minarea);    printf ("%.4f\n", Fabs (Minarealength-minlength)); } return 0;}


UVA 12386 smallest Polygon n points of arbitrary polygons for minimum perimeter science of violence

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.