luogu P1354 房间最短路问题 计算几何_Floyd_线段交

luogu P1354 房间最短路问题 计算几何_Floyd_线段交

第一次写计算几何,还是很开心的吧(虽然题目好水qaq)

暴力枚举端点,暴力连边即可

用线段交判一下是否可行.

Code:

#include <cstdio>
#include <algorithm>
#include <cmath> 
#include <cstring> 
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 100008
const double eps = 0.00000001; 
const double inf = 10000000000.0; 
using namespace std; 
int n,idx,ls,st,ed; 
double map[200][200]; 
struct Point{ double x,y;  Point(double x=0,double y=0):x(x),y(y){}   }point[maxn]; 
struct Line{  Point a,b;  }line[maxn]; 
int exis(double t){ return (fabs(t)<=eps) ? 0 : (t < 0 ? -1 : 1 ) ; }
double det(double x1,double y1,double x2,double y2) { return x1*y2-x2*y1;  }
double cross(Point a,Point b,Point c) { return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);  }
bool check(Point a,Point b,Point c,Point d) {
    if((exis(cross(a,c,b)) ^ exis(cross(a,d,b)))==-2 && (exis(cross(d,a,c)) ^ exis(cross(d,b,c))) == -2)  
        return true; 
    return false; 
}      
double dis(Point a,Point b) {
    double xx = b.x - a.x; 
    double yy = b.y - a.y; 
    return (double)sqrt(xx*xx+yy*yy) ; 
}
bool is_ok(int a,int b) {
    for(int i=1;i<=ls;++i)      
    {         
        if(check(line[i].a,line[i].b,point[a],point[b])) {
            return false;
        } 
    }
    return true; 
}
void addline(double a,double b,double c,double d)
{
    line[++ls].a.x = a;
    line[ls].a.y = b;
    line[ls].b.x = c;
    line[ls].b.y = d; 
}
void addedge(int u,int v,double dis) { map[u][v] = dis;}
int main(){
    //setIO("input");  
    scanf("%d",&n); 
    for(int i=1;i<=n;++i) {    
        double a,b,c,d,x; 
        scanf("%lf%lf%lf%lf%lf",&x,&a,&b,&c,&d);     
        point[++idx] = Point(x,a);
        point[++idx] = Point(x,b); 
        point[++idx] = Point(x,c); 
        point[++idx] = Point(x,d);    
        addline(x,0,x,a); 
        addline(x,b,x,c); 
        addline(x,d,x,10.0); 
    } 
    point[++idx] = Point(0,5), st=idx; 
    point[++idx] = Point(10,5),ed=idx;          
    for(int i=1;i<=idx;++i) 
        for(int j=1;j<=idx;++j) map[i][j] = inf; 
            for(int i=1;i<=idx;++i) map[i][i]=0; 
    for(int i=1;i<=idx;++i) 
        for(int j=1;j<i;++j)             
            if(i!=j&&is_ok(i,j)) 
                addedge(i,j,dis(point[i],point[j])),addedge(j,i,dis(point[i],point[j])); 
    for(int k=1;k<=idx;++k) 
        for(int i=1;i<=idx;++i) 
            for(int j=1;j<=idx;++j) 
                if(map[i][k]+map[k][j] < map[i][j]) map[i][j] = map[i][k] + map[k][j]; 
    printf("%.2f",map[st][ed]);   
    return 0; 
}

  

posted @ 2019-03-09 13:54 EM-LGH 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值