描述


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. store procedure 翻页

    store procedure 翻页例子 .turn page CREATE PROCEDURE pageTest --用于翻页的测试 --需要把排序字段放在第一列 ( )=null, --当前页面里 ...

  2. iOS svn版本回退 cornerstone

    http://blog.csdn.net/x32sky/article/details/46866899   IOS开发中,SVN如何恢复到某一个版本(以Cornerstone为例) Cornerst ...

  3. C#基础总复习03

    继续更新...接下来就是面向对象的知识了 1.面向对象:概念:使用面向对象的思想进行编程可以让的程序变得扩展性更高,便于维护: 我们在现实生活中去描述一个人的时候,通过描述这个人的特征和行为. 我们在 ...

  4. Name control

    static: (Page 406) In both C and C++ the keyword static has two basic meanings, which unfortunately ...

  5. 今天收到报警邮件,提示网站502 bad gateway,

    今天收到报警邮件,提示网站502 bad gateway, 输入网站url后果然无法打开: 登录服务器查看nginx进程正常: 查看fastcGI进程已经停止运行了: 问题找到后就该查找是什么原因产生 ...

  6. phpredis

    安装php的redis扩展: http://pecl.php.net/package/redis 也可以用PHP直接连redis: http://www.cnblogs.com/kudosharry/ ...

  7. 关于Linux内核学习的误区以及相关书籍介绍

    http://www.hzlitai.com.cn/article/ARM9-article/system/1605.html 写给Linux内核新手-关于Linux内核学习的误区 先说句正经的:其实 ...

  8. nodejs -mysql模块链接数据库创建库创建表单。

    var mysql = require('mysql'); var connection= mysql.createConnection({ host:'localhost', user:'root' ...

  9. JavaScript符串中每个单词的首字母大写化

    map() + replace() function titleCase(str) { var convertToArray = str.toLowerCase().split(" &quo ...

  10. PHP优化小结

    1.echo 比 print 快,并且使用echo的多重参数(指用逗号而不是句点)代替字符串连接,比如echo $str1,$str2.如果使用echo $str1.$str2 就会需要 PHP 引擎 ...