Advertisement
Hippskill

Untitled

Jan 19th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include<stdio.h>
  3. #include<iostream>
  4. #include<vector>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<memory.h>
  8. #include<map>
  9. #include<set>
  10. #include<queue>
  11. #include<list>
  12. #include<sstream>
  13. #include<cstring>
  14. #include<numeric>
  15. #include<limits.h>
  16. using namespace std;
  17.  
  18. const int N = 1e5;
  19.  
  20. struct point{
  21. private:
  22.     double x, y;
  23. public:
  24.     point() {}
  25.     point(double x, double y) : x(x), y(y) {}
  26.     void scan() {
  27.         scanf("%lf%ld", &x, &y);
  28.     }
  29.     point operator-(const point & A) const {
  30.         return point(x - A.x, y - A.y);
  31.     }
  32.  
  33.     double operator* (const point & A) const {
  34.         return x * A.y - y * A.x;
  35.     }
  36.  
  37.     double operator% (const point & A) const {
  38.         return x * A.x + y * A.y;
  39.     }
  40.  
  41.     double dist(const point & A) const {
  42.         point r = A - *this;
  43.         return r.val();
  44.     }
  45.  
  46.     double val() {
  47.         return hypot(x, y);
  48.     }
  49.  
  50.  
  51.    
  52. } a[N];
  53.  
  54.  
  55. int main(){
  56.     int n;
  57.     scanf("%d", &n);
  58.     point P;
  59.     P.scan();
  60.     for (int i = 0; i < n; i++) {
  61.         a[i].scan();
  62.     }
  63.     a[n] = a[0];
  64.     bool minus = false;
  65.     bool plus = false;
  66.     double res = 1e12;
  67.     for (int i = 0; i<n; i++)
  68.     {
  69.         double val = (a[i] - P) * (a[i + 1] - P);
  70.         if ((P - a[i]) % (a[i + 1] - a[i]) < 0)
  71.             res = min(res, a[i].dist(P));
  72.         else {
  73.             res = min(res, ((P - a[i + 1]) % (a[i] - a[i + 1])) < 0 ?
  74.                 a[i + 1].dist(P) :
  75.                 abs(val) / ((a[i] - a[i + 1]).val())
  76.                 );
  77.         }
  78.         if (val > 0)
  79.             plus = true;
  80.         else
  81.             if (val < 0)
  82.                 minus = true;
  83.     }
  84.     if (!(plus && minus))
  85.     {
  86.         printf("0.000");
  87.         return 0;
  88.     }
  89.     printf("%.3lf", res * 2);
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement