zoukankan      html  css  js  c++  java
  • POJ 1556 The Doors(线段相交+最短路)

    题目:

    Description

    You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length. 

    Input

    The input data for the illustrated chamber would appear as follows. 


    4 2 7 8 9 
    7 3 4.5 6 7 

    The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1. 

    Output

    The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

    Sample Input

    1
    5 4 6 7 8
    2
    4 2 7 8 9
    7 3 4.5 6 7
    -1

    Sample Output

    10.00
    10.06

    题意:给一个10*10的正方形 其中有数堵墙 每堵墙上都有两个门 给出两个门(每个门为一段区间)的坐标 求从(0,5)到(10,5)的最短路
    思路:枚举每个门向后所有可直达的点的距离 然后建边跑最短路 因为数据很小 所以随便挑一个最短路跑就可以了

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const double inf=0x3f3f3f3f;
    const double eps=1e-10;
    const int maxn=110;
    int n;
    double x,y_1,y2,y3,y4;
    double dis[maxn][maxn];
    
    int dcmp(double x){
        if(fabs(x)<eps) return 0;
        if(x<0) return -1;
        else return 1;
    }
    
    struct Point{
        double x,y;
        Point(){}
        Point(double _x,double _y){
            x=_x,y=_y;
        }
        Point operator + (const Point &b) const{
            return Point(x+b.x,y+b.y);
        }
        Point operator - (const Point &b) const{
            return Point(x-b.x,y-b.y);
        }
        double operator * (const Point &b) const{
            return x*b.x+y*b.y;
        }
        double operator ^ (const Point &b) const{
            return x*b.y-y*b.x;
        }
    };
    
    struct Line{
        Point s,e;
        Line(){}
        Line(Point _s,Point _e){
            s=_s,e=_e;
        }
    }line[maxn];
    
    bool inter(Line l1,Line l2){
        return 
            max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x) &&
            max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x) &&
            max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y) &&
            max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y) &&
            dcmp((l2.s-l1.s)^(l1.e-l1.s))*dcmp((l2.e-l1.s)^(l1.e-l1.s))<=0 &&
            dcmp((l1.s-l2.s)^(l2.e-l2.s))*dcmp((l1.e-l2.s)^(l2.e-l2.s))<=0;
    }
    
    double dist(Point a,Point b){
        return sqrt((b-a)*(b-a));
    }
    
    int main(){
        while(~scanf("%d",&n)){
            if(n==-1) break;
            for(int i=1;i<=n;i++){
                scanf("%lf%lf%lf%lf%lf",&x,&y_1,&y2,&y3,&y4);
                line[2*i-1]=Line(Point(x,y_1),Point(x,y2));
                line[2*i]=Line(Point(x,y3),Point(x,y4));
            }
            for(int i=0;i<=4*n+1;i++){
                for(int j=0;j<=4*n+1;j++){
                    if(i==j) dis[i][j]=0;
                    else dis[i][j]=inf;
                }
            }
            for(int i=1;i<=4*n;i++){
                int lid=(i+3)/4;
                int flag=1;
                Point tmp;
                if(i&1) tmp=line[(i+1)/2].s;
                else tmp=line[(i+1)/2].e;
                for(int j=1;j<lid;j++){
                    if(inter(line[2*j-1],Line(Point(0,5),tmp))==false
                        && inter(line[2*j],Line(Point(0,5),tmp))==false)
                        flag=0;
                }
                if(flag) dis[0][i]=dis[i][0]=dist(Point(0,5),tmp);
                flag=1;
                for(int j=lid+1;j<=n;j++){
                    if(inter(line[2*j-1],Line(Point(10,5),tmp))==false
                        && inter(line[2*j],Line(Point(10,5),tmp))==false)
                        flag=0;
                }
                if(flag) dis[4*n+1][i]=dis[i][4*n+1]=dist(Point(10,5),tmp);
            }
            for(int i=1;i<=4*n;i++)
                for(int j=i+1;j<=4*n;j++){
                    int lid1=(i+3)/4;
                    int lid2=(j+3)/4;
                    int flag=1;
                    Point p1,p2;
                    if(i&1) p1=line[(i+1)/2].s;
                    else p1=line[(i+1)/2].e;
                    if(j&1) p2=line[(j+1)/2].s;
                    else p2=line[(j+1)/2].e;
                    for(int k=lid1+1;k<lid2;k++){
                        if(inter(line[2*k-1],Line(p1,p2))==false
                            && inter(line[2*k],Line(p1,p2))==false)
                            flag=0;
                    } 
                    if(flag) dis[i][j]=dis[j][i]=dist(p1,p2);
                }
            int flag=1;
            for(int i=1;i<=n;i++){
                if(inter(line[2*i-1],Line(Point(0,5),Point(10,5)))==false
                    && inter(line[2*i],Line(Point(0,5),Point(10,5)))==false)
                    flag=0;
            }
            if(flag) dis[0][4*n+1]=dis[4*n+1][0]=10;
            for(int k=0;k<4*n+1;k++)
                for(int i=0;i<=4*n+1;i++)
                    for(int j=0;j<=4*n+1;j++)
                        if(dis[i][j]>dis[i][k]+dis[k][j])
                            dis[i][j]=dis[i][k]+dis[k][j];
            printf("%.2f
    ",dis[0][4*n+1]);
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    使用highcharts.js插件,在ie7浏览器、文本模式为quirks模式下,鼠标移动到折线图中时,弹出框有拖影现象的解决办法
    【转载】Redis在windows下安装过程
    Asp.Net Mvc+Localdb数据库项目在IIS部署的配置
    C# 反射只获取自己定义的属性,不获取父类的属性
    【转载】C#根据当前时间获取周,月,季度,年度等时间段的起止时间
    excel导入sql server 文本被截断,或者一个或多个字符在目标代码页中没有匹配项 错误处理
    一个表中的字段值用作另一个表的In查询条件
    解决UEditor将div标签换成p标签的问题
    将table中的值转换成json格式传到后台接收处理。
    EF CodeFirst 命令步骤
  • 原文地址:https://www.cnblogs.com/whdsunny/p/9839098.html
Copyright © 2011-2022 走看看