POJ 1556 The Doors(线段交+最短路)

来源:互联网 发布:公安部大数据平台 编辑:程序博客网 时间:2024/05/21 11:26
The Doors
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 8639 Accepted: 3310

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

15 4 6 7 824 2 7 8 97 3 4.5 6 7-1

Sample Output

10.0010.06


题意:给出几道竖直的墙,求从(0,5)到(10,5)不碰墙的最短距离

思路:建图求最短路

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<cstring>#include<string>#include<algorithm>using namespace std;#define ll long long#define ms(a,b)memset(a,b,sizeof(a))#define eps 1e-10#define inf 1e8int n,m,cntl,cntp;double add(double a,double b){    if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;    return a+b;}struct P{    double x,y;    P(){}    P(double x,double y): x(x),y(y){}    P operator + (P p)    {        return P(add(x,p.x),add(y,p.y));    }    P operator - (P p)    {        return P(add(x,-p.x),add(y,-p.y));    }    P operator *(double d)    {        return P(x*d,y*d);    }    double dot (P p)    {        return add(x*p.x,y*p.y);    }    double det(P p)    {        return add(x*p.y,-y*p.x);    }};bool on_seg(P p1,P p2,P q){    return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<0;}P intersection(P p1,P p2,P q1,P q2){    return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));}P p[500];pair<P,P> line[500];double Map[500][500];bool jiao(P p1,P p2,P p3,P p4){    P r=intersection(p1,p2,p3,p4);    if(on_seg(p1,p2,r)&&on_seg(p3,p4,r))        return true;    return false;}int main(){    while(~scanf("%d",&n))    {        if(n==-1) break;        cntl=0;        cntp=0;        p[cntp++]=P(0.0,5);        p[cntp++]=P(10.0,5);        for(int i=1;i<=n;i++)        {            double x,y[4];            scanf("%lf%lf%lf%lf%lf",&x,&y[0],&y[1],&y[2],&y[3]);            for(int i=0;i<4;i++)                p[cntp++]=P(x,y[i]);            line[cntl++]=make_pair(P(x,0),P(x,y[0]));            line[cntl++]=make_pair(P(x,y[1]),P(x,y[2]));            line[cntl++]=make_pair(P(x,y[3]),P(x,10));        }        for(int i=0;i<100;i++)            for(int j=0;j<100;j++)                Map[i][j]=inf;        for(int i=0;i<cntp;i++)            for(int j=i+1;j<cntp;j++)        {            int flag=0;            for(int k=0;k<cntl;k++)            {                if(jiao(line[k].first,line[k].second,p[i],p[j]))                {                   // cout<<i<<" "<<j<<endl;                 //   cout<<line[k].first.x<<" "<<line[k].first.y<<endl;                //    cout<<line[k].second.x<<" "<<line[k].second.y<<endl;                    flag=1;                    break;                }            }            if(!flag)            {                Map[i][j]=Map[j][i]=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));            }        }      //  cout<<p[2].x<<" "<<p[2].y<<endl;       // cout<<Map[0][2]<<endl;        for(int k=0;k<cntp;k++)            for(int i=0;i<cntp;i++)                for(int j=0;j<cntp;j++)                    Map[i][j]=min(Map[i][j],Map[i][k]+Map[k][j]);        printf("%.2lf\n",Map[0][1]);    }    return 0;}