题目传送门

题意:从(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;
}

  

转载于:https://www.cnblogs.com/Running-Time/p/4906367.html

简单几何(线段相交+最短路) POJ 1556 The Doors相关推荐

  1. POJ 1556 The Doors(计算几何+最短路)

    这题就是,处理出没两个点.假设能够到达,就连一条边,推断可不能够到达,利用线段相交去推断就可以.最后求个最短路就可以 代码: #include <cstdio> #include < ...

  2. POJ-1556 The Doors 线段相交+最短路

    题意:在一个矩形平面内,有若干道墙,现求从左部某一点到右部某一点的最短路径. 解法:有一个事实是线路一定是从门两边的点上通过的,不可能出现从中间穿过的可能.因此我们就枚举两两点之间是否可达,这里就要使 ...

  3. 简单几何(线段覆盖) POJ 3347 Kadj Squares

    题目传送门 题意:告诉每个矩形的边长,它们是紧贴着的,问从上往下看,有几个还能看到. 分析:用网上猥琐的方法,将边长看成左端点到中心的距离,这样可以避免精度问题.然后先求出每个矩形的左右端点,然后如果 ...

  4. Poj 1556 The Doors 计算几何+最短路

    其实本题非常的无脑,无脑拍完1A,写到blog里只因为TM无脑拍也拍了很久啊= = #include <cstdio> #include <cstring> #include ...

  5. POJ 1556 The Doors (未完)

    -- 转载于:https://www.cnblogs.com/XDJjy/p/3209296.html

  6. poj 1556 (Dijkstra + Geometry 线段相交)

    链接:http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  7. 【POJ - 1556】The Doors (计算几何,线段相交)

    题干: You are to find the length of the shortest path through a chamber containing obstructing walls. ...

  8. 几何常用算法与判断线段相交【转】

    下面这个函数在我写的计算几何库函数里面有,那个库可以在http://algorithm.126.com/的资源中心   -   代码角   找到. 算法简单说明: 首先判断以两条线段为对角线的矩形是否 ...

  9. POJ 2653 Pick-up sticks (线段相交)

    题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...

最新文章

  1. K - 迷宫问题 POJ - 3984
  2. 关于站在主管的角度来看企业用人的一些规则
  3. linux刷新磁盘的命令,sync命令 – 刷新文件系统缓冲区
  4. itmz文件如何打开_如何使用proteus8打开低版本proteus7的仿真文件?
  5. 只需三步就能做出可视化大屏的python工具,真香!
  6. asic面试题目 英伟达_免笔试!不限量!全球可编程图形处理技术领袖英伟达2021校园招聘火热进行中!...
  7. 橘子游戏平台_apex英雄_游戏快速下载_雷神加速器全网最快
  8. python是动态_Python中的对象和动态性 [菜鸟的理解,高手莫入]
  9. 安装nginx make install 报错/usr/include/fastdfs/fdfs_define.h:15:27: fatal error: common_define.h: No su
  10. 【入门】用matlab做数字信号处理(学习记录)
  11. 【游戏开发进阶】教你在Windows平台编译tolua runtime的各个平台库(Unity | 热更新 | tolua | 交叉编译)
  12. 程序员面试被问“你的缺点是什么”,该怎么回答?
  13. Operator ‘==‘ cannot be applied to operands of type ‘byte[]‘ and ‘string‘
  14. Word文档中统一字符串八大妙法(转)
  15. 互联网、云计算之用户服务
  16. 花菁染料|cas146368-08-3-齐岳生物
  17. 2021年不限地区不限行业,减轻税负压力,税收优惠政策汇总
  18. hibernate 学习 并且与spring 整合
  19. 经度、纬度、高度转换成大地坐标系XYZ的程序
  20. spring事务的隔离级别和传播特性详解(附实例)

热门文章

  1. mysql unix 安装教程_在UNIX系统下安装MySQL_MySQL
  2. RS232接口是如何工作的?
  3. 【 MATLAB 】信号处理工具箱之 idct 简介及案例分析
  4. 《C和指针》对于数组这一节的总结
  5. ubuntu root默认密码(初始密码)
  6. 从博客园博问站点迁移ASP.NET Core展望.NET Core
  7. Spark源码的编译过程详细解读(各版本)(博主推荐)
  8. asp.net调用opencv类库,实现图像处理显示
  9. NOIP2013pj车站分级[拓扑排序]
  10. 美妆视频小红唇如何打开大数据之门