POJ 1556 The Doors

线段与线段判相交,然后建图搞SPFA完事……好久没写代码觉得这题都写得异常蛋疼……

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <memory>
#include <complex>
#include <numeric>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>

using namespace std;

#pragma pack(4)

const double  eps = 1e-8;
const double   pi = acos(-1.0);
const int     inf = 0x7f7f7f7f;

#define loop(a,n)                            \
    for(int i=0;n>i;i++)                     \
        cout<<a[i]<<(i!=n-1?' ':'\n')
#define loop2(a,n,m)                         \
    for(int i=0;n>i;i++)                     \
        for(int j=0;m>j;j++)                 \
            cout<<a[i][j]<<(j!=m-1?' ':'\n')

#define   at(a,i) ((a)&(1<<(i)))
#define   nt(a,i) ((a)^(1<<(i)))
#define set1(a,i) ((a)|(1<<(i)))
#define set0(a,i) ((a)&(~(1<<(i))))

#define cmp(a,b) (fabs((a)-(b))<eps?0:(((a)-(b))>eps?+1:-1))

#define lmax(a,b) ((a)>(b)?(a):(b))
#define lmin(a,b) ((a)<(b)?(a):(b))
#define fmax(a,b) (cmp(a,b)>0?(a):(b))
#define fmin(a,b) (cmp(a,b)<0?(a):(b))

const int MAXV = 12000;
const int MAXE = 24000;

typedef struct Point
{
    double o[2];
    double & operator [] (int i)
    {
        return o[i];
    }
    Point(double x=0,double y=0)
    {
        o[0]=x;
        o[1]=y;
    }
    Point operator + (Point argu)
    {
        return Point(o[0]+argu[0],o[1]+argu[1]);
    }
    Point operator - (Point argu)
    {
        return Point(o[0]-argu[0],o[1]-argu[1]);
    }
    Point operator + (double argu)
    {
        return (*this)*(1+argu/length());
    }
    Point operator - (double argu)
    {
        return (*this)*(1-argu/length());
    }
    Point operator * (double argu)
    {
        return Point(o[0]*argu,o[1]*argu);
    }
    Point operator / (double argu)
    {
        return Point(o[0]/argu,o[1]/argu);
    }
    bool operator > (Point argu) const
    {
        if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])>0;
        else return cmp(o[1],argu[1])>0;
    }
    bool operator >= (Point argu) const
    {
        if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])>=0;
        else return cmp(o[1],argu[1])>=0;
    }
    bool operator < (Point argu) const
    {
        if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])<0;
        else return cmp(o[1],argu[1])<0;
    }
    bool operator <= (Point argu) const
    {
        if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])<=0;
        else return cmp(o[1],argu[1])<=0;
    }
    bool operator != (Point argu) const
    {
        return cmp(o[0],argu[0])!=0||cmp(o[1],argu[1])!=0;
    }
    bool operator == (Point argu) const
    {
        return cmp(o[0],argu[0])==0&&cmp(o[1],argu[1])==0;
    }
    double length(void)
    {
        return sqrt(o[0]*o[0]+o[1]*o[1]);
    }
    double angle(void)
    {
        return fmod(atan2(o[1],o[0])+2*pi,2*pi);
    }
}Vector;

double dot(Vector a,Vector b)
{
    return a[0]*b[0]+a[1]*b[1];
}
double dot(Point o,Point a,Point b)
{
    return dot(a-o,b-o);
}
int ward(Point p,Point s,Point e)
{
    return cmp(dot(s,e,p),0);
}
double cross(Vector a,Vector b)
{
    return a[0]*b[1]-a[1]*b[0];
}
double cross(Point o,Point a,Point b)
{
    return cross(a-o,b-o);
}
int side(Point p,Point s,Point e)
{
    return cmp(cross(s,e,p),0);
}

int boundingBox(double x,double a,double b)
{
    return cmp(a,x)*cmp(x,a);
}
int boundingBox(Point p,Point s,Point e)
{
    if(boundingBox(p[0],s[0],e[0])>0&&boundingBox(p[1],s[1],e[1])>0) return 1;
    else if(boundingBox(p[0],s[0],e[0])>=0&&boundingBox(p[1],s[1],e[1])>=0) return 0;
    else return -1;
}

int locate(Point p,Point s,Point e)
{
    if(p==s||p==e) return 0;
    return side(p,s,e)==0&&boundingBox(p,s,e)>=0?1:-1;
}

int intersection(Point s1,Point e1,Point s2,Point e2)
{
    if(side(s1,s2,e2)*side(e1,s2,e2)<0&&side(s2,s1,e1)*side(e2,s1,e1)<0) return 1;
    else
    {
        if(locate(s1,s2,e2)>=0) return 0;
        if(locate(e1,s2,e2)>=0) return 0;
        if(locate(s2,s1,e1)>=0) return 0;
        if(locate(e2,s1,e1)>=0) return 0;
        return -1;
    }
}

int n;
Point p[MAXV];
double X,Y1,Y2,Y3,Y4;

bool check(Point s,Point e,int cnt)
{
    for(int i=2;cnt>i;i+=2)
    {
        if(intersection(s,e,p[i],p[i+1])>0) return false;
    }
    return true;
}

struct node
{
    int v;
    double w;
}G[MAXE];
int _index,pre[MAXV],next[MAXE];

void clear(void)
{
    _index=0;
    memset(pre,-1,sizeof(pre));
}
void add(int u,int v,double w)
{
    G[_index].v=v;
    G[_index].w=w;
    next[_index]=pre[u];
    pre[u]=_index++;
}

int Q[MAXV];
bool inQ[MAXV];
double dis[MAXV];

double SPFA(int src,int des,int n)
{
    int u,v;
    double w;
    for(int i=0;n>i;i++)
    {
        dis[i]=inf;
        inQ[i]=false;
    }
    dis[Q[0]=src]=0;
    for(int f=0,r=1;f!=r;)
    {
        inQ[u=Q[f]]=false;
        f=(f+1)%MAXV;
        for(int i=pre[u];i!=-1;i=next[i])
        {
            v=G[i].v;
            w=G[i].w;
            if(cmp(dis[v],dis[u]+w)>0)
            {
                dis[v]=dis[u]+w;
                if(!inQ[v])
                {
                    inQ[Q[r]=v]=true;
                    r=(r+1)%MAXV;
                }
            }
        }
    }
    return dis[des];
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("The Doors.txt","r",stdin);
    #else
    #endif

    while(scanf("%d",&n),n!=-1)
    {
        int cnt=0;
        p[cnt++]=Point(0,5);
        p[cnt++]=Point(10,5);
        for(int i=0;n>i;i++)
        {
            scanf("%lf %lf %lf %lf %lf",&X,&Y1,&Y2,&Y3,&Y4);
            p[cnt++]=Point(X,0);
            p[cnt++]=Point(X,Y1);
            p[cnt++]=Point(X,Y2);
            p[cnt++]=Point(X,Y3);
            p[cnt++]=Point(X,Y4);
            p[cnt++]=Point(X,10);
        }
        clear();
        for(int i=0;cnt>i;i++)
        {
            for(int j=0;cnt>j;j++)
            {
                if(check(p[i],p[j],cnt))
                {
                    add(i,j,(p[i]-p[j]).length());
                }
            }
        }
        printf("%.2f\n",SPFA(0,1,cnt));
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值