poj 1556
哦天哪这个萨比提又浪费了我好几个小时。
我们在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的更多相关文章
- 最短路+线段交 POJ 1556 好题
// 最短路+线段交 POJ 1556 好题 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终 ...
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- poj 1556 (Dijkstra + Geometry 线段相交)
链接:http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- poj 1556 The door
题目链接:http://poj.org/problem?id=1556 #include<cstdio> #include<cstring> #include<cmath ...
- poj 1556 zoj1721 BellmanFord 最短路+推断直线相交
http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- POJ 1556 - The Doors - [平面几何+建图spfa最短路]
题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- 【POJ 1556】The Doors 判断线段相交+SPFA
黑书上的一道例题:如果走最短路则会碰到点,除非中间没有障碍. 这样把能一步走到的点两两连边,然后跑SPFA即可. #include<cmath> #include<cstdio> ...
- poj 1556 The Doors
The Doors Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...
随机推荐
- pygame-KidsCanCode系列jumpy-part18-背景滚动
接上回继续,之前的游戏背景过于单调,今天加几朵白云的背景效果. 要点: 1. 白云要有大有小,尽量模拟出远近层次的效果. 2. 兔子向上跳时,(背景)白云也要相应的滚动,但是为了视觉效果,速度要低于档 ...
- Redis的快照持久化-RDB与AOF
Redis持久化功能 Redis为了内部数据的安全考虑,会把本身的数据以文件形式保存到硬盘中一份,在服务器重启之后会自动把硬盘的数据恢复到内存(redis)的里边. 数据保存到硬盘的过程就称为“持久化 ...
- SAP传输请求自动发布
最近公司服务器做迁移,原R3 PRE需要迁到另外的地方,迁移后一段时间内,需要两套PRE环境同时运行,过一段时间后才将传输路线切换到新的PRE.在切换前,要求新PRE环境也要正常同步发布请求, ...
- 00、Word Count
1.开发环境 1.eclipse-jee-neon-3 2.scala-ide:http://download.scala-ide.org/sdk/lithium/e46/scala212/stabl ...
- [转]CentOS6 Linux上安装ss5服务器
本文章转自:http://blog.csdn.net/cuiyifang/article/details/10346239 最后我增加了添加防火墙规则的部分.感谢作者. ss5是常见的socks5 p ...
- glide引出恶心的git submodule
起因 某一天一个同事,在看那个glide,然后我路过,看到他为何不编译“glide”项目(他说,编译中出错,反正都是看源码而已,所以就懒得搞,然后我出于“好心”,给他弄一下,我擦) 报错: Pro ...
- ero-configuration Web Application Debugging with Xdebug and PhpStorm
1. Install Xdebug To use Xdebug with PhpStorm for debugging PHP applications, you need to have a PHP ...
- curl命令例解
curl -i --url "https://open.abc.com/ddn/purge/ItemIdReceiver" \-X "POST" \-u &qu ...
- power designer 16.5 使用总结[转]
测试环境:power designer 16.5.vs2010.win7 对于破解版的power designer经常出现崩溃.停止工作的情况 请运行pdlegacyshell16.exe,不要运行P ...
- [Java并发编程(四)] Java volatile 的理论实践
[Java并发编程(四)] Java volatile 的理论实践 摘要 Java 语言中的 volatile 变量可以被看作是一种 "程度较轻的 synchronized":与 ...