题目传送门

题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离

分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijkstra跑最短路。好题!

/************************************************
* Author :Running_Time
* Created Time :2015/10/24 星期六 09:48:49
* File Name :POJ_1556.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 300;
const int E = N * N;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
struct Point { //点的定义
double x, y;
Point (double x=0, double y=0) : x (x), y (y) {}
};
typedef Point Vector; //向量的定义
Point read_point(void) { //点的读入
double x, y;
scanf ("%lf%lf", &x, &y);
return Point (x, y);
}
double polar_angle(Vector A) { //向量极角
return atan2 (A.y, A.x);
}
double dot(Vector A, Vector B) { //向量点积
return A.x * B.x + A.y * B.y;
}
double cross(Vector A, Vector B) { //向量叉积
return A.x * B.y - A.y * B.x;
}
int dcmp(double x) { //三态函数,减少精度问题
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
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);
}
bool operator < (const Point &a, const Point &b) { //点的坐标排序
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool operator == (const Point &a, const Point &b) { //判断同一个点
return dcmp (a.x - b.x) == 0 && dcmp (a.y - b.y) == 0;
}
double length(Vector A) { //向量长度,点积
return sqrt (dot (A, A));
}
double angle(Vector A, Vector B) { //向量转角,逆时针,点积
return acos (dot (A, B) / length (A) / length (B));
}
double area_triangle(Point a, Point b, Point c) { //三角形面积,叉积
return fabs (cross (b - a, c - a)) / 2.0;
}
Vector rotate(Vector A, double rad) { //向量旋转,逆时针
return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad));
}
Vector nomal(Vector A) { //向量的单位法向量
double len = length (A);
return Vector (-A.y / len, A.x / len);
}
Point point_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程
Vector U = p - q;
double t = cross (W, U) / cross (V, W);
return p + V * t;
}
double dis_to_line(Point p, Point a, Point b) { //点到直线的距离,两点式
Vector V1 = b - a, V2 = p - a;
return fabs (cross (V1, V2)) / length (V1);
}
double dis_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式 if (a == b) return length (p - a);
Vector V1 = b - a, V2 = p - a, V3 = p - b;
if (dcmp (dot (V1, V2)) < 0) return length (V2);
else if (dcmp (dot (V1, V3)) > 0) return length (V3);
else return fabs (cross (V1, V2)) / length (V1);
}
Point point_proj(Point p, Point a, Point b) { //点在直线上的投影,两点式
Vector V = b - a;
return a + V * (dot (V, p - a) / dot (V, V));
}
bool inter(Point a1, Point a2, Point b1, Point b2) { //判断线段相交,两点式
double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1),
c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1);
return dcmp (c1) * dcmp (c2) < 0 && dcmp (c3) * dcmp (c4) < 0;
}
bool on_seg(Point p, Point a1, Point a2) { //判断点在线段上,两点式
return dcmp (cross (a1 - p, a2 - p)) == 0 && dcmp (dot (a1 - p, a2 - p)) < 0;
}
double area_poly(Point *p, int n) { //多边形面积
double ret = 0;
for (int i=1; i<n-1; ++i) {
ret += fabs (cross (p[i] - p[0], p[i+1] - p[0]));
}
return ret / 2;
}
struct Edge {
int v, nex;
double w;
Edge () {}
Edge (int v, double w, int nex) : v (v), w (w), nex (nex) {}
bool operator < (const Edge &r) const {
return w > r.w;
}
}edge[E];
double d[N];
int head[N];
bool vis[N];
int n, tot, e; void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, double w) {
edge[e] = Edge (v, w, head[u]);
head[u] = e++;
} void Dijkstra(int s) {
memset (vis, false, sizeof (vis));
for (int i=0; i<tot; ++i) {
d[i] = 1e9;
}
d[s] = 0;
priority_queue<Edge> Q; Q.push (Edge (s, d[s], 0));
while (!Q.empty ()) {
int u = Q.top ().v; Q.pop ();
if (vis[u]) continue;
vis[u] = true;
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v;
double w = edge[i].w;
if (!vis[v] && d[v] > d[u] + w) {
d[v] = d[u] + w;
Q.push (Edge (v, d[v], 0));
}
}
}
} Point P[N]; int main(void) {
while (scanf ("%d", &n) == 1) {
if (n == -1) break;
init ();
tot = 0; double x, y1, y2, y3, y4;
P[tot++] = Point (0, 5);
for (int i=0; i<n; ++i) {
scanf ("%lf%lf%lf%lf%lf", &x, &y1, &y2, &y3, &y4);
P[tot++] = Point (x, y1);
P[tot++] = Point (x, y2);
P[tot++] = Point (x, y3);
P[tot++] = Point (x, y4);
}
P[tot++] = Point (10, 5); for (int i=0; i<tot; ++i) {
for (int j=i+1; j<tot; ++j) {
if (P[i].x == P[j].x) continue;
bool flag = true;
for (int k=i+1; k<j; ++k) {
if (P[k].x == P[i].x || P[k].x == P[j].x) continue;
if (k % 4 == 1) {
if (inter (P[i], P[j], P[k], Point (P[k].x, 0))) {
flag = false; break;
}
}
else if (k % 4 == 0) {
if (inter (P[i], P[j], P[k], Point (P[k].x, 10))) {
flag = false; break;
}
}
else if (k % 4 == 2) {
if (inter (P[i], P[j], P[k], P[k+1])) {
flag = false; break;
}
}
else if (k % 4 == 3) {
if (inter (P[i], P[j], P[k], P[k-1])) {
flag = false; break;
}
}
}
if (flag) {
add_edge (i, j, length (P[j] - P[i]));
}
}
} Dijkstra (0);
printf ("%.2f\n", d[tot-1]);
} return 0;
}

  

简单几何(线段相交+最短路) POJ 1556 The Doors的更多相关文章

  1. 简单几何(线段相交) POJ 2653 Pick-up sticks

    题目传送门 题意:就是小时候玩的一种游戏,问有多少线段盖在最上面 分析:简单线段相交,队列维护当前最上的线段 /******************************************** ...

  2. 简单几何(线段相交)+模拟 POJ 3449 Geometric Shapes

    题目传送门 题意:给了若干个图形,问每个图形与哪些图形相交 分析:题目说白了就是处理出每个图形的线段,然后判断是否相交.但是读入输出巨恶心,就是个模拟题加上线段相交的判断,我第一次WA不知道输出要按字 ...

  3. 简单几何(线段相交) POJ 1410 Intersection

    题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...

  4. 简单几何(线段相交) POJ 1066 Treasure Hunt

    题目传送门 题意:从四面任意点出发,有若干障碍门,问最少要轰掉几扇门才能到达终点 分析:枚举入口点,也就是线段的两个端点,然后选取与其他线段相交点数最少的 + 1就是答案.特判一下n == 0的时候 ...

  5. 简单几何(线段相交) POJ 2826 An Easy Problem?!

    题目传送门 题意:两条线段看成两块木板,雨水从上方往下垂直落下,问能接受到的水的体积 分析:恶心的分类讨论题,考虑各种情况,尤其是入口被堵住的情况,我的方法是先判断最高的两个点是否在交点的同一侧,然后 ...

  6. POJ 1556 - The Doors 线段相交不含端点

    POJ 1556 - The Doors题意:    在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少.    分析:        要么直达,要么 ...

  7. POJ_1556_The Doors_判断线段相交+最短路

    POJ_1556_The Doors_判断线段相交+最短路 Description You are to find the length of the shortest path through a ...

  8. POJ 1556 The Doors【最短路+线段相交】

    思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...

  9. ●POJ 1556 The Doors(简单计算几何+最短路)

    ●赘述题目 10*10的房间内,有竖着的一些墙(不超过18个).问从点(0,5)到(10,5)的最短路. 按照输入样例,输入的连续5个数,x,y1,y2,y3,y4,表示(x,0--y1),(x,y2 ...

随机推荐

  1. JSONKit 简单使用

    http://blog.csdn.net/l_ch_g/article/details/8477187 例子上写的比较浅显易懂, 不过我还是稍微总结一下: 导入JSONKit.h之后 字符串转NSDi ...

  2. win7 64位系统HP LaserJet P1008 / HP LaserJet P1008 P1007 驱动安装成功,但无法打印的原因

    HP LaserJet P1008 打印机驱动安装成功,但是无法打印相关文档的原因是: 1.打印机是水货,惠普中国提供的驱动和该打印机不符合.显示的应该是HP LaserJet Professiona ...

  3. http://www.zhihu.com/question/24896283

    http://www.zhihu.com/question/24896283 Rix Tox,太不專業了 三百.知乎用户.raintorr 等人赞同 1. 更改变量名的几种方法这种情况下该如何快速选中 ...

  4. qcow2文件压缩

    qemu-img convert -O qcow2 /path/old.img.qcow2 /path/new.img.qcow2 转自:https://havee.me/linux/2011-09/ ...

  5. cobbler部署机器的默认密码

    修改cobbler的默认密码: 用 openssl 生成一串密码后加入到 cobbler 的配置文件(/etc/cobbler/settings)里,替换 default_password_crypt ...

  6. 【OpenStack】OpenStack系列15之OpenStack高可用详解

    高可用 概念 级别 陈本 如何实现 分类 Openstack的HA 虚拟机的HA 虚拟机HA 比较 应用级别HA,Heat的HA模板   组件的HA 示意图 Mysql的HA 三种方式之一——主从同步 ...

  7. 深入了解PooledConnectionFactory CachingConnectionFactory Sin

    深入理解PooledConnectionFactory CachingConnectionFactory SingleConnectionFactory PooledConnectionFactory ...

  8. tcp粘包问题(封包)

    tcp粘包分析     http://blog.csdn.net/zhangxinrun/article/details/6721495 解决TCP网络传输“粘包”问题(经典)       http: ...

  9. 在64位的linux上运行32位的程序

    1.症状 (1)执行bin文件时提示:No such file or directory (2)ldd bin文件  的输出为: not a dynamic executable (3)file bi ...

  10. 用RPM包安装MySQL的默认安装路径问题

    在安装PHP时候要对一些配置选项进行设置,其中就有:--with-mysql[=DIR]:包含MySQL扩展,[=DIR]指定mysql安装目录,省略[=DIR]则为默认位置/usr--with-my ...