博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单几何(线段相交+最短路) POJ 1556 The Doors
阅读量:6938 次
发布时间:2019-06-27

本文共 4329 字,大约阅读时间需要 14 分钟。

 

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

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

 

/************************************************* Author        :Running_Time* Created Time  :2015/10/24 星期六 09:48:49* File Name     :POJ_1556.cpp ************************************************/#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1typedef 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
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
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

  

 

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

你可能感兴趣的文章
我的友情链接
查看>>
Firefox 52 发大招:正式支持 TLS 1.3
查看>>
Django之单元测试
查看>>
Exchange Server 内部版本号和发行日期汇总
查看>>
2015.10.10信息系统项目管理师作业
查看>>
我的友情链接
查看>>
mrtg流量波动大
查看>>
Java8-Stream-终止操作-归约与收集
查看>>
IOS 常用的设计模式
查看>>
spring boot(一):入门篇
查看>>
ext-js当用blur()和focus()来控制焦点
查看>>
测试网线需要注意的地方
查看>>
如何查看linux版本 如何查看LINUX是多少位
查看>>
hibernate cp30和dbcp配置加jidn配置
查看>>
学习雷锋,与爱同行
查看>>
那些无用的人
查看>>
手机office办公——微软推出安卓手机端Office Mobile应用
查看>>
更改Ubuntu默认python版本
查看>>
linux 下改ip(Centos7)
查看>>
sudo命令:解决使用Linux命令行时出现的错误提示
查看>>