POJ 1566(toj 1952)

The Doors
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 126    Accepted Runs: 69



The Problem

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.

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


Source: Mid-Central USA 1996

交题的时候poj挂了,在hdu上也没找到,突然想起自己学校的oj,在toj上一找还真找到了,交了一发,1A,开心~

说明toj还是很厉害的,题目还是很全的~

思路:乍一看感觉是一道几何题,再一看发现可以用最短路做

在所有点之间连线,判断跟墙是否相交,不相交的话就建边,然后跑一边最短路,什么方法都行了

#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <iomanip>

#define INF 100000000.0

using namespace std;

struct point
{
    double x,y;
    point(){};
    point (double _x,double _y)
    {
        x=_x,y=_y;
    }
};
typedef point vec;

double operator /(vec a, vec b)
{
    return (a.x*b.y-a.y*b.x);
}
vec operator- (point a,point b)
{
    vec c;
    c.x=a.x-b.x;
    c.y=a.y-b.y;
    return c;
}
struct line
{
    point a,b;
    line(){};
    line (point _a,point _b)
    {
        a=_a;
        b=_b;
    }
};
double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

struct edge
{
    int v,next;
    double w;
};
int cross(line m,line n)
{
    if(((n.a-m.a)/(m.b-m.a))*((n.b-m.a)/(m.b-m.a))<=0
       &&((m.a-n.a)/(n.b-n.a))*((m.b-n.a)/(n.b-n.a))<=0)
       return 1;
       else return 0;
}
edge e[5005];
int head[105],cnt=0,vis[5005],n,m;
double dist[105];
queue<int>q;
void addedge(int u,int v,double w)
{
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void spfa(int s)
{
    for(int i=0;i<=n;i++)
        dist[i]=INF,vis[i]=0;
    while(!q.empty()) q.pop();
    q.push(s);
    vis[s]=1;
    dist[s]=0.0;
    while(!q.empty())
    {
        int tmp;
        tmp=q.front();
        q.pop();
        for(int i=head[tmp];i!=-1;i=e[i].next)
        {
            if(dist[e[i].v]>dist[tmp]+e[i].w)
            {
                dist[e[i].v]=dist[tmp]+e[i].w;
                if(!vis[e[i].v])
                {
                    vis[e[i].v]=1;
                    q.push(e[i].v);
                }
            }

        }
        vis[tmp]=0;
    }
}
point p[105];
line le[5005];
bool check(int i,int j)
{
    line temp(p[i],p[j]);
    int left,right;
    if(i!=0 && j!=n-1)
    {
        left=(((i-1)/4)+1)*3;
        right=((j-1)/4)*3;
        for(int c1=left;c1<right;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    else if(i==0 && j!=n-1)
    {
        right=((j-1)/4)*3;
        for(int c1=0;c1<right;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    else if(i!=0 && j==n-1)
    {
        left=(((i-1)/4)+1)*3;
        for(int c1=left;c1<m-1;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    else
    {
        for(int c1=0;c1<m-1;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    return true;
}
int main()
{
    int t;
    while(cin>>t)
    {
        if(t==-1)
            break;
        init();
        int totl=0,totp=1;
        n=4*t+2;
        m=3*t;
        p[0].x=0,p[0].y=5.0;
        p[n-1].x=10.0,p[n-1].y=5.0;
        for(int i=0;i<t;i++)
        {
            double x,y1,y2,y3,y4;
            point a,b;
            line tmp;
            cin>>x>>y1>>y2>>y3>>y4;
            p[totp].x=x,p[totp++].y=y1;
            p[totp].x=x,p[totp++].y=y2;
            p[totp].x=x,p[totp++].y=y3;
            p[totp].x=x,p[totp++].y=y4;
            a.x=x,a.y=0.0;
            b.x=x,b.y=y1;
            tmp.a=a,tmp.b=b;
            le[totl++]=tmp;
            a.x=x,a.y=y2;
            b.x=x,b.y=y3;
            tmp.a=a,tmp.b=b;
            le[totl++]=tmp;
            a.x=x,a.y=y4;
            b.x=x,b.y=10.0;
            tmp.a=a,tmp.b=b;
            le[totl++]=tmp;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(check(i,j))
                    addedge(i,j,dis(p[i],p[j]));
            }
        }
        spfa(0);
        double ans=dist[n-1];
        cout<<fixed<<setprecision(2)<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值