poj 1556 The Doors 线段相交,最短路
来源:互联网 发布:pc录屏软件 编辑:程序博客网 时间:2024/05/16 00:52
题目地址:poj1556
首先,这是个最短路问题,数据范围比较小,可以考虑floyd
首先不难想象,最后的路径,只要是拐点,那一定是经过某个门的端点的,否则稍微扰动就可以变大变小。
对于两点间路径的长度,他们连结线段被某条线段中断,那么他们的距离应该是通过连接某几个中间点完成的。否则,没有中断,那就是欧氏距离。
初始化以后,然后floyd一遍就可以了。
代码:
#include<iostream>#include<cmath>#include<cstdio>#include<algorithm>const double eps=1e-8;using namespace std;struct Point{ double x; double y; Point(double x=0,double y=0):x(x),y(y){} void operator<<(Point &A) {cout<<A.x<<' '<<A.y<<endl;}};int dcmp(double x) {return (x>eps)-(x<-eps); }typedef Point Vector;Vector operator +(Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y);}Vector operator -(Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); }Vector operator *(Vector A,double p) { return Vector(A.x*p,A.y*p); }Vector operator /(Vector A,double p) {return Vector(A.x/p,A.y/p);}// ps coutostream &operator<<(ostream & out,Point & P) { out<<P.x<<' '<<P.y<<endl; return out;}bool operator< (const Point &A,const Point &B) { return A.x<B.x||(A.x==B.x&&A.y<B.y); }bool operator== ( const Point &A,const Point &B) { return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0;}double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}double Cross(Vector A,Vector B) {return A.x*B.y-B.x*A.y; }double Length(Vector A) { return sqrt(Dot(A, A));}double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));}double Area2(Point A,Point B,Point C ) {return Cross(B-A, C-A);}Vector Rotate(Vector A,double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}Vector Normal(Vector A) {double L=Length(A);return Vector(-A.y/L,A.x/L);}Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){ Vector u=P-Q; double t=Cross(w, u)/Cross(v,w); return P+v*t; }double DistanceToLine(Point P,Point A,Point B){ Vector v1=P-A; Vector v2=B-A; return fabs(Cross(v1,v2))/Length(v2); }double DistanceToSegment(Point P,Point A,Point B){ if(A==B) return Length(P-A); Vector v1=B-A; Vector v2=P-A; Vector v3=P-B; if(dcmp(Dot(v1,v2))==-1) return Length(v2); else if(Dot(v1,v3)>0) return Length(v3); else return DistanceToLine(P, A, B); }Point GetLineProjection(Point P,Point A,Point B){ Vector v=B-A; Vector v1=P-A; double t=Dot(v,v1)/Dot(v,v); return A+v*t;}bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){ double c1=Cross(b1-a1, a2-a1); double c2=Cross(b2-a1, a2-a1); double c3=Cross(a1-b1, b2-b1); double c4=Cross(a2-b1, b2-b1); return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0 ; }bool OnSegment(Point P,Point A,Point B){ return dcmp(Cross(P-A, P-B))==0&&dcmp(Dot(P-A,P-B))<=0;}double PolygonArea(Point *p,int n){ double area=0; for(int i=1;i<n-1;i++) { area+=Cross(p[i]-p[0], p[i+1]-p[0]); } return area/2; }Point read_point(){ Point P; scanf("%lf%lf",&P.x,&P.y); return P;}const double INF=0x3fffffff;double d[100][100];Point p[100];int n;int N;void init(){ for(int i=0;i<100;i++) for(int j=0;j<100;j++) d[i][j]=(i==j)?0:INF; }void floyd(){ for(int k=0;k<N;k++) for(int i=0;i<N;i++) for(int j=0;j<N;j++) { d[i][j]=min(d[i][j],d[i][k]+d[k][j]); }}int main(){ double h[5]; while(cin>>n) { if(n==-1) break; init(); for(int i=0;i<n;i++) { for(int j=0;j<5;j++) scanf("%lf",&h[j]); for(int j=0;j<4;j++) p[4*i+j]=Point(h[0],h[j+1]); } N=4*n;; p[N++]=Point(0,5); p[N++]=Point(10,5); // for(int i=0;i<N;i++)// cout<<p[i]; for(int i=0;i<N;i++) for(int j=0;j<N;j++) { bool ok=1; for(int k=0;k<n;k++) { Point bottom=Point(p[4*k].x,0); Point top=Point(p[4*k].x,10); if(SegmentProperIntersection(p[i], p[j], bottom, p[4*k])){ ok=0; } if(SegmentProperIntersection(p[i], p[j], p[4*k+1], p[4*k+2])){ ok=0; } if(SegmentProperIntersection(p[i], p[j], p[4*k+3], top)){ ok=0; } } if(ok) d[i][j]=Length(p[i]-p[j]); } floyd(); printf("%.2f\n",d[N-2][N-1]); }}
0 0
- poj 1556 The Doors(最短路+判断线段相交)
- poj 1556 The Doors(最短路+判断线段相交)
- poj 1556 The Doors(线段相交+最短路)
- POJ 1556 The Doors(判断线段相交 + 最短路)
- poj 1556 The Doors 线段相交,最短路
- 【POJ】1556 The Doors 最短路+判断线段相交
- POJ 1556 The Doors 判断线段相交+最短路
- POJ 1556 The Doors(判断线段相交 && 最短路)
- POJ 1156 The Doors(线段相交+最短路)
- POJ 1556 The Doors 最短路floyd + 判断两线段相交
- Poj.1556 The Doors【线段相交+最短路】 2015/08/24
- POJ 1556 The Doors (计算几何判断线段相交+最短路)
- POJ1556 The Doors(线段与线段相交 + 最短路
- poj 1556 The Doors(线段交+最短路)
- POJ 1556 The Doors 点与线段交+最短路
- POJ 1556 The Doors(线段交+最短路)
- POJ 1556 The Doors (线段相交+Dijkstra)
- poj-1556-The Doors-dij+线段相交
- 金融校园通
- 患伺靥夜厥伦椒狄端刚跃谔炮琢啬
- Skyline_Analysis_CreateFlood
- Struts2输入校验
- 第六周作业——编码实现哈夫曼树
- poj 1556 The Doors 线段相交,最短路
- 如何启动、关闭和设置ubuntu防火墙
- 写着玩玩的密码验证
- 土法炼钢:服务器定时汇报IP地址给我
- 晨跑感悟:三快三爽三熬
- 不高兴的小明
- [leetcode]Maximum subarray
- EOSS V3.0.2 企业运营支撑系统(基于RBAC原理的权限管理)
- OSGI 中文版 3