poj 1556

#include<iostream>
#include<cmath>
using namespace std;
double map[1000][1000];
const double esp=1e-6;
struct point {//P[]存点的坐标,line[][]存墙的坐标
        double x,y;
};
point P[1000],line[1000][5];
double dis[1000];
int t,vis[1000];
double xmult(point p0,point p1,point p2){
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double Dis(point a,point b){
       return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool isIntersected(point s1,point e1, point s2,point e2)//判断线段相交
{
    return  (max(s1.x,e1.x) >= min(s2.x,e2.x))  &&
            (max(s2.x,e2.x) >= min(s1.x,e1.x))  &&
            (max(s1.y,e1.y) >= min(s2.y,e2.y))  &&
            (max(s2.y,e2.y) >= min(s1.y,e1.y))  &&
            (xmult(s1,s2,e1)*xmult(s1,e1,e2)>0) &&
            (xmult(s2,s1,e2)*xmult(s2,e2,e1)>0);
}
void djs(int s,int e){
     int k;double Min;
     for (int i=0;i<=t;i++){
         dis[i]=map[s][i];
         vis[i]=0;
     }
     dis[s]=0;vis[s]=1;
     for (int i=0;i<=t;i++){
         Min=1.0e10;
         for (int j=0;j<=t;j++){
             if (!vis[j]&&dis[j]<Min){Min=dis[j];k=j;}
         }
         vis[k]=1;
         for (int j=0;j<=t;j++){
             if (!vis[j]&&map[k][j]!=1.0e6&&dis[k]+map[k][j]<dis[j])
                dis[j]=dis[k]+map[k][j];
         }
     }
     printf("%.2lf\n",dis[e]);
}
int main(){
    int wall_num;
    while (~scanf("%d",&wall_num)&&wall_num!=-1){
          int k=1;
          double x,a,b,c,d;
          P[0].x=0;P[0].y=5;
          t=4*wall_num+1;
          P[t].x=10;P[t].y=5;
          for (int i=0;i<wall_num;i++) {
              scanf("%lf%lf%lf%lf%lf",&x,&a,&b,&c,&d);
              line[3*i][0].x=x;
              line[3*i][0].y=0;
              line[3*i][1].x=x;
              line[3*i][1].y=a;

              line[3*i+1][0].x=x;
              line[3*i+1][0].y=b;
              line[3*i+1][1].x=x;
              line[3*i+1][1].y=c;

              line[3*i+2][0].x=x;
              line[3*i+2][0].y=d;
              line[3*i+2][1].x=x;
              line[3*i+2][1].y=10;

              P[4*i+1].x=P[4*i+2].x=P[4*i+3].x=P[4*i+4].x=x;
              P[4*i+1].y=a;P[4*i+2].y=b;P[4*i+3].y=c;P[4*i+4].y=d;
          }
          for (int i=0;i<=t;i++)
              for (int j=0;j<=t;j++)
                  if (i==j)map[i][j]=0;
                  else map[i][j]=1.0e6;
          for (int i=0;i<=t;i++){
              for (int j=i+1;j<=t;j++){
                      int ok=1;
                      if(P[i].x==P[j].x) continue;
                      for (int k=0;k<3*wall_num;k++){
                          if (P[i].x!=line[k][0].x&&isIntersected(P[i],P[j],line[k][0],line[k][1])){ok=0;break;}
                      }
                      if(ok)map[i][j]=Dis(P[i],P[j]);
              }
          }
          djs(0,t);
    }
    return 0;
}


你可能感兴趣的:(poj 1556)