标签:des style blog http color os java io strong
Time Limit: 1000 MS Memory Limit: 10000 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
1 5 4 6 7 8 2 4 2 7 8 9 7 3 4.5 6 7 -1
10.00 10.06
#include <iostream> #include <string.h> #include <stdio.h> #include <cmath> using namespace std; #define maxx 100 #define INF 10000000 struct Node { double x; double y; } p[maxx]; ///每扇门的终点 起点 和门的两个端点的平面坐标 struct EDGE { int u; int v; } Edge[maxx*maxx]; ///存构造的边 因为之前是孤立的点 int n; ///n个墙 double wX[20]; ///输入每堵墙的横坐标 double py[20][4]; ///每堵墙横坐标对应的纵坐标 0 1 2 3 double g[maxx][maxx]; ///存邻接矩阵 配合dis[]的 double dis[maxx]; ///beg到其他点的最短距离 int Psize; ///边的数量 int Esize; ///点的数量 double Dis(Node a,Node b) ///计算亮点之间的距离
{ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double cross(double x1,double y1,double x2,double y2,double x3,double y3) ///判断(x3,y3)与(x1,y1)(x2,y2)是否交叉 { return (x2-x1)*(y3-y1)-(x3-x1)*(y2-y1); } bool IsOk(Node a,Node b) ///判断两点之间是否可以连线 { if(a.x>=b.x) return false; bool falg=true; int i=0; while(wX[i]<=a.x&&i<n) i++; while(wX[i]<b.x&&i<n) { if(cross(a.x,a.y,b.x,b.y,wX[i],0)*cross(a.x,a.y,b.x,b.y,wX[i],py[i][0])<0 ||cross(a.x,a.y,b.x,b.y,wX[i],py[i][1])*cross(a.x,a.y,b.x,b.y,wX[i],py[i][2])<0 ||cross(a.x,a.y,b.x,b.y,wX[i],py[i][3])*(cross(a.x,a.y,b.x,b.y,wX[i],10))<0) { falg=false; break; } i++; } return falg; } double Bellman(int beg,int end) { for(int i=0;i<maxx;i++) dis[i]=INF; dis[beg]=0; bool EX=true; for(int i=0;i<=Psize&&EX;i++) { EX=false; for(int j=0;j<Esize;j++) { if(dis[Edge[j].u]<INF&&dis[Edge[j].v]>(dis[Edge[j].u]+g[Edge[j].u][Edge[j].v])) { dis[Edge[j].v]=(dis[Edge[j].u]+g[Edge[j].u][Edge[j].v]); EX=true; } } } return dis[end]; } int main() { while(scanf("%d",&n)!=EOF) { if(n==-1) break; p[0].x=0; p[0].y=5; Psize=1; for(int i=0; i<n; i++) { cin>>wX[i]; for(int j=0; j<4; j++) { p[Psize].x=wX[i]; cin>>p[Psize].y; py[i][j]=p[Psize].y; Psize++; } } p[Psize].x=10; p[Psize].y=5; for(int i=0; i<=Psize; i++) { for(int j=0; j<=Psize; j++) { g[i][j]==INF; } } Esize=0; for(int i=0; i<=Psize; i++) { for(int j=i+1; j<=Psize; j++) { if(IsOk(p[i],p[j])) { g[i][j]=Dis(p[i],p[j]); Edge[Esize].u=i; Edge[Esize].v=j; Esize++; } } } printf("%.2lf\n",Bellman(0,Psize)); } return 0; }
标签:des style blog http color os java io strong
原文地址:http://www.cnblogs.com/zhangying/p/3940330.html