描述


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. .net中压缩和解压缩的处理

    最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip 参考代码如下: //======================= ...

  2. 小改动,大作为——C# 4.0中的微小改动

    1.可选参数和命名实参 可选参数和命名实参就如同一对好基友,因为它们经常一起使用. 1.1 可选参数 可选参数重在“可选”,即在调用方法时,该参数可以明确指定实参,也可以不指定.如下代码所示,下面代码 ...

  3. asp:DateDiff 函数

    DateDiff 函数 返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目. 语法 DateDiff(interval, date1, date2[, firstdayofwe ...

  4. OC与Swift的区别三(条件语句)

    11.swift中的switch结构 区别一: oc中switch条件只可以放整数 swift中switch条件可以放几乎任何数据类型 区别二: oc中每一个case中应有break,如果没有brea ...

  5. Fxcop 初体验

    代码质量对于软件项目的成败很重要,这点我想大家都明白.那么在一个软件团队中如何保证代码质量呢?对于这个问题不同的人可能会有不同的答案,对于我而言我觉得做好两点代码质量基本就可以保证了: 1.代码规范( ...

  6. java新手笔记1 Hello World!

    //Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { S ...

  7. Python GUI with Tkinter (from youtube) 在youtube上能找到很多编程视频...

    Python GUI with Tkinter - 1 - Introduction以上链接是一个python tkinter视频系列的第一讲的链接.虽然英语不好,但是,程序还是看得懂的(照着做就可以 ...

  8. HDU 3920 Clear All of Them I(DP + 状态压缩 + 贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3920 题目大意:你在一个位置用激光枪灭敌人,给你初始位置,下面是2*n个敌人的位置,你一枪能杀两个,可 ...

  9. CentOS安装+配置+远程

    这篇博客我之前写在了csdn,转了过来,这篇是自己认为写的比较有技术含量的文章^_^ 最近和CentOS打了交到,其中遇到了很多问题,于是看了一些博客,解决了一些问题,但是都不是特别全面,所以想来一篇 ...

  10. python【第十九篇】Django进阶

    1.路由系统优化 1.1 路由分发 前面我们已经知道,在工程名下的urls.py中写我们的路由映射关系,那么问题来了,假设我们有10个app,如果把所有的url映射都写在urls.py文件中,那么每一 ...