POJ-1556 The Doors

Posted nianheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-1556 The Doors相关的知识,希望对你有一定的参考价值。

题意:给出一些墙,不能穿墙,求从起点到终点的最短路

就是最短路啊

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+100;
const double inf=1e10;
struct Point{
    double x,y;
    Point(double xx=0,double yy=0){
        x=xx,y=yy;
    }
}a[maxn],s[maxn],t[maxn];
struct Vector{
    double x,y;
    Vector(double xx=0,double yy=0){
        x=xx,y=yy;
    }
};
int tt,m,v[maxn],nex[maxn],head[maxn],num=1,vis[maxn],n;
double x,sy,ty,w[maxn],bj[maxn];
queue<int>q;
int dcmp(double x){return fabs(x)<1e-9?0:(x>0?1:-1);}
Vector operator - (Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}
double operator * (Vector a,Vector b){return a.x*b.y-a.y*b.x;}
double len(Vector a){return sqrt(a.x*a.x+a.y*a.y);}
void add(int x,int y,double z){
    v[++num]=y;
    w[num]=z;
    nex[num]=head[x];
    head[x]=num;
    v[++num]=x;
    w[num]=z;
    nex[num]=head[y];
    head[y]=num;
}
int segment(Point a,Point b,Point c,Point d){
    Vector x=b-a,y=d-c;
    Vector v1=c-a,v2=d-a;
    if(dcmp(x*v1)*dcmp(x*v2)>=0) return 0;
    v1=a-c,v2=b-c;
    if(dcmp(y*v1)*dcmp(y*v2)>=0) return 0;
    return 1;
}
void getdist(){
    memset(head,0,sizeof(head));
    num=1;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++){
            bool ok=1;
            for(int k=1;k<=m;k++)
                if(segment(a[i],a[j],s[k],t[k])){
                    ok=0;
                    break;
                }
            if(ok) add(i,j,len(a[i]-a[j]));
        }
}
double spfa(){
    for(int i=1;i<=n;i++)
        bj[i]=inf;
    bj[1]=0;
    q.push(1);
    while(!q.empty()){
        int now=q.front();
        q.pop();
        for(int i=head[now];i;i=nex[i])
            if(dcmp(bj[v[i]]-bj[now]-w[i])>0){
                bj[v[i]]=bj[now]+w[i];
                if(!vis[v[i]])
                    vis[v[i]]=1,q.push(v[i]);
            }
        vis[now]=0;
    }
    return bj[n];
}
int main(){
    //freopen(".in","r",stdin);
    while(1){
        scanf("%d",&tt);
        if(tt==-1) break;
        n=m=0;
        a[++n]=Point(0,5);
        for(int i=1;i<=tt;i++){
            sy=0,scanf("%lf%lf",&x,&ty);
            s[++m]=Point(x,sy),t[m]=Point(x,ty);
            a[++n]=Point(x,ty);
            scanf("%lf%lf",&sy,&ty);
            s[++m]=Point(x,sy),t[m]=Point(x,ty);
            a[++n]=Point(x,sy),a[++n]=Point(x,ty);
            scanf("%lf",&sy),ty=10;
            s[++m]=Point(x,sy),t[m]=Point(x,ty);
            a[++n]=Point(x,sy);
        }
        a[++n]=Point(10,5);
        getdist();
        printf("%.2lf
",spfa());
    }
    return 0;
} 

以上是关于POJ-1556 The Doors的主要内容,如果未能解决你的问题,请参考以下文章

poj1556 The Doors 2012-01-11

POJ 1556 The Doors

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

POJ 1556 The Doors 线段交 dijkstra

POJ 1556The Doors 判断线段相交+SPFA

POJ-1556 The Doors---线段相交+最短路