一道几何题

来源:互联网 发布:尚学堂java马士兵全套 编辑:程序博客网 时间:2024/04/29 22:29
  • [1558] Racing Cheat

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • Building buildings all day is so boring, so XadillaX organized a race in Minecraft.

    To increase the difficulty and fun, XadillaX build some walls on the racing ground. The racing ground is a rectangle and all the walls are on the ground with negligible thick. That means the thick of the wall can be ignored. So on the small map, it look like that:

    In fact, everyone's speed in Minecraft is the same. We assume that the ground is N * N miles, there're W walls and the speed of each player is 1 mile per second. The starting point is (0, 0) and the destination is (N, N).
    Hungar gets the small map, so he can cheat! But he's too stupid to cheat. Can you help Hungar to calculate out the minimum time that Hungar can get to the destination from the starting point?

  • 输入
  • This problem contains several cases.
    The first line of each case are two numbers, N and W. N indicates the side length of the city and W is the number of walls. (1.0 <= N <= 100.0, 0 <= M <= 50)
    Then follow M lines. Each line has two coordinates x1 y1 x2 y2, indicates the WALLi. You can assume that no two walls are intersected.
  • 输出
  • For each case, you should print the minimum second that Hungar can get to the destination, two decimal places.
  • 样例输入
  • 10 31.0 1.0 1.0 5.09.0 5.0 9.0 9.05.0 2.5 5.0 7.510 11.0 1.0 9.0 9.0
  • 样例输出
  • 14.6414.14
http://acm.nbut.cn:8081/Problem/view.xhtml?id=1558
下午比赛的一道题。

double EPS=1e-10;// 考虑误差的加法运算double add(double a,double b){    if(fabs(a+b)<EPS*(fabs(a)+fabs(b))) return 0;    return a+b;}struct Point{    double x,y;    Point(){}    Point(double x,double y):x(x),y(y){} // 构造函数,方便代码编写    Point operator +(Point p){        return Point(add(x,p.x), add(y,p.y));    }    Point operator-(Point p){        return Point(add(x,-p.x),add(y,-p.y));    }    Point operator*(double d){        return Point(x*d,y*d);    }    double operator*(Point p){  // 内积 点乘        return add(x*p.x, y*p.y);    }    double operator^(Point p){//  外积 叉乘        return add(x*p.y,-y*p.x);    }    double dist(Point p){        return sqrt(add((x-p.x)*(x-p.x) , (y-p.y)*(y-p.y))) ;    }};//判断点p0是否在线段p1p2内int on_segment(Point p1, Point p2, Point p0){    if (((p1-p0).x * (p2-p0).x <=0 )&& ((p1-p0).y * (p2-p0).y <=0))   // 中间是 &&        return 1;    return 0;}// 判断线段p1p2与q1是否相交int intersection(Point p1,Point p2, Point q1,Point q2){    double d1=(p2-p1)^(q1-p1);           // 计算p1p2 到q 点的转向 d1>0 左转,  d1 <0 右转    double d2=(p2-p1)^(q2-p1);    double d3=(q2-q1)^(p1-q1);    double d4=(q2-q1)^(p2-q1);    if(d1*d2<0 && d3*d4 <0)    // 中间是 &&        return 1;    return 0;}struct Line{       Point s , t ;       void read(){            scanf("%lf%lf%lf%lf" ,&s.x,&s.y,&t.x,&t.y) ;       }}line[58] ;int  m ;vector<pair<int , double> > List[120] ;int  ok(Point a , Point b){     for(int i = 1 ; i <= m ; i++){          if(intersection(a,b,line[i].s , line[i].t))  return 0 ;     }     return 1 ;}queue<int> qq ;double  dist[200] ;bool    in[200] ;double  spfa(){        int i ;        while(! qq.empty()) qq.pop() ;        memset(in , 0 , sizeof(in)) ;        for(i = 0 ; i <= 2*m+1 ; i++) dist[i] = 1000000000 ;        dist[0] = 0 ;        in[0] = 1 ;        qq.push(0) ;        while(! qq.empty()){             int u = qq.front() ; qq.pop() ;             in[u] = 0 ;             for(int i = 0 ; i < List[u].size() ; i++){                   int v = List[u][i].first ;                   double w = List[u][i].second ;                   if(dist[u] + w < dist[v]){                         dist[v] = dist[u] + w ;                         if(! in[v]){                               in[v] = 1 ;                               qq.push(v) ;                         }                   }             }        }        return dist[2*m+1] ;}int  main(){     double n  ;     int i , j;     while(cin>>n>>m){          Point S(0 , 0) , T(n , n) ;          for(i = 1 ; i <= m ; i++)  line[i].read() ;          for(i = 0 ; i <= m*2+1 ; i++) List[i].clear() ;          for(i = 1 ; i <= m ; i++){               List[i*2-1].push_back(make_pair(i*2 , line[i].s.dist(line[i].t))) ;               List[i*2].push_back(make_pair(i*2-1 , line[i].s.dist(line[i].t))) ;          }          for(i = 1 ; i <= m ; i++){              if(ok(S , line[i].s))                   List[0].push_back(make_pair(i*2-1 , S.dist(line[i].s))) ;              if(ok(S , line[i].t))                   List[0].push_back(make_pair(i*2 , S.dist(line[i].t))) ;          }           for(i = 1 ; i <= m ; i++){              if(ok(T , line[i].s))                   List[i*2-1].push_back(make_pair(m*2+1 , T.dist(line[i].s))) ;              if(ok(T , line[i].t))                   List[i*2].push_back(make_pair(m*2+1 , T.dist(line[i].t))) ;          }          if(ok(S , T)) List[0].push_back(make_pair(2*m+1 , S.dist(T))) ;          for(i = 1 ; i <= m ; i++){              for(j = i+1 ; j <= m ; j++){                   if(ok(line[i].s , line[j].s)){                       List[i*2-1].push_back(make_pair(j*2-1 , line[i].s.dist(line[j].s))) ;                       List[j*2-1].push_back(make_pair(i*2-1 , line[i].s.dist(line[j].s))) ;                   }                   if(ok(line[i].s , line[j].t)){                       List[i*2-1].push_back(make_pair(j*2 , line[i].s.dist(line[j].t))) ;                       List[j*2].push_back(make_pair(i*2-1 , line[i].s.dist(line[j].t))) ;                   }                   if(ok(line[i].t , line[j].s)){                       List[i*2].push_back(make_pair(j*2-1 , line[i].t.dist(line[j].s))) ;                       List[j*2-1].push_back(make_pair(i*2 , line[i].t.dist(line[j].s))) ;                   }                   if(ok(line[i].t , line[j].t)){                       List[i*2].push_back(make_pair(j*2 , line[i].t.dist(line[j].t))) ;                       List[j*2].push_back(make_pair(i*2 , line[i].t.dist(line[j].t))) ;                   }              }          }          printf("%.2lf\n" , spfa()) ;     }     return 0 ;}







0 0