描述


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=329

坐标系,x,y轴都是0~10.起点(0,5),终点(10,5),中间可能有墙,每一堵墙有两个门,给出门的上下定点的坐标,求从起点到终点的最短路.

The Doors 

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x=0, x=10, y=0, and y=10. The initial and final points of the path are always (0,5) and (10,5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length.

Input

The input data for the illustrated chamber would appear as follows.

2
4 2 7 8 9
7 3 4.5 6 7

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0<x<10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.

Output

The output file should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input

1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1

Sample Output

10.00
10.06

分析


在走的时候,如果起点和终点之间没有阻碍,那就直接过去,否则就要绕,假设要绕的门在原路线的上方,绕的话就是先向上走到这个门那里,再向下走,所以走到门的定点就可以了,不需要再走,所有最短路中只会走到门的顶点(想想自己走路绕墙的时候是不是这样...),这样一来把起点,终点,门的两个顶点都当做无向图中的点,两两连边.这时候要去掉和墙相交(严格相交)的边,然后随便用Dijkstra或者Spfa或者Floyd跑一遍最短路即可.

注意:

1.不能用小写的vector(如果有vector的头文件的话).

2.在segment_cross_simple函数中括号要特!别!小!心!调了一个多小时...

 #include <bits/stdc++.h>
using namespace std; const int maxn=;
const double oo=~0u>>,eps=1e-;
int wall_num,edge_num,point_num;
double d[maxn][maxn]; struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){}
}p[maxn];
typedef Point Vector;
Vector operator + (Vector a,Vector b){ return Vector(a.x+b.x,a.y+b.y); }
Vector operator - (Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y); }
Vector operator * (Vector a,double p){ return Vector(a.x*p,a.y*p); }
Vector operator / (Vector a,double p){ return Vector(a.x/p,a.y/p); }
struct edge{
double x1,y1,x2,y2;
edge(double x1=,double y1=,double x2=,double y2=):x1(x1),y1(y1),x2(x2),y2(y2){}
}g[maxn];
void add_point(double x,double y){
p[++point_num]=Point(x,y);
}
void add_edge(double x1,double y1,double x2,double y2){
g[++edge_num]=edge(x1,y1,x2,y2);
}
inline int dcmp(double x){
if(fabs(x)<eps) return ;
return x<?-:;
}
inline double cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
inline bool segment_cross_simple(Point a,Point b,Point c,Point d){
return (dcmp(cross(b-a,c-a))^dcmp(cross(b-a,d-a)))==-&&(dcmp(cross(d-c,a-c))^dcmp(cross(d-c,b-c)))==-;
}
inline double dis(Point a,Point b){
return sqrt(pow(a.x-b.x,)+pow(a.y-b.y,));
}
void Floyd(){
for(int k=;k<=point_num;k++)
for(int i=;i<=point_num;i++)
for(int j=;j<=point_num;j++)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
void outit(){
for(int i=;i<=point_num;i++){
for(int j=;j<=point_num;j++){
printf("d[%d][%d]=%.2lf\t",i,j,d[i][j]);
}
printf("\n");
}
} void solve(){
for(int i=;i<=point_num;i++)
for(int j=i+;j<=point_num;j++){
bool link=true;
for(int k=;k<=edge_num;k++){
Point t1=Point(g[k].x1,g[k].y1);
Point t2=Point(g[k].x2,g[k].y2);
if(segment_cross_simple(p[i],p[j],t1,t2)){
link=false;
break;
}
}
if(link) d[i][j]=d[j][i]=dis(p[i],p[j]);
}
Floyd();
printf("%.2lf\n",d[][]);
}
int main(){
while(scanf("%d",&wall_num)&&wall_num!=-){
point_num=edge_num=;
add_point(,);
add_point(,);
for(int i=;i<=wall_num;i++){
double x,y1,y2,y3,y4;
scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
add_point(x,y1); add_point(x,y2); add_point(x,y3); add_point(x,y4);
add_edge(x,,x,y1); add_edge(x,y2,x,y3); add_edge(x,y4,x,);
}
for(int i=;i<=point_num;i++){
for(int j=;j<=point_num;j++)
d[i][j]=oo;
d[i][i]=;
}
solve();
}
return ;
}

UVA_393_Doors_(计算几何基础+最短路)的更多相关文章

  1. nyis oj 68 三点顺序 (计算几何基础)

    三点顺序 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 如今给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,如今让你推断A,B,C是顺时针给出的还是逆 ...

  2. 【BZOJ】1043: [HAOI2008]下落的圆盘(计算几何基础+贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1043 唯一让我不会的就是怎么求圆的周长并QAAQ... 然后发现好神!我们可以将圆弧变成$[0, 2 ...

  3. 计算几何基础——矢量和叉积 && 叉积、线段相交判断、凸包(转载)

    转载自 http://blog.csdn.net/william001zs/article/details/6213485 矢量 如果一条线段的端点是有次序之分的话,那么这种线段就称为 有向线段,如果 ...

  4. BZOJ_1610_[Usaco2008_Feb]_Line连线游戏_(计算几何基础+暴力)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1610 给出n个点,问两两确定的直线中,斜率不同的共有多少条. 分析 暴力枚举直线,算出来斜率放 ...

  5. 二维计算几何基础题目泛做(SYX第一轮)

    题目1: POJ 2318 TOYS 题目大意: 给一个有n个挡板的盒子,从左到右空格编号为0...n.有好多玩具,问每个玩具在哪个空格里面. 算法讨论: 直接叉积判断就可以.注意在盒子的边界上面也算 ...

  6. 【kuangbin专题】计算几何基础

    1.poj2318 TOYS 传送:http://poj.org/problem?id=2318 题意:有m个点落在n+1个区域内.问落在每个区域的个数. 分析:二分查找落在哪个区域内.叉积判断点与线 ...

  7. 计算几何基础算法几何C++实现

    This file is implementation of Common Common Computational Geometry Algorithms.Please please pay att ...

  8. 【POJ】1556 The Doors(计算几何基础+spfa)

    http://poj.org/problem?id=1556 首先路径的每条线段一定是端点之间的连线.证明?这是个坑...反正我是随便画了一下图然后就写了.. 然后re是什么节奏?我记得我开够了啊.. ...

  9. 【POJ】2318 TOYS(计算几何基础+暴力)

    http://poj.org/problem?id=2318 第一次完全是$O(n^2)$的暴力为什么被卡了-QAQ(一定是常数太大了...) 后来排序了下点然后单调搞了搞..(然而还是可以随便造出让 ...

随机推荐

  1. FOR XML PATH 转换问题

    以下我带大家了解关于 FOR XML PATH 首先我们看下所熟悉的表数据 之后转换 <骨牌编号>1</骨牌编号> <骨牌颜色>橙</骨牌颜色> < ...

  2. 再跟SQL谈一谈--基础篇

    1.简介 2.DDL & DML 3.SELECT ①DISTINCT ②WHERE ③AND & OR ④ORDER BY 4.INSERT 5.UPDATE 6.DELETE 1. ...

  3. 使用instsrv.exe+srvany.exe将应用程序安装为windows服务[转]

      转自:http://qingmu.blog.51cto.com/4571483/1248649 一.什么是instsrv.exe和srvany.exe instsrv.exe.exe和srvany ...

  4. 用变量a给出下面的定义

    a)一个整型数(An integer)b) 一个指向整型数的指针(A pointer to an integer)  c) 一个指向指针的的指针,它指向的指针是指向一个整型数(A pointer to ...

  5. ios NSAssert趣谈

    Apple 官网介绍 NSAssert 的定义如下: #define NSAssert(condition, desc, ...) \ do { \ __PRAGMA_PUSH_NO_EXTRA_AR ...

  6. SomeThing of Memcache

    Memcache for .net 文章一: http://blog.csdn.net/dinglang_2009/article/details/6917794 不定时更新

  7. OpenCV(6)-腐蚀和膨胀

    腐蚀和膨胀属于形态学操作. 腐蚀和膨胀 腐蚀是指:将卷积核B滑过图像A,找出卷积核区域内最小像素值作为锚点像素值.这一操作可以扩大低像素值区域. 膨胀是指:将卷积核B滑过图像A,找出卷积核区域内最大像 ...

  8. clr介绍

    CLR(公用语言运行时)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集),并保证应用和底层操作系统之间必要的分离..NET提供了一个运行时环境,叫做公用语言运行时(Comm ...

  9. MYSQL命令cmd操作

    今天我们就来看一下数据库的各种命令,以下命令全部是从CMD命令窗口下的命令行输入指令,首先如果如果输入mysql,系统提示“mysql不是内部命 令或外部命令.那么这其实是环境变量没有设置好的原因,例 ...

  10. zabbix 配置

    终于把zabbix配置好了.可能还有待优化   我主要参考了几个链接 http://lnmp.org/install.html   一键安装lnmp http://blog.unix178.com/2 ...