Calculate geometry + Shortest circuit
Enumerates whether a segment intersects a diagram and then runs the shortest
#include <cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<algorithm>using namespacestd;Const intmaxn= ++Ten;Const Doubleeps=1e-8;intN;intTotp,totl;structpoint{Doublex; Doubley;} p[ +];structline{Point A; Point B;} line[ +];structpath{intto ; DoubleVal; Path (intTDoublev) { to=T; Val=v; }};vector<Path>G[maxn];queue<int>Q;BOOLFLAG[MAXN];DoubleD[MAXN];DoubleXmult (Point p1,point p2,point p0) {return(p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (p1.y-p0.y);}intopposite_side (Point p1,point p2,point l1,point L2) {returnXmult (L1,P1,L2) *xmult (L1,P2,L2) <-EPS;}intintersect_ex (Point u1,point u2,point v1,point v2) {returnOpposite_side (U1,U2,V1,V2) &&opposite_side (V1,V2,U1,U2);}voidRead () {TotP=0, totl=0; p[totp].x=0, p[totp++].y=5; for(intI=1; i<=n; i++) { Doublex,a,b,c,d; scanf ("%LF%LF%LF%LF%LF",&x,&a,&b,&c,&d); P[TOTP+0].x=x,p[totp+0].y=0; P[TOTP+1].x=x,p[totp+1].y=A; P[TOTP+2].x=x,p[totp+2].y=b; P[TOTP+3].x=x,p[totp+3].y=B; P[TOTP+4].x=x,p[totp+4].y=D; P[TOTP+5].x=x,p[totp+5].y=Ten; LINE[TOTL].A=p[totp+0]; LINE[TOTL].B=p[totp+1]; Totl++; LINE[TOTL].A=p[totp+2]; LINE[TOTL].B=p[totp+3]; Totl++; LINE[TOTL].A=p[totp+4]; LINE[TOTL].B=p[totp+5]; Totl++; TotP=totp+6; } p[totp].x=Ten, p[totp++].y=5;}DoubleDis (point a,point b) {returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y));}voidSPFA () {memset (flag,0,sizeofflag); for(intI=0; i<= +; i++) d[i]=9999999; while(!Q.empty ()) Q.pop (); Q.push (0); flag[0]=1; d[0]=0; while(!Q.empty ()) { intHead=Q.front (); Q.pop (); for(intI=0; I<g[head].size (); i++) {Path Path=G[head][i]; if(d[head]+path.val<D[path.to]) {D[path.to]=d[head]+Path.val; if(!Flag[path.to]) {Flag[path.to]=1; Q.push (path.to); } } } }}intMain () { while(~SCANF ("%d",&N)) {if(n==-1) Break; Read (); for(intI=0; i<= +; i++) g[i].clear (); for(intI=0; i<totp; i++) { for(intj=i+1; j<totp; J + +) { if(ABS (p[i].x-p[j].x) <eps)Continue; BOOLFail=0; for(intk=0; k<totl; k++) if(INTERSECT_EX (p[i],p[j],line[k].a,line[k].b)) fail=1; if(!fail) { Doublelen=dis (p[i],p[j]); Path Path (J,len); G[i].push_back (path); }}} SPFA (); printf ("%.2f\n", d[totp-1]); } return 0;}
POJ 1556 The Doors