描述


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. php session_id()函数介绍及代码实例

    session_id()功能: 获取设置当前回话ID. 函数说明: string session_id ([ string $id ] ) 参数: 如果指定了参数$id,那么函数会替换当前的回话id. ...

  2. oracle 11g 64位安装sqldeveloper打开不了

    oracle 11g 64位安装sqldeveloper打开不了解决方法: 1.到官网下载对应版本的sqldeveloper. 2.找对应安装路径下的F:\app\Administrator\prod ...

  3. jQuery对象和Dom对象的区分以及之间转换

    刚开始学习jQuery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介绍一下jQuery,以及两者相互间的转换. 一,什么是jQuer ...

  4. html-----016---HTTP 状态消息

    HTTP 状态消息 当浏览器从 web 服务器请求服务时,可能会发生错误. 从而有可能会返回下面的一系列状态消息: 1xx: 信息 消息: 描述: 100 Continue 服务器仅接收到部分请求,但 ...

  5. PDF编译出现错误解决办法

    今天在编译PDF时发现使用了一下STL中的z数值极限<Numeric Limits>竟然编译不过, return GetRangeConstraint(value <= std::n ...

  6. 由json生成php配置文件

    $str = '<?php return ' . var_export(json_decode($json, true), true) . ';';file_put_contents('./co ...

  7. 编写留言板是遇到的mysql中文乱码问题

    mysql中文显示,需要编码统一,数据库链接文件,database,table编码均要设置一致

  8. 粘滞位(sticky bit)

    linux特殊权限:setUid, setGid, 粘着位(sticky) (1)目录的X权限(执行) 文件的可执行权限很简单,也就是可否执行它的意思,但目录的执行权限又代表什么意思呢? 当然不可能是 ...

  9. Ubuntu15.10 编译VLC Android(安卓)过程记录

    持续更新中... 最后一次修改于 2016-03-20 15:33:45 1.必要库的安装 除基本编译环境(gcc.g++等外),需要额外安装如下的库(用于下载必要的依赖文件) (1)JDK 推荐安装 ...

  10. 【XCode7+iOS9】http网路连接请求、MKPinAnnotationView自定义图片和BitCode相关错误--备用

    更新了iOS9和XCode7,之后,Swift变成了2.0,有了新的语法习惯,iOS也加强了安全方面的限制.我们原本的项目就会出现不少问题.先来看我之前的项目中出现的3个错误吧和相关的解决办法吧. 1 ...