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

原题链接

Problem 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.
avatar

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

题目大意

有一个边长为10的正方形,起点为(0,5),终点为(10,5).中间还有t(t<=18)扇门,每扇门有两个门洞,现给出各门洞的端点坐标,要求每次只能走直线,问从起点到终点的最短路径是多少。

解题思路

首先把各个门洞的边缘看成是点,然后如果点于点之间的连线没有被其他门挡住,就建一条边,判断是否挡住转化成直线与线段是否相交。建好图之后跑一次SPFA即可。
(吐槽:这题建图真是烦= =)

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define rep(i,n) for(int i = 0;i < n; i++)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define mp make_pair
#define exp 2.7182818
#define PI 3.141592653589793
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define eps 1e-8
#define mod 1000000007ll
#define sign(x) ((x)>eps?1:((x)<-eps?(-1):(0)))
using namespace std;
double mysqrt(double x) { return max(0.0, sqrt(x)); }
const int maxv=100;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct point2
{
    double x,y;
    point2(){}
    point2(double _x,double _y)
    {
        x=_x,y=_y;
    }
    point2 operator-(const point2 &ne)
    {
        return point2(x-ne.x,y-ne.y);
    }
    point2 operator+(const point2 &ne)
    {
        return point2(x+ne.x,y+ne.y);
    }
    point2 operator*(const double t)
    {
        return point2(x*t,y*t);
    }
};
struct line2
{
    point2 a,b;
    line2(){}
    line2(point2 _a,point2 _b)
    {
        a=_a;
        b=_b;
    }
};
double xmult(point2 a,point2 b)
{ 
    return a.x*b.y-a.y*b.x; 
}
double xmult(point2 o,point2 a,point2 b) { return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y); }
double xmult(double x1,double y1,double x2,double y2) { return x1*y2-x2*y1; }
double dmult(point2 a,point2 b) { return a.x*b.x+a.y*b.y; } 
double dmult(point2 o,point2 a,point2 b) { return (a.x-o.x)*(b.x-o.x)+(a.y-o.y)*(b.y-o.y); }
double lenth(point2 a){ return sqrt(dmult(a,a)); } 
double dist(point2 a,point2 b){ return lenth(b-a); }
double dist2(point2 a,point2 b){ return dmult(b-a,b-a); }
bool Seg_inter_line(line2 l1,line2 l2) 
{
    return sgn(xmult((l2.a-l1.b),(l1.a-l1.b)))*sgn(xmult((l2.b-l1.b),(l1.a-l1.b))) <= 0;
}

struct edge{
    int from, to;
    double cost;
    edge() {}
    edge(int _from, int _to, double _cost) {
        from = _from;
        to = _to;
        cost = _cost;
    }
};

vector<int>G[maxv];
vector<edge> edges;
int rk[maxv];
double dis[maxv];
bool inque[maxv];

void add(int u, int v, double w) {
    edges.push_back(edge(u, v, w));
    int m = edges.size();
    G[u].push_back(m-1);
}

int spfa(int s, int n) {
    for (int i = 0; i <= n; i++) {
        dis[i] = inf;
        rk[i] = 0;
        inque[i] = false;
    }
    dis[s] = 0;
    rk[s] = 1;
    inque[s] = true;
    queue<int>que;
    que.push(s);
    while (!que.empty()) {
        int u = que.front();
        inque[u] = false;
        que.pop();
        for (int i = 0; i < (int)G[u].size(); i++) {
            edge e = edges[G[u][i]];
            if (dis[e.to] > dis[u] + e.cost) {
                dis[e.to] = dis[u] + e.cost;
                if (!inque[e.to]) {
                    que.push(e.to);
                    inque[e.to] = true;
                    rk[e.to]++;
                    if (rk[e.to] >= n) return false;
                }
            }
        }
    }
    return true;
}

int main() 
{ 
    int t;
    double pos;
    double ta,tb,tc,td;

    while(cin>>t&&t!=-1)
    {
        struct node
        {
            double pos;
            vector<point2> pp;
            vector<line2> li;
        }qq[25];

        cl(G);
        edges.clear();
        cl(rk);
        cl(dis);
        cl(inque);
        if(t==0)
        {
            cout<<"10.00"<<endl;
        }
        else
        {
            //int st=0;
            //int ed=4*t+1;
            qq[0].pos=0.0;
            qq[0].pp.pb(point2(0.0,5.0));
            qq[t+1].pos=10.0;
            qq[t+1].pp.pb(point2(10.0,5.0));

            for(int i=1;i<=t;++i)
            {
                scanf("%lf%lf%lf%lf%lf",&pos,&ta,&tb,&tc,&td);
                qq[i].pos=pos;
                qq[i].pp.pb(point2(pos,ta));
                qq[i].pp.pb(point2(pos,tb));
                qq[i].pp.pb(point2(pos,tc));
                qq[i].pp.pb(point2(pos,td));
                qq[i].li.pb(line2(point2(pos,0.0),point2(pos,ta)));
                qq[i].li.pb(line2(point2(pos,tb),point2(pos,tc)));
                qq[i].li.pb(line2(point2(pos,td),point2(pos,10.0)));
            }


            for(int i=0;i<=t;++i)
            {
                for(int j=0;j<qq[i].pp.size();++j)
                {
                    for(int k=i+1;k<=t+1;++k)
                    {
                        for(int l=0;l<qq[k].pp.size();++l)
                        {
                            line2 tmp=line2(qq[i].pp[j],qq[k].pp[l]);
                            int flag=1;
                            for(int x=i+1;x<=k-1&&flag;++x)
                            {
                                for(int y=0;y<qq[x].li.size()&&flag;++y)
                                {
                                    if(Seg_inter_line(tmp,qq[x].li[y]))
                                    {
                                        flag=0;
                                    }
                                }
                            }
                            if(flag)
                            {
                                if(i==0)
                                {
                                    add(0,(k-1)*4+l+1,dist(qq[0].pp[0],qq[k].pp[l]));
                                    add((k-1)*4+l+1,0,dist(qq[0].pp[0],qq[k].pp[l]));
                                }
                                else
                                {
                                    add((i-1)*4+j+1,(k-1)*4+l+1,dist(qq[i].pp[j],qq[k].pp[l]));
                                    add((k-1)*4+l+1,(i-1)*4+j+1,dist(qq[i].pp[j],qq[k].pp[l]));
                                }
                            }
                        }
                    }
                }
            }
            int useless=spfa(0,4*t+1);
            printf("%.2f\n",dis[4*t+1]);
        }
    }   
    return 0;  
}

猜你喜欢

转载自blog.csdn.net/xj949967574/article/details/70767196