poj3608Bridge Across Islands【旋转卡壳】
来源:互联网 发布:js 多维数组 编辑:程序博客网 时间:2024/06/05 08:06
Description
Thousands of thousands years ago there was a small kingdom located in the middle of the Pacific Ocean. The territory of the kingdom consists two separated islands. Due to the impact of the ocean current, the shapes of both the islands became convex polygons. The king of the kingdom wanted to establish a bridge to connect the two islands. To minimize the cost, the king asked you, the bishop, to find the minimal distance between the boundaries of the two islands.
Input
The input consists of several test cases.
Each test case begins with two integers N, M. (3 ≤ N, M ≤ 10000)
Each of the next N lines contains a pair of coordinates, which describes the position of a vertex in one convex polygon.
Each of the next M lines contains a pair of coordinates, which describes the position of a vertex in the other convex polygon.
A line with N = M = 0 indicates the end of input.
The coordinates are within the range [-10000, 10000].
Output
For each test case output the minimal distance. An error within 0.001 is acceptable.
Sample Input
4 40.00000 0.000000.00000 1.000001.00000 1.000001.00000 0.000002.00000 0.000002.00000 1.000003.00000 1.000003.00000 0.000000 0
Sample Output
1.00000
题意:给出两个多边形求他们之间最小距离
题中没说但后台数据应该是按照逆时针数据给出的所以不用再求凸包;
#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm> #define eps 1e-8#define inf 0x3f3f3f3fusing namespace std;struct point{double x,y;}A[10010],B[10010];double MIN(double a,double b){return a<b?a:b;}double dist(point a,point b){//求两点间距离 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double dotp(point p0,point p1,point p2){//点积 return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);}double cp(point p0,point p1,point p2){//叉积 return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);}double pld(point p1,point p2,point p3){//点线距 if(dist(p1,p2)<eps)return dist(p2,p3);if(dotp(p1,p2,p3)+eps<0)return dist(p1,p3);if(dotp(p2,p1,p3)+eps<0)return dist(p2,p3);return fabs(cp(p1,p2,p3)/dist(p1,p2));}double lld(point p1,point p2,point p3,point p4){//线线距 return MIN(MIN(pld(p1,p2,p3),pld(p1,p2,p4)),MIN(pld(p3,p4,p1),pld(p3,p4,p2)));}double rotating_calipers(point *p,point *q,int n,int m){//旋转卡壳 int i,d=0,u=0;p[n]=p[0];q[m]=q[0];for(int i=0;i<n;++i){if(p[i].y<p[d].y)d=i;//y值最小点 }for(int i=0;i<m;++i){//y值最大点 if(q[i].y>q[u].y)u=i;}double ans=inf;for(i=0;i<n;++i){double cnt;while(cnt=cp(p[d+1],q[u+1],p[d])-cp(p[d+1],q[u],p[d])>eps)u=(u+1)%m;if(cnt+eps<0)ans=MIN(ans,pld(p[d],p[d+1],q[u]));else ans=MIN(ans,lld(p[d],p[d+1],q[u],q[u+1]));d=(d+1)%n;}return ans;}int main(){int i,n,m;while(scanf("%d%d",&n,&m),n||m){for(i=0;i<n;++i){scanf("%lf%lf",&A[i].x,&A[i].y);}for(i=0;i<m;++i){scanf("%lf%lf",&B[i].x,&B[i].y);}printf("%.5lf\n",MIN(rotating_calipers(A,B,n,m),rotating_calipers(B,A,m,n)));}return 0;}
- poj3608Bridge Across Islands【旋转卡壳】
- poj3608Bridge Across Islands(旋转卡壳)
- 旋转卡壳解poj3608Bridge Across Islands
- POJ 3608 Bridge Across Islands(旋转卡壳)
- POJ:3608 Bridge Across Islands 旋转卡壳
- poj3608Bridge Across Islands+两凸包最近距离
- PKU 3608 Bridge Across Islands 凸包+旋转卡壳
- Bridge Across Islands----POJ_3608----求两凸包最近距离----旋转卡壳
- poj3608 Bridge Across Islands (旋转卡壳之两凸包最短距)
- poj 3608 Bridge Across Islands(旋转卡壳)
- poj 3608 Bridge Across Islands(旋转卡壳求凸包最短距离)
- POJ 3608 Bridge Across Islands (凸包+旋转卡壳)
- poj 3608 Bridge Across Islands(旋转卡壳求俩凸包间最小间距)
- POJ 3608 Bridge Across Islands(两个凸包最近距离,旋转卡壳)
- POJ 3608 Bridge Across Islands(旋转卡壳 求不相交凸包之间的最短距离)
- poj 3608 Bridge Across Islands(卡壳旋转求两个多边形的最近点对)
- poj 3608 Bridge Across Islands 旋转卡壳(两个凸包的最近距离)
- poj 3608 Bridge Across Islands, 旋转卡壳求凸多边形间最小距离
- Android 开发环境下载地址
- 站在巨人的肩膀上学习
- 错误Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1
- ubuntu下用命令行安装Qt
- java(1)--集合框架
- poj3608Bridge Across Islands【旋转卡壳】
- Dijkstra
- 设计模式3之工厂模式
- 第二章:java语法
- andrid五大布局
- Androdi网络连接——WebView、Volley、xutils
- 怎么让Html控件onclick事件和onserverclick事件同时有效
- 第三章:java流程语句
- iOS学习----UI阶段基础资料汇总