poj 1556 The Doors (bellman+几何)

The Doors
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6294 Accepted: 2530

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

有些点中间有墙不能连线,故要挨个判断,然后建图,用最短路解决。

</pre><pre name="code" class="cpp">#include"stdio.h"
#include"string.h"
#include"math.h"
#include"iostream"
#include"algorithm"
using namespace std;
#define LL __int64
#define N 100
#define M 1000005
const double inf=1e10;
#define max(a,b) (a>b?a:b)
struct point  //存储点坐标
{
    double x,y;
}p[N];
struct edge   //存储边信息
{
    int u,v;
}e[N*N];
int i,j,n,psize,esize;
double wx[20];  //存储每堵墙的X坐标
double g[N][N]; //邻接矩阵
double py[20][4];
double d[N];
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;
    int i=0;
    while(wx[i]<=a.x&&i<n)
        i++;
    while(wx[i]<b.x&&i<n)
    {
        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
           ||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
           ||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)
        {
            return false;
        }
        i++;
    }
    return true;
}
void Bellman()
{
    int i,j;
    for(i=0;i<psize;i++)
        d[i]=inf;
    d[0]=0;
    bool ff=true;
    int u,v;
    for(i=0;i<psize&&ff;i++)
    {
        ff=false;
        for(j=0;j<esize;j++)
        {
            u=e[j].u; v=e[j].v;
            if(d[u]<inf&&d[u]+g[u][v]<d[v])
            {
                d[v]=d[u]+g[u][v];
                ff=true;
            }
        }
    }
    printf("%.2f\n",d[psize-1]);
}
void solve()
{
    int i,j;
    p[0].x=0;
    p[0].y=5;
    psize=1;
    for(i=0;i<n;i++)
    {
        scanf("%lf",&wx[i]);
        for(j=0;j<4;j++)
        {
            p[psize].x=wx[i];
            scanf("%lf",&py[i][j]);
            p[psize++].y=py[i][j];
        }
    }
    p[psize].x=10;
    p[psize++].y=5;
    for(i=0;i<psize;i++)
    {
        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]))
            {
                g[i][j]=Dis(p[i],p[j]);
                e[esize].u=i;
                e[esize++].v=j;
            }
        }
    }
    Bellman();
}
int main()
{
    while(scanf("%d",&n)!=-1)
    {
        if(n==-1)
            break;
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值