Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7430 | Accepted: 2915 |
Description
Input
Output
Sample Input
1 5 4 6 7 8 2 4 2 7 8 9 7 3 4.5 6 7 -1
Sample Output
10.00 10.06
Source
【思路】
枚举所有点,如果不与竖边相交则连边,做最短路即可。
【代码】
1 #include<cmath> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++) 6 using namespace std; 7 8 const int N = 300+10; 9 const double INF = 1e9; 10 const double eps = 1e-8; 11 12 int dcmp(double x) { 13 if(x<eps) return 0; else return x<0? -1:1; 14 } 15 16 struct Pt { 17 double x,y; 18 Pt(double x=0,double y=0):x(x),y(y) {}; 19 }; 20 struct Seg { Pt a1,a2; }; 21 typedef Pt vec; 22 23 vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); } 24 bool operator != (Pt A,Pt B) { 25 if(dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0) return 0; 26 else return 1; 27 } 28 29 double cross(Pt A,Pt B) { return A.x*B.y-A.y*B.x; } 30 31 bool SegInter(Pt s1, Pt e1, Pt s2, Pt e2) { 32 if( 33 cross(e1-s1,s2-s1) * cross(e1-s1,e2-s1) <= 0 && 34 cross(e2-s2,s1-s2) * cross(e2-s2,e1-s2) <= 0 35 ) return true; 36 return false; 37 } 38 double dist(Pt a,Pt b) { 39 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 40 } 41 double f[N][N]; 42 Seg L[N]; int lc; 43 Pt P[N]; int pc; 44 int n; 45 46 int main() { 47 while(scanf("%d",&n)==1 && n>0) { 48 pc=lc=0; 49 FOR(i,1,n) { 50 double x,y1,y2,y3,y4; 51 scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4); 52 L[++lc]=(Seg) {Pt(x,0),Pt(x,y1)}; 53 L[++lc]=(Seg) {Pt(x,y2),Pt(x,y3)}; 54 L[++lc]=(Seg) {Pt(x,y4),Pt(x,10)}; 55 P[++pc]=Pt(x,y1) , P[++pc]=Pt(x,y2); 56 P[++pc]=Pt(x,y3) , P[++pc]=Pt(x,y4); 57 } 58 P[++pc]=Pt(0,5), P[++pc]=Pt(10,5); 59 FOR(i,1,pc) FOR(j,1,pc) f[i][j]=INF; 60 FOR(i,1,pc) FOR(j,i+1,pc) { 61 bool flag=1; 62 FOR(k,1,lc) 63 if(SegInter(P[i],P[j],L[k].a1,L[k].a2)) 64 { flag=0; break; } 65 if(flag) 66 f[i][j]=f[j][i]=dist(P[i],P[j]); 67 } 68 FOR(i,1,n) { 69 FOR(j,i+1,n) if(f[i][j]!=INF) 70 printf("%d,%d : %.2lf\n",i,j,f[i][j]); 71 } 72 FOR(k,1,pc) FOR(i,1,pc) FOR(j,1,pc) 73 f[i][j]=min(f[i][j],f[i][k]+f[k][j]); 74 printf("%.2lf\n",f[pc-1][pc]); 75 } 76 return 0; 77 }