POJ 1556 计算几何+最短路

 

The Doors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5526   Accepted: 2230

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.
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 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
根据线段相交建边,然后最短路。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<ctype.h>
#include<iomanip>
#include<set>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
const double pi=acos(-1.0);
const double eps = 1e-8;
int sgn(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);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    void transXY(double B)
    {
        double tx = x,ty = y;
        x = tx*cos(B) - ty*sin(B);
        y = tx*sin(B) + ty*cos(B);
    }
};
double dis(Point a,Point b)
{
        double ss=a.x-b.x;
        double tt=a.y-b.y;
        return sqrt(ss*ss+tt*tt);
}
struct Line
{
    Point s,e;
    double k;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
        k = atan2(e.y - s.y,e.x - s.x);
    }
    pair<int,Point> operator &(const Line &b)const
    {
        Point res = s;
        if(sgn((s-e)^(b.s-b.e)) == 0)
        {
            if(sgn((s-b.e)^(b.s-b.e)) == 0)
                return make_pair(0,res);
            else return make_pair(1,res);
        }
        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x += (e.x-s.x)*t;
        res.y += (e.y-s.y)*t;
        return make_pair(2,res);
    }
};
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) &&
        sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 &&
        sgn((l1.s-l2.s)^(l2.e-l1.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
}
struct NODE
{
        double x,y1,y2,y3,y4;
}pp[2000];
bool cmp(NODE a,NODE b)
{
        return a.x<b.x;
}
#define inf 10000000
const int maxn=300010;
int head[maxn],tol,m,n;
double dist[maxn];
struct Edge
{
        int to,next;
        double val;
}edge[10*maxn];
void add(int u,int v,double w)
{
        edge[tol].to=v;
        edge[tol].next=head[u];
        edge[tol].val=w;
        head[u]=tol++;
}
struct Node
{
        int id;
        double dist;
        Node(int a=0,double b=0):id(a),dist(b){}
        bool operator < (const Node &b) const
        {
                return dist>b.dist;
        }
};
void fun(int st)
{
        int i,j,u,v;
        priority_queue<Node> q;
        q.push(Node(st,0));
        for(i=1;i<=n;i++)dist[i]=inf;
        dist[st]=0;
        while(!q.empty())
        {
                Node ret=q.top();q.pop();
                u=ret.id;
                if(dist[u]<ret.dist)continue;
                for(i=head[u];i!=-1;i=edge[i].next)
                {
                        v=edge[i].to;
                        if(dist[v]>dist[u]+edge[i].val)
                        {
                                dist[v]=dist[u]+edge[i].val;
                                q.push(Node(v,dist[v]));
                        }
                }
        }
}
Line s1[500];
Point s2[500];
int main()
{
        int i,j,k,m;
        while(~scanf("%d",&m))
        {
                if(m==-1)break;
                for(i=1;i<=m;i++)scanf("%lf%lf%lf%lf%lf",&pp[i].x,&pp[i].y1,&pp[i].y2,&pp[i].y3,&pp[i].y4);
                sort(pp+1,pp+m+1,cmp);
                memset(head,-1,sizeof(head));tol=0;n=0;
                int indexx=0;
                s2[++n]=Point(0,5);
                for(i=1;i<=m;i++)
                {
                      Point p1,p2,p3,p4,p5,p6;
                      p1=Point(pp[i].x,0.0);
                      s2[++n]=p1;
                      p2=Point(pp[i].x,pp[i].y1);
                       s2[++n]=p2;
                      p3=Point(pp[i].x,pp[i].y2);
                      s2[++n]=p3;
                      p4=Point(pp[i].x,pp[i].y3);
                      s2[++n]=p4;
                      p5=Point(pp[i].x,pp[i].y4);
                      s2[++n]=p5;
                      p6=Point(pp[i].x,10.0);
                      s2[++n]=p6;
                      s1[++indexx]=Line(p1,p2);
                      s1[++indexx]=Line(p3,p4);
                      s1[++indexx]=Line(p5,p6);
                }
                s2[++n]=Point(10,5);
                for(i=1;i<=n;i++)
                {
                        for(j=i+1;j<=n;j++)
                        {
                             if(sgn(s2[i].x-s2[j].x)==0)continue;
                             Line s(s2[i],s2[j]);
                             int flag=1;
                             for(k=1;k<=indexx;k++)
                             {
                                     if(sgn(s1[k].s.x-s2[i].x)==0)continue;
                                     if(sgn(s1[k].s.x-s2[j].x)==0)continue;
                                     if(inter(s1[k],s))
                                     {
                                           flag=0;
                                           break;
                                     }
                             }
                             if(flag)
                             {
                                     add(i,j,dis(s2[i],s2[j]));
                                     //cout<<i<<" "<<j<<endl;
                             }
                            // cout<<i<<" "<<j<<endl;
                        }
                }
                //cout<<n<<endl;
                //cout<<tol<<endl;
               // cout<<indexx<<endl;
               //for(i=1;i<=n;i++)cout<<s2[i].x<<" "<<s2[i].y<<endl;
                fun(1);
                printf("%.2f\n",dist[n]);
        }
        return 0;
}

 

posted @ 2013-09-20 22:08  线性无关  阅读(134)  评论(0)    收藏  举报