Simple Geometry (segment intersection + shortest path) POJ 1556 the Doors

Source: Internet
Author: User
Tags cos

Topic Portal

Test instructions: From (0, 5) to (10, 5), there are some doors in the middle, the road is straight, ask the shortest distance

Analysis: The key is to build a map, you can save all the points, two-point connectivity is the condition is the line segment and the middle of the line is not intersected, the establishment of a directed graph, and then use Dijkstra to run the shortest way. Good question!

/************************************************* author:running_time* Created time:2015/10/24 Saturday 09:48:49* Fi Le Name:P oj_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 | 1typedef long ll;const int N = 300;const int E = n * n;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const Doubl    E EPS = 1e-10;struct Point {//points defined by double x, y;       Point (Double x=0, double y=0): X (x), Y (y) {}};typedef point Vector; Vector definition point read_point (void) {//points read in Double X, Y    scanf ("%lf%lf", &x, &y); Return point (x, y);} Double Polar_angle (vector A) {//Vector polar return atan2 (A.Y, a.x);} Double dot (vector A, vector B) {//vector dot product return a.x * b.x + a.y * B.Y;} Double Cross (vector A, vector B) {//vector fork product return a.x * B.Y-A.Y * b.x;}    int dcmp (double x) {///three state function, reduced accuracy problem if (fabs (x) < EPS) return 0; else return x < 0? -1:1;} Vector operator + (vector A, vector B) {//vector addition return vector (a.x + b.x, A.Y + b.y);} Vector operator-(vector A, vector B) {//vector subtraction return vector (a.x-b.x, a.y-b.y);} Vector operator * (vector A, double p) {//vector multiplied by the scalar return vector (a.x * p, A.Y * p);} Vector operator/(vector A, double p) {//vector divided by scalar return vector (a.x/p, a.y/p);} BOOL operator < (const dot &a, const point &b) {//points ' coordinate sort return a.x < b.x | | (a.x = = b.x && a.y < B.Y);} BOOL operator = = (Const point &a, const point &b) {       Judge the same point return dcmp (a.x-b.x) = = 0 && dcmp (a.y-b.y) = = 0;} Double length (vector a) {//vector length, dot product return sqrt (dot (A, a));} Double angle (vector a, vector b) {//Vector corner, counterclockwise, dot product return ACOs (dot (A, b)/Length (a)/length (b));} Double Area_triangle (Point A, point B, point C) {//triangular area, fork product return Fabs (Cross (b-a, c-a))/2.0;} Vector Rotate (vector A, double rad) {//vector rotation, counter-clockwise return vector (a.x * cos (RAD)-A.y * sin (rad), a.x * sin (rad ) + a.y * cos (RAD));}    Vector Nomal (vector a) {//vector double len = length (a) of the unit method vectors; Return Vector (-a.y/len, A.x/len);}    Point Point_inter (point P, Vector V, point q, Vector W) {//two line intersection, parametric equation Vector U = p-q;    Double T = Cross (w, U)/Cross (V, W); return p + V * t;}    Double Dis_to_line the distance from {//points to a straight line, two-point Vector V1 = b-a, V2 = p-a; Return Fabs (Cross (V1, V2))/Length (V1);}    Double dis_to_seg (point P, point A, point B){//point-to-line distance, two-point 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, points a, dot b) {//dots are projected on a line, two points Vector V = b-a; Return a + v * (dot (v, p-a)/dot (V, v));} BOOL Inter (Points A1, point A2, Spot b1, dot b2) {//Judgment segment intersection, two-bit double C1 = Cross (A2-A1, b1-a1), C2 = Cro    SS (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, bit A1, points A2) {//Judgment dot on segment, two-point return dcmp (Cross (a1-p, a2-p)) = = 0 && DCMP (dot (a1-p, a2-p)) < 0;}    Double Area_poly (point *p, int n) {//polygon area 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 &AMP;R) const    {return w > R.W;    }}edge[e];d ouble 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 = (). 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) contin                Ue                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], Po   Int (p[k].x))) {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;}


Simple Geometry (segment intersection + shortest path) POJ 1556 the Doors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.