哦天哪这个萨比提又浪费了我好几个小时。

我们在check的时候只考虑严格相交就行了,想了很久才注意到这一点。

然后就建图跑最短路,over。

 #include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <iostream>
typedef double db;
using namespace std;
const db eps=1e-;
const db pi=acos(-);
int sign(db k){
if (k>eps) return ; else if (k<-eps) return -; return ;
}
int cmp(db k1,db k2){return sign(k1-k2);}
struct point{
db x,y;
point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
point operator * (db k1) const{return (point){x*k1,y*k1};}
point operator / (db k1) const{return (point){x/k1,y/k1};}
db abs(){ return sqrt(x*x+y*y);}
db dis(point k1){ return(*this-k1).abs();}
};
db cross(point k1,point k2){ return k1.x*k2.y-k1.y*k2.x; }
db dot(point k1,point k2){ return k1.x*k2.x+k1.y*k2.y;}
int intersect(db l1,db r1,db l2,db r2){
if (l1>r1) swap(l1,r1); if (l2>r2) swap(l2,r2); return cmp(r1,l2)!=-&&cmp(r2,l1)!=-;
}
int checkSS(point k1,point k2,point k3,point k4){
return //intersect(k1.x,k2.x,k3.x,k4.x)&&intersect(k1.y,k2.y,k3.y,k4.y)&&
sign(cross(k3-k1,k4-k1))*sign(cross(k3-k2,k4-k2))<&&
sign(cross(k1-k3,k2-k3))*sign(cross(k1-k4,k2-k4))<;
}
struct line{
point p[];
};
int checkSS(line a,line b){
return checkSS(a.p[],a.p[],b.p[],b.p[]);
}
int n;
db dis[];
struct node { //堆节点
int u;db d;
bool operator <(const node& rhs) const {
return d>rhs.d;
}
};
struct Edge { int v,nxt;db w;};
Edge e[];
int head[],cnt=;
void addEdge(int u,int v,db w) {
e[++cnt].v=v;
e[cnt].w=w;
e[cnt].nxt=head[u];
head[u]=cnt;
}
void Dijkstra() {
for (int i=;i<=;i++) dis[i]=;
dis[]=;
priority_queue<node> Q; //堆
Q.push((node){,});
while (!Q.empty()) {
node fr=Q.top(); Q.pop();
int u=fr.u;db d=fr.d;
if (cmp(d,dis[u])>) continue;
for (int i=head[u];i;i=e[i].nxt) {
int v=e[i].v;db w=e[i].w;
if (cmp(dis[u]+w,dis[v])<) {
dis[v]=dis[u]+w;
Q.push((node){v,dis[v]});
}
}
}
}
db yl,y2,y3,y4,x;
vector<line> v;
vector<point> g;
bool check(line tmp){
for(int i=;i<v.size();i++){
if(checkSS(tmp,v[i])){
return false;
}//return false;
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cout<<fixed<<setprecision();
while (cin>>n&&n!=-){
g.push_back(point{,});
for(int i=;i<=n;i++){
cin>>x>>yl>>y2>>y3>>y4;
g.push_back(point{x,yl});
g.push_back(point{x,y2});
g.push_back(point{x,y3});
g.push_back(point{x,y4});
v.push_back(line{point{x,},point{x,yl}});
v.push_back(line{point{x,y2},point{x,y3}});
v.push_back(line{point{x,y4},point{x,}});
}
g.push_back(point{,});
int m=g.size();
for(int i=;i<m;i++){
for(int j=i+;j<m;j++){
if(check(line{g[i],g[j]})){
//printf("%d %d\n",i,j);
addEdge(i,j,g[i].dis(g[j]));
addEdge(j,i,g[i].dis(g[j]));
}
}
}
Dijkstra();
cout<<dis[m-]<<endl;
g.clear();
v.clear();
cnt=;
memset(head,, sizeof(head));
}
}
/**
2
4 2 7 8 9
7 3 4.5 6 7
-1
*/

poj 1556的更多相关文章

  1. 最短路+线段交 POJ 1556 好题

    // 最短路+线段交 POJ 1556 好题 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终 ...

  2. POJ 1556 - The Doors 线段相交不含端点

    POJ 1556 - The Doors题意:    在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少.    分析:        要么直达,要么 ...

  3. POJ 1556 The Doors 线段交 dijkstra

    LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...

  4. poj 1556 (Dijkstra + Geometry 线段相交)

    链接:http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  5. poj 1556 The door

    题目链接:http://poj.org/problem?id=1556 #include<cstdio> #include<cstring> #include<cmath ...

  6. poj 1556 zoj1721 BellmanFord 最短路+推断直线相交

    http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  7. POJ 1556 - The Doors - [平面几何+建图spfa最短路]

    题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...

  8. POJ 1556 The Doors【最短路+线段相交】

    思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...

  9. 【POJ 1556】The Doors 判断线段相交+SPFA

    黑书上的一道例题:如果走最短路则会碰到点,除非中间没有障碍. 这样把能一步走到的点两两连边,然后跑SPFA即可. #include<cmath> #include<cstdio> ...

  10. poj 1556 The Doors

    The Doors Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u   Java ...

随机推荐

  1. 12、mysql补充

    本篇导航: 视图 触发器 事务 存储过程 函数 流程控制 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可 ...

  2. java获得上下周及本周日期

    public static SimpleDateFormat getFormat(String format) { return new SimpleDateFormat(format); } /** ...

  3. beego orm 忽略字段

    忽略字段 设置 - 即可忽略 struct 中的字段 type User struct { ... AnyField string `orm:"-"` ... } beego or ...

  4. USE " cc.exports.* = value " INSTEAD OF SET GLOBAL VARIABLE"

    Cocos2d-x 3.5的lua项目生成后,变成了MVC模式,并且,加入了一个全局变量的检测功能.也就是说,你不小心用了全局变量,他会提示你出错! 比如 local temp = 1 temp = ...

  5. 我的第一个正式react demo

    以前在看深入浅出react和redux的时候, 那个demo 总是用creat-react-app 创建的, 现在终于可以实现自己手动搭建一个简单的demo了. 1.首先新建一个文件夹, 执行npm ...

  6. Structured Exception Handling

    https://docs.microsoft.com/en-us/windows/desktop/Debug/structured-exception-handling An exception is ...

  7. 移动端禁止页面拖动 h5禁止拖动页面

    PC上css控制滚动仅css("overflow","hidden")已足够. 但是,如果在Mobile上还是可以拖动的!所以需要监听touchmove事件. ...

  8. SNF框架及机器人2018年1-9月份升级内容

    1月 增加评星控件.年月选择控件 完善表格弹框的封装,增加多选弹框 的封装 增加表格 单元格合并.列头必填与可填写的标识 4月 关于分页查询和排序的各种修改(扶额) 导入excel优化 bs计算合计的 ...

  9. C#跑马灯,图片滚动,后台获取图片地址。动态绑定图片,imag显示文字

    下面附下载地址. http://download.csdn.net/download/njxiaogui/10002058 1.跑马灯效果,图片连续循环滚动,图片下面并可附文字描述,图片是从数据库中获 ...

  10. Android Studio 好用的设置

    Android Studio 好用的设置 设置目录 Getter 模板修改--自动处理 null 判断 格式化代码自动整理方法位置--广度 or 深度 设置步骤: Getter 模板修改,自动处理 n ...