Determination of intersection of POJ-1556 line segments and shortest circuit (ry + Graph Theory)

Source: Internet
Author: User


In Room 10*10, there are some walls. Ask you the shortest path from the source point () to the opposite point (), and you cannot pass through the wall.


Maybe I have seen the shortest path, and I have some questions. "Why must the shortest path pass through the endpoint of the wall, is there a path from a certain point (not above) so that it can directly reach the destination "?

These points are possible. However, I have heard of the endpoint, and I will not pursue it.

If it is an endpoint, it is easy to do. Because there are not many points, it is possible to enumerate the path and determine whether the cable segment is intersecting with it. The intersection of the endpoint cannot be calculated.


# Include <cstdio >#include <cmath> # define INF 0x3fff # define EPS 1e-8using namespace STD; const int M = 500; struct point {Double X, Y ;} point [m]; struct line {point a, B;} line [m]; int CNT, P; double dis [m], map [m] [m]; bool vis [m]; double cross (point a, point B, point C) {return (. x-c.x) * (B. y-c.y)-(B. x-c.x) * (. y-c.y);} bool judge (point a, point B, point C, point D) {return cross (a, B, c) * Cross (a, B, d) <-EPS;} bool iscross (point A, point B) {for (INT I = 0; I <CNT; I ++) if (Judge (a, B, line [I]. a, line [I]. b) & judge (line [I]. a, line [I]. b, A, B) // you need to change the comparison. Previously, only one, WA, was judged. Return false; return true;} double count (point a, point B) {return SQRT (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} void set_map () {int I, j; for (I = 0; I <= P; I ++) {for (j = I; j <= P; j ++) {if (I = J) map [I] [J] = 0; else map [I] [J] = map [J] [I] = inf ;}}for (I = 0; I <p; I ++) {for (j = I + 1; j <p; j ++) {If (iscross (point [I], point [J]) {map [I] [J] = map [J] [I] = count (point [I], point [J]) ;}}} double Dijkstra () {int I, j, k; double min; for (I = 0; I <p; I ++) {dis [I] = map [0] [I]; vis [I] = 0;} dis [0] = 0, vis [0] = 1; for (I = 1; I <p; I ++) {min = inf; For (j = 0; j <p; j ++) {If (! Vis [J] & dis [J] <min) {min = dis [J]; k = J ;}} dis [k] = min; vis [k] = 1; for (j = 0; j <p; j ++) {If (! Vis [J] & map [k] [J] + dis [k] <dis [J]) {dis [J] = map [k] [J] + dis [k] ;}}return dis [P-1] ;}int main () {int N; Double X, y1, Y2, Y3, Y4; while (scanf ("% d", & N) & n! =-1) {CNT = p = 0; point [p]. X = 0, point [p ++]. y = 5; while (n --) {scanf ("% lf", & X, & Y1, & Y2, & Y3, & Y4); line [CNT]. a. X = x, line [CNT]. a. y = 0; // edge line [CNT]. b. X = x, line [CNT ++]. b. y = Y1; line [CNT]. a. X = x, line [CNT]. a. y = Y2; line [CNT]. b. X = x, line [CNT ++]. b. y = Y3; line [CNT]. a. X = x, line [CNT]. a. y = Y4; line [CNT]. b. X = x, line [CNT ++]. b. y = 10; point [p]. X = x, point [p ++]. y = Y1; // Add some point [p]. X = x, point [p ++]. y = Y2; point [p]. X = x, point [p ++]. y = Y3; P Oint [p]. X = x, point [p ++]. y = Y4;} Point [p]. X = 10, point [p ++]. y = 5; set_map (); printf ("%. 2f \ n ", Dijkstra (); // G ++, use % F in poj, not % lf.} Return 0 ;}

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.