POJ 1556

POJ 1556

计算几何+最短路
题目大意是有N堵竖直的墙,每堵墙上有三扇门,分别给出每扇门上端点和下端点的坐标。求点(0,5)到(10,5)的最短距离。
枚举两个点,判断这条线段是否与墙相交,相交则这两个点无法直达,否则可以直达,求出这对点之间的距离。最后用Dijkstra求点(0,5)到(10,5)的最短距离。时间复杂度O(n^3)。

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
#define mm0(a) memset(a,0,sizeof(a));
#define debug puts("It's ok here!")
using namespace std;
const int maxn=1e2+5;
const double eps=1e-8;
const double Inf=1e17;
struct Point{
    double x, y;
    Point(){}
    Point(double _x, double _y):x(_x), y(_y){}
    Point operator - (const Point &rhs)const{
        return Point(x-rhs.x,y-rhs.y);
    }
    Point operator + (const Point &rhs)const{
        return Point(x+rhs.x,y+rhs.y);
    }
};
typedef struct Point Vector;
struct line{
    Point s, t;
    line(){}
    line(double sx,double sy,double tx,double ty){s.x=sx; s.y=sy; t.x=tx; t.y=ty;}
    line(Point _s, Point _t):s(_s), t(_t){}
};
int dcmp(double x){
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}
double cross(Vector A,Vector B){
    return A.x*B.y-A.y*B.x;
}
double cross(Point a,Point b,Point c){
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
bool crossleft(Point a,Point b,Point c){
    return cross(a, b, c)>0;
}
bool SegIntersection(line A, line B){
    double c1=cross(A.t-A.s,B.s-A.s), c2=cross(A.t-A.s,B.t-A.s);
    double c3=cross(B.t-B.s,A.s-B.s), c4=cross(B.t-B.s,A.t-B.s);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
double dis(Point a,Point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Point P[maxn];
line Seg[maxn];
int n, m;
int scnt, pcnt;
bool NoSegIntersection(line A,int n){
    for(int i=0;i<n;i++)
    if(SegIntersection(A,Seg[i]))
        return false;
    return true;
}
double d[maxn];
int vis[maxn];
double G[maxn][maxn];
void Dijkstra(int s,int n){
    for(int i=0;i<=n;i++) d[i]=Inf;
    d[s]=0;
    mm0(vis);
    while(1){
        int mi=n;
        for(int j=0;j<n;j++)
        if(!vis[j]&&d[j]<d[mi])
            mi=j;
        if(mi==n) break;
        vis[mi]=1;
        for(int j=0;j<n;j++)
        if(d[j]>d[mi]+G[mi][j])
        d[j]=d[mi]+G[mi][j];
    }
}
int main()
{
    double x, y1, y2;
    int T=0;

//    scanf("%d",&T);
    while(~scanf("%d",&n)&&n!=-1){
        scnt=0, pcnt=0;
        P[pcnt++]=Point(0.0,5.0);
        for(int i=0;i<n;i++){
            scanf("%lf",&x);
            y1=0;
            for(int j=0;j<4;j++){
                scanf("%lf",&y2);
                P[pcnt++]=Point(x,y2);
                if(j%2==0) Seg[scnt++]=line(Point(x,y1),Point(x,y2));
                y1=y2;
            }
            Seg[scnt++]=line(Point(x,y1),Point(x,10.0));
        }
        P[pcnt++]=Point(10.0,5.0);
        for(int i=0;i<pcnt;i++)
        for(int j=i+1;j<pcnt;j++){
            if(dcmp(P[i].x-P[j].x)!=0&&NoSegIntersection(line(P[i],P[j]),scnt))
                G[i][j]=G[j][i]=dis(P[i],P[j]);
            else
                G[i][j]=G[j][i]=Inf;
        }
        Dijkstra(0,pcnt);
        printf("%.2lf\n",d[pcnt-1]);
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值