poj1556The Doors

链接

枚举两点 若不和任何线段相交 建边为dis(i,j) floyd求最短路

  1 #include <iostream>

  2 #include<cstdio>

  3 #include<cstring>

  4 #include<algorithm>

  5 #include<stdlib.h>

  6 #include<vector>

  7 #include<cmath>

  8 #include<queue>

  9 #include<set>

 10 using namespace std;

 11 #define N 100

 12 #define LL long long

 13 #define INF 0xfffffff

 14 const double eps = 1e-8;

 15 const double pi = acos(-1.0);

 16 const double inf = ~0u>>2;

 17 struct point

 18 {

 19     double x,y;

 20     point(double x=0,double y=0):x(x),y(y){}

 21 }p[N];

 22 struct line

 23 {

 24     point u,v;

 25 }li[N];

 26 double w[N][N];

 27 typedef point pointt;

 28 pointt operator - (point a,point b)

 29 {

 30     return pointt(a.x-b.x,a.y-b.y);

 31 }

 32 int dcmp(double x)

 33 {

 34     if(fabs(x)<eps) return 0;

 35     return x<0?-1:1;

 36 }

 37 double dis(point a)

 38 {

 39     return sqrt(a.x*a.x+a.y*a.y);

 40 }

 41 double cross(point a,point b)

 42 {

 43     return a.x*b.y-a.y*b.x;

 44 }

 45 bool segprointer(point a1,point a2,point b1,point b2)

 46 {

 47     double c1 = cross(a2-a1,b1-a1),c2 = cross(a2-a1,b2-a1),

 48     c3  = cross(b2-b1,a1-b1),c4 = cross(b2-b1,a2-b1);

 49     return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;

 50 }

 51 int main()

 52 {

 53     int n,i,j,k;

 54     while(scanf("%d",&n)!=EOF)

 55     {

 56         if(n==-1) break;

 57         int g = 0;

 58         for(i = 1; i <= 100 ; i++)

 59         {

 60             for(j = 1;  j<= 100 ; j++)

 61             w[i][j] = INF;

 62             w[i][i] = 0;

 63         }

 64         int o = 0;

 65         for(i = 1; i <= n ;i++)

 66         {

 67             double k;

 68             scanf("%lf",&k);

 69             for(j = 1; j <= 4; j++)

 70             {

 71                 p[++g].x = k;

 72                 scanf("%lf",&p[g].y);

 73             }

 74             point pp = point(k,0);

 75             li[++o].u = pp;

 76             li[o].v = p[g-3];

 77             li[++o].u = p[g-2];

 78             li[o].v = p[g-1];

 79             li[++o].u = p[g];

 80             pp = point(k,10);

 81             li[o].v = pp;

 82         }

 83         p[g+1] = point(0,5);

 84         p[g+2] = point(10,5);

 85         //printf("%d\n",segprointer(p[g+1],p[g+2],li[5].u,li[5].v));

 86         for(i = 1; i <= g+2; i++)

 87             for(j = i+1; j <= g+2; j++)

 88             {

 89                 if(i==j) continue;

 90                 for(k = 1; k <= o ; k++)

 91                 {

 92                     if(segprointer(p[i],p[j],li[k].u,li[k].v))break;

 93 

 94                 }

 95                 if(k>o)

 96                 w[i][j] = w[j][i] = dis(p[i]-p[j]);

 97                 //printf("%.2f %.2f %.2f %.2f %.2f\n",p[i].x,p[i].y,p[j].x,p[j].y,w[i][j]);

 98             }

 99         for(i = 1; i <= g+2 ; i++)

100             for(j = 1; j <=g+2 ;j++)

101                 for(k = 1; k <= g+2 ; k++)

102                 w[j][k] = min(w[j][i]+w[i][k],w[j][k]);

103         printf("%.2f\n",w[g+1][g+2]);

104     }

105     return 0;

106 }
View Code

 

你可能感兴趣的:(poj)