"POJ" 1556 the Doors (Computational Geometry Foundation +SPFA)

Source: Internet
Author: User
Tags ipoint

http://poj.org/problem?id=1556

First, each line of the path must be a connection between the endpoints. Prove? It's a pit. Anyway, I did a random painting and then I wrote it.

And what is the rhythm of re? I remember I had enough to drive ... And then open the big point just a ... It's so embarrassing.

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const double Eps=1e-6;int dcmp (double x) {return abs (x) <eps?0:(x<0?-1:1); }struct IPoint {double x, y;}; Double Icross (ipoint &a, IPoint &b, IPoint &c) {static double x1, x2, y1, y2;x1=a.x-c.x; y1=a.y-c.y;x2=b.x-c.x ; Y2=b.y-c.y;return X1*y2-x2*y1;} int Ijiao (ipoint &p1, IPoint &p2, IPoint &q1, IPoint &q2) {return (dcmp (Icross, p1, Q1)) Q2 (^DCMP (icross , Q1, Q2)) ==-2 && (dcmp (Icross (Q1, p1, p2)) ^dcmp (Icross (Q2, p1, p2))) ==-2;} const int n=1000;struct dat {int next, to; double W;} E[n<<2];int Ihead[n], cnt;void Add (int u, int v, double w) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt ].w=w;} Double SPFA (int s, int t, int n) {static double d[n];static int q[n], front, tail, u, v;static bool Vis[n];front=tail=0;fo R1 (i, 0, N) vis[i]=0, d[i]=1e99;d[s]=0; Q[tail++]=s; Vis[s]=1;while (Front!=tail) {u=q[front++]; if (front==n) front=0; VIS[U]=0;RDM (U, i) if (D[v=e[i].to]+eps>d[u]+e[i] . W) {d[v]=d[u]+e[i].w;if (!vis[v]) {vis[v]=1;if (d[v]<d[q[front]]+eps) {--front; if (front<0) Front+=N;q[front]=v;} else {q[tail++]=v; if (tail==n) tail=0;}}}} return d[t];} IPoint P[n], line[n*3][2];int N, pn, Ln;bool check (ipoint &x, IPoint &y) {For1 (i, 1, LN) if (Ijiao (x, y, line[i][0] , line[i][1])) return False;return true; Double Sqr (double x) {return x*x;} Double Dis (ipoint &x, IPoint &y) {return sqrt (SQR (x.x-y.x) +SQR (X.Y-Y.Y));} int main () {while (read (n), n!=-1) {ln=0; pn=0;++pn; p[pn].x=0; p[pn].y=5;++pn; p[pn].x=10; p[pn].y=5;static Double Rx, ry[ 4];while (n--) {scanf ("%lf", &rx), Rep (K, 4) scanf ("%lf", &ry[k]); ++ln; line[ln][0]= (ipoint) {rx, 0};line[ln][1] = (ipoint) {rx, ry[0]};++ln; Line[ln][0]= (ipoint) {rx, ry[1]};line[ln][1]= (ipoint) {rx, ry[2]};++ln; Line[ln][0]= (ipoint) {rx, ry[3]};line[ln][1]= (ipoint) {rx, 10};rep (k, 4) ++PN, P[pn].x=rx, p[pn].y=ry[k];}  For1 (i, 1, PN) For1 (j, 1, PN) if (i!=j && check (P[i], p[j])) Add (i, J, Dis (p[i], p[j]));p rintf ("%.2f\n", SPFA (1, 2, PN)); memset (ihead, 0, sizeof (int) * (pn+1)); cnt=0;} return 0;}

  

Description

You is to find the length of the shortest path through a chamber containing obstructing walls. The chamber always has sides at x = 0, x = ten, y = 0, and y = 10. The initial and final points of the path is always (0, 5) and (10, 5). There'll also is from 0 to vertical walls inside the chamber and each with the doorways. The figure below illustrates such a chamber and also shows the path of minimal length.

Input

The input data for the Illustrated Chamber would appear as follows.

2
4 2 7) 8 9
7 3 4.5) 6 7

The first line contains the number of interior walls. Then there are a line for each such wall, containing five real numbers. The first number is the X coordinate of the wall (0 < x < ten), and the remaining four are the y coordinates of the E NDS of the doorways in that wall. The x coordinates of the walls is in increasing order, and within all line the y coordinates is in increasing order. The input file would contain at least one such set of data. The end of the data comes when the number of walls is-1.

Output

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to both decimal places past the decimal point, and always showing t The He and the decimal places past the decimal point. The line should contain no blanks.

Sample Input

15 4 6 7 824 2 7 8 97 3 4.5 6 7-1

Sample Output

10.0010.06

Source

Mid-Central USA 1996

"POJ" 1556 the Doors (Computational Geometry Foundation +SPFA)

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: info-contact@alibabacloud.com 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.