POJ 1556 The Doors 线段交 dijkstra
题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离。
思路:将每个开口的两端点作为一个节点,再枚举点与点间能否直接到达(判相交),以此建图求最短路。
/** @Date : 2017-07-11 16:17:31
* @FileName: POJ 1556 线段交+dijkstra 计算几何.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <math.h>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const double INF = 0x7f;
const int N = 1e5+20;
const double eps = 1e-8; struct Point
{
double x, y;
Point(){}
Point(double xx, double yy){x = xx, y = yy;}
Point operator -(const Point &b) const
{
return Point(x - b.x, y - b.y);
}
double operator *(const Point &b) const
{
return x * b.x + y * b.y;
}
}; double cross(Point a, Point b)
{
return a.x * b.y - a.y * b.x;
} struct Line
{
Point s, t;
Line(){}
Line(Point ss, Point tt){s = ss, t = tt;}
}; double distc(Point p1, Point p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
double xmult(Point p1, Point p2, Point p0)
{
return cross(p1 - p0, p2 - p0);
} //判两点在线段异侧,点在线段上返回0
bool opposite_side(Point p1, Point p2, Line l)
{
return xmult(l.s, p1, l.t)*xmult(l.s, p2, l.t) < -eps;
} bool JudgeInter(Line a, Line b)//判断线段相交 不包括端点及重合
{
return opposite_side(a.s, a.t, b) && opposite_side(b.s, b.t, a);
} Line l[200];
Point p[200];
double mp[200][200];
double dis[200];
bool vis[200]; void dijkstra(int n)
{
MMF(vis);
MMI(dis);
dis[0] = 0;
vis[0] = 1;
queue<int>q;
q.push(0);
while(!q.empty())
{
int nw = q.front();
q.pop();
for(int i = 0; i < n; i++)
{
double dic = dis[nw] + mp[nw][i];
if(dis[i] > dic)
dis[i] = dic;
}
double mi = INF;
int np = 0;
for(int i = 0; i < n; i++)
if(!vis[i] && mi > dis[i])
mi = dis[i], np = i;
if(mi == INF)
break;
q.push(np);
vis[np] = 1;
}
}
int n;
int main()
{
while(~scanf("%d", &n) && n!=-1)
{
int cntl = 0;
int cntp = 0;
p[cntp++] = Point(0, 5.0000);
double x, a, b, c, d;
for(int i = 0; i < n; i++)
{
scanf("%lf%lf%lf%lf%lf", &x, &a, &b, &c, &d);
p[cntp++] = Point(x, 0.000);
p[cntp++] = Point(x, a);
l[cntl++] = Line(p[cntp - 2], p[cntp - 1]); p[cntp++] = Point(x, b);
p[cntp++] = Point(x, c);
l[cntl++] = Line(p[cntp - 2], p[cntp - 1]); p[cntp++] = Point(x, d);
p[cntp++] = Point(x, 10.0000);
l[cntl++] = Line(p[cntp - 2], p[cntp - 1]);
}
p[cntp++] = Point(10.0000, 5.0000);
/////
for(int i = 0; i < cntp; i++)//枚举任意两个点间是否能直接到达
{
for(int j = 0; j < cntp; j++)
{
if(j == i)
continue;
Line tmp = Line(p[i], p[j]);
int flag = 0;
for(int k = 0; k < cntl; k++)
{
if(JudgeInter(tmp, l[k]))//判断是否与线段相交
{
mp[i][j] = INF;
flag = 1;
break;
}
} if(!flag)
mp[i][j] = distc(p[i], p[j]);
}
}
dijkstra(cntp); /*for(int i = 0; i < cntp; i++)
cout << dis[i] << endl;*/
printf("%.2lf\n", dis[cntp - 1]);
}
return 0;
}
POJ 1556 The Doors 线段交 dijkstra的更多相关文章
- POJ 1556 The Doors(线段交+最短路)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- POJ 1556 The Doors 线段判交+Dijkstra
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6734 Accepted: 2670 Descrip ...
- POJ 1556 The Doors(线段交+最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5210 Accepted: 2124 Descrip ...
- POJ 3304 Segments 基础线段交判断
LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- poj 1556 The Doors(线段相交,最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7430 Accepted: 2915 Descr ...
- POJ 1556 The Doors(线段相交+最短路)
题目: Description You are to find the length of the shortest path through a chamber containing obstruc ...
随机推荐
- Python:模块学习——sys模块
sys模块常见函数和变量 sys.argv:命令行参数,实现从程序外部向程序传递参数 [注]:(1) sys.argv[0] 表示代码本身的文件路径 (2)sys.argv是一个元组,可以用[ ]提取 ...
- C#高级编程 (第六版) 学习 第三章:对象和类型
第三章 对象和类型 1,类和结构 类存储在托管堆上 结构存储在堆栈上 2,类成员 类中的数据和函数称为类成员 数据成员 数据成员包括了字段.常量和事件 函数成员 方法:与某个类相关的函数,可以 ...
- 02_Java基础_第2天(变量、运算符)_讲义
今日内容介绍 1.变量 2.运算符 01变量概述 * A: 什么是变量? * a: 变量是一个内存中的小盒子(小容器),容器是什么?生活中也有很多容器, * 例如水杯是容器,用来装载水:你家里的大衣柜 ...
- rabbitmqctl 的常用命令
# 查看服务器的状态 rabbitmqctl status # 查看环境变量 rabbitmqctl environment # 停止rabbitmq的应用 rabbitmqctl stop_ ...
- mysql 中文字段排序
方法1)select * from mytable order by CONVERT(chineseColumnName USING gbk); (备注:chineseColumnName 位排序字 ...
- Delphi定位TDataSet数据集最后一条记录
dst_temp.last ;//最后一条dst_temp.first ;//第一条dst_temp.next ;//下一条dst_temp.prior;//上一条
- 对Spark2.2.0文档的学习3-Spark Programming Guide
Spark Programming Guide Link:http://spark.apache.org/docs/2.2.0/rdd-programming-guide.html 每个Spark A ...
- 【JavaScript】离线应用与客户端存储
一.前言 这章非常重要,由于之后需要负责平台手机APP的日后维护,如何让用户在离线状态下正常使用,以及联网后的数据合并变得非常重要. 二.内容 离线检测 navigator ...
- ZJOI 2017 二试 day1 4.26
day0,11:30熄灯,又因为在房间里太浪,空调开了28度,过了好久才成功降温,导致睡得不太好QaQ. 于是早上昏昏欲睡,也没怎么听懂(orz孙耀峰). 中午大家一致提议下午不去听课,回到房间浪了好 ...
- BIOS和CMOS的区别
原文链接:https://www.cnblogs.com/boltkiller/articles/5732424.html 在日常操作和维护计算机的过程中,常常可以听到有关BIOS设置和CMOS设置的 ...