• zoj1721 poj1556


    The Doors

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 file 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

    #include <cstdio>
    #include <cmath>
    #define INF 2000000000
    #define MAXN 100
    
    struct POINT    //平面上点的坐标
    {
        double x, y;
    };
    struct EDGE        //
    {
        int u, v;
    };
    
    int n;            //房间里墙的数目
    double wX[20];    //每堵墙的x坐标(升序)
    POINT p[MAXN];    //存储起点、每扇门的两个端点、终点的平面坐标
    int pSize;        //点的数目(含起点、终点)
    double pY[20][4];    //pY[i][0],pY[i][1],pY[i][2],pY[i][3]:第i堵墙的4个y坐标
    double g[MAXN][MAXN];//邻接矩阵
    EDGE e[MAXN*MAXN];    //存储构造的每条边
    int eSize;            //边的数目
    int i, j;            //循环变量
    
    double Dis( POINT a, POINT b )    //求平面上两个点之间的距离
    {
        return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
    }
    
    //判断点(x3,y3)是否位于点(x1,y1)和(x2,y2)所确定的直线的上方还是下方
    //返回值>0表示(x3,y3)位于直线上方,<0表示位于下方
    double Cross( double x1, double y1, double x2, double y2, double x3, double y3 )
    {
        return (x2-x1)*(y3-y1) - (x3-x1)*(y2-y1);
    }
    
    bool IsOk( POINT a, POINT b )    //在构造有向网时判断两个点之间能否连一条边
    {
        if( a.x >= b.x )  return false;
        bool flag = true;    //是否能提前结束判断的状态变量
        int i = 0;    //循环变量
        while( wX[i] <= a.x && i < n )
            i++;
        while( wX[i] < b.x && i < n )    //判断点a和b之间是否被第i堵墙阻挡
        {
            if( Cross(a.x, a.y, b.x, b.y, wX[i], 0)
                *Cross(a.x, a.y, b.x, b.y, wX[i], pY[i][0]) < 0    //a和b被第1段阻挡
                || Cross(a.x, a.y, b.x, b.y, wX[i], pY[i][1])
                *Cross(a.x, a.y, b.x, b.y, wX[i], pY[i][2]) < 0 //a和b被第2段阻挡
                || Cross(a.x, a.y, b.x, b.y, wX[i], pY[i][3])
                *Cross(a.x, a.y, b.x, b.y, wX[i], 10) < 0) //a和b被第3段阻挡
            {
                flag = false;  break;
            }
            i++;
        }
        return flag;
    }
    
    double BellmanFord( int beg, int end )    //求起始顶点beg到终止顶点end的最短距离
    {
        double d[MAXN];    //起点beg到其他每个顶点的最短距离
        int i, j;
        for( i=0; i<MAXN; i++ )
            d[i] = INF;
        d[ beg ] = 0;
        bool ex = true;    //是否可以提前退出bellman循环的状态变量
        for( i=0; i<pSize && ex; i++ )    //bellman算法
        {
            ex = false;
            for( j=0; j<eSize; j++ )//判断每条边(u,v),能否使顶点v的最短路径距离缩短
            {
                if( d[e[j].u] < INF && d[e[j].v] > d[e[j].u] + g[e[j].u][e[j].v] )
                {
                    d[e[j].v] = d[e[j].u] + g[e[j].u][e[j].v];
                    ex = true;
                }
            }
        }
        return d[end];    //返回起点beg到终点end的最短路径距离
    }
    
    void Solve( )
    {
        p[0].x = 0;    //起点
        p[0].y = 5;
        pSize = 1;
        for( i=0; i<n; i++ )
        {
            scanf( "%lf", &wX[i] );    //读入每堵墙的x坐标
            for( j=0; j<4; j++ )
            {
                p[pSize].x = wX[i];
                scanf("%lf", &p[pSize].y);    //读入墙上两扇门的y坐标
                pY[i][j] = p[pSize].y;
                pSize++;
            }
        }
        p[pSize].x = 10;  p[pSize].y = 5;    //终点
        pSize++;
        for( i=0; i<pSize; i++ )    //初始化邻接矩阵g
        {
            for( j=0; j<pSize; j++ )
                g[i][j] = INF;
        }
        eSize = 0;    //边的数目
        for( i=0; i<pSize; i++ )
        {
            for( j=i+1; j<pSize; j++ )
            {
                if( IsOk( p[i], p[j] ) ) //判断第i个点和第j个点是否连线
                {
                    g[i][j] = Dis( p[i], p[j] );    //邻接矩阵
                    e[eSize].u = i;  e[eSize].v = j;    //
                    eSize++;
                }
            }
        }
        //求第0个顶点到第pSize-1个顶点之间的最短距离
        printf( "%.2lf
    ", BellmanFord( 0, pSize-1 ) );
    }
    
    int main( )
    {
        while( scanf("%d", &n) != EOF )
        {
            if( n == -1 )  break;
            Solve( );
        }
        return 0;
    }
  • 相关阅读:
    face_recognition人脸识别框架
    POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)
    POJ 2392 Space Elevator(多重背包变形)
    POJ 1014 Dividing(多重背包, 倍增优化)
    POJ 1384 Piggy-Bank(完全背包)
    POJ 2063 Investment(完全背包)
    POJ 3211 Washing Cloths(01背包变形)
    POJ 1837 Balance(01背包变形, 枚举DP)
    POJ 2923 Relocation(01背包变形, 状态压缩DP)
    POJ 1243 One Person
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3228140.html
Copyright © 2020-2023  润新知